Functors: I was WRONG! - FunFunFunction #11

Поділитися
Вставка
  • Опубліковано 20 сер 2024
  • 💖 Support the show by becoming a Patreon
    / funfunfunction
    A functor is an object that has a map method. Arrays in JavaScript implement map and are therefore functors. Promises, Streams and Trees often implement map in functional languages, and when they do, they are considered functors. The map method of the functor takes it’s own contents and transforms each of them using the transformation callback passed to map, and returns a new functor, which contains the structure as the first functor, but with the transformed values.
    Previous video on functors
    • Functors - FunFunFunct...
    Map
    • Map - Part 2 of Functi...
    Promises
    • Promises - Part 8 of F...
    Great functional person
    / drboolean
    "What is a Functor?" by Tikhon Jelvis
    www.quora.com/...
    Best place to learn Haskell
    learnyouahaskel...
    Full script here
    / 5f794a1ca246
    💛 Follow on Twitch and support by becoming a Subscriber
    We record the show live Mondays 7 AM PT
    / funfunfunction
    💛 Fun Fun Forum
    Private discussion forum with other viewers in between shows. www.funfunforu.... Available to patron members, become one at / funfunfunction
    💛 mpj on Twitter
    / mpjme
    💛 CircleCI (Show sponsor)
    Robust and sleek Docker-based Continuous Integration as a service. I used CircleCI prior to them becoming a sponsor and I love that their free tier is powerful enough for small personal projects, even if they are private. Use this link when you sign up to let them know you came from here:
    circleci.funfu...
    💛 Quokka (Show sponsor)
    Wonder how MPJ evaluates JavaScript inline his editor. Quokka is the answer - use this link when you buy to let them know you came from here:
    quokka.funfunfu...
    💛 FUN FUN FUNCTION
    Since 2015, Fun Fun Function (FFF) is one of the longest running weekly UA-cam shows on programming 🏅 thanks to its consistency and quality reaching 200,000+ developers.
    🤦‍♂️ The Failing Together concept is what makes FFF unique. Most coding content out there focus on step-by-step tutorials. We think tutorials are too far removed from what everyday development is like. Instead, FFF has created a completely new learning environment where we grow from failure, by solving problems while intensively interacting with a live audience.
    Tutorials try to solve a problem. Failing Together makes you grow as a developer and coworker.
    📹 Each show is recorded live on Twitch in a 2-hour livestream on Mondays. The host, assisted by the audience, is tasked to complete a programming challenge by an expert guest. Like in the real world, we often fail, and learn from it. This, of course, reflects what the audience identifies with, and is one of the most praised aspects of the show.
    ⏯ On Fridays, an edited version of the show is adapted for and published on UA-cam.
    Content Topics revolve around: JavaScript, Functional Programming, Software Architecture, Quality Processes, Developer Career and Health, Team Collaboration, Software Development, Project Management

КОМЕНТАРІ • 167

  • @RobertFerentz
    @RobertFerentz 8 років тому +77

    Admitting you were wrong... you'd make a terrible politician. :P

    • @manualvarado2212
      @manualvarado2212 6 років тому +2

      Hahahaha!

    • @Yetipfote
      @Yetipfote 5 років тому +1

      wish more politicians would be like that :(

    • @chromosundrift
      @chromosundrift 4 роки тому +1

      @@Yetipfote let's vote for those ones then ;)

    • @chromosundrift
      @chromosundrift 4 роки тому

      On a more positive note, admitting you're wrong enables you to learn and makes you more trustworthy to others trying to learn!

  • @iAmTheSquidThing
    @iAmTheSquidThing 7 років тому +54

    So… In short, a functor is a collection that can be passed a function and apply it nondestructively to all of its elements, returning another functor?

    • @funfunfunction
      @funfunfunction  7 років тому +28

      +Andy Brice you've just invented the most succinct explanation of it I've ever heard. The only thing I would add is that it returns a functor of the same TYPE.

    • @iAmTheSquidThing
      @iAmTheSquidThing 7 років тому +6

      funfunfunction Ahh, I guess so that you can chain a bunch of functions together in whatever order, secure in the knowledge that they'll all be operating on the same type of object.

    • @funfunfunction
      @funfunfunction  7 років тому +3

      +Andy Brice precisely.

    • @AndersJackson
      @AndersJackson 7 років тому

      Same type?
      You had an array of dragons and then an array of string and last an array of int. They are not the same type.
      The structure are isomorph, that is, it looks like the original in structure. Where each element is transfered to another type by the function sent to map.
      Ok, in Java script I belive you are allowed to store mixed types in arrays.
      And yes, @Andy made a very short and good explanation of a Functor.

    • @AndersJackson
      @AndersJackson 7 років тому

      ***** well, actually it isn't. The content type is part of the type. And yes, I know some languages are not that particular about type checking. That they treat Arrays as a generic datatype, where you can store objects of any type. Same with lists.
      Doesn't change that an array of int and array of char are different types though.

  • @EivindDahl
    @EivindDahl 4 роки тому +1

    In short, a functor is an assignment of, for any type T, a type FT, and for any transformation f : T ---> U a transformation Ff : FT ---> FU subject to certain niceness conditions. Write FT as T[] and Ff as map(f) and you're in business.

  • @scuden
    @scuden 8 років тому +22

    I absolutely love this guy

  • @Vintharas
    @Vintharas 8 років тому +6

    Excellent vid! Hilarious as usual and very interesting as well! Function composition FTW! Also very brave to shout to the world that you were wrong :)

  • @coolworx
    @coolworx 8 років тому +14

    Am I the only one that thinks the host should experiment with wigs?

  • @yasinyaqoobi
    @yasinyaqoobi 4 роки тому +1

    I think functors must also preserve composition so map (f . g) == map f . map g , which i think means ---> [1,2,3].map(x => double(x)).map(x => square(x)) == [1,2,3].map(x => square(double(x))).

  • @salvacarsimoreno5228
    @salvacarsimoreno5228 7 років тому +1

    You got it really wrong in the previous video haha, even though it help a lot to understand the reason for functors to exist. Thanks for the rectification, its a while I am trying to understand functors and monads and your videos are a big help. Keep on the good work!

  • @tymon111
    @tymon111 8 років тому +3

    "Secret club became so secret that it has no members"
    :D

  • @solvm1652
    @solvm1652 8 років тому +2

    Awesome! Thanks for the clarification. Way to handle being "wrong" like a G! You're a developer's developer.

  • @kaicurry9111
    @kaicurry9111 8 років тому +23

    The hat!!! Thanks for another fantastic video.

  • @DennisRiungu
    @DennisRiungu 7 років тому

    You are simply the best I have watched in a while!! You made me get more serious on functional!!!

  • @jagk4459
    @jagk4459 4 роки тому

    Thanks for sharing. I like your videos. It makes my day happier. =)
    One small suggestion, maybe, you can accompany the video with downloadable notes that is easy to understand for anyone (children etc.), i.e: PDF format. For example, in this video, I learned that functors have three characteristics;
    1. Transformation of content
    2. Maintain the structure of content
    3. Returns a new functor
    Something like that. Again, thank you very much. I learned a lot from you. =)

  • @lifematch
    @lifematch 8 років тому +1

    Oh if you come in the c++ land. Functor is basically a class with bunch of operator overloading which makes it seem like a function that can handle multiple arguments.

  • @vbikcl
    @vbikcl 8 років тому

    The simplest definition of a functor is an object which acts as a container for a value, which then allows to you APPLY one or more functions to that value, and returns a new functor containing the transformed value.
    It's not about having a map method per se, it's just the idea of wrapping a simple value in an object that then allows you to "decorate" it with functionality to be evaluated either eagerly, or lazily.

  • @Shambo271
    @Shambo271 5 років тому

    There is so much confusion around functors and monads and your forthrightness is appreciated.

  • @pdowner1
    @pdowner1 6 років тому

    I am glad to be getting to things I dont understand. seriously, thank you!

  • @pol...
    @pol... 8 років тому +1

    What about strings in Haskell where they are defined as a list of characters?
    You can very well map a function that does, let's say return True if the character is 'a' and False else. In this sense the structure is conserved because you give it a list of Chars and returns a list of any other things (in the example booleans).
    So doing:
    let as = map isA "abc"
    is the same as doing:
    let as = map isA ['a','b','c']
    and as will have the value of
    [True, False,False]
    ______________________________
    Code example
    mapFunction :: (a -> b) -> [a] -> [b]
    mapFunction _ [] = []
    mapFunction f (x:xs) = f x : mapFunction f xs
    isA :: Char -> Bool
    isA 'a' = True
    isA _ = False
    ghci: mapFunction isA "abc"
    [True, False, False]
    ______________________________________
    So if I am getting this right, strings in Haskell (since they are just a type synonym for [Char]) are functors.
    Am I right? Could you please help me shine some light on this issue?
    Btw. Very nice channel and video. I am subscribing. :)

  • @marcnitzsche
    @marcnitzsche 7 років тому +16

    I like watching those videos on 1.5x.

  • @gregsmith80
    @gregsmith80 8 років тому

    The last two videos were the best in a while.

  • @insertoyouroemail
    @insertoyouroemail 4 роки тому +1

    .then is map for promises. .then is also bind because it is overloaded to also accept a lambda that returns another promise.

  • @kevinpeter7202
    @kevinpeter7202 7 років тому

    The Kate Beaton pony shirt again. Yay!
    Also, although there was lots of interesting content regarding functors, the strange dancing Santa hat alone made this video worth watching

  • @SeanForeman
    @SeanForeman 5 років тому

    That was great. I eeally enjoyed your explanation of what a functor is and why your prior video was wrong.

  • @corlaez
    @corlaez 4 роки тому

    Thank you very much for fixing the mistakes. I respect that.

  • @alihammadshah
    @alihammadshah 7 років тому +1

    Functors are 1-morphism of Objects(A,B...) eg. A -> B in 2-category where objects are categories (1-cat). How come morphism become an object (that implements map), shouldn't the functor be the function that's passed into the map?

  • @klausklemper5987
    @klausklemper5987 5 років тому +1

    I love your humor !!:D

  • @paganaye
    @paganaye 8 років тому

    I understood where you come from. Personnally I will still call the map function a Functor rather than the "high order function". I certainly hope that the functor denomination will stick.

  • @cKFSDev
    @cKFSDev 7 років тому

    @3:25 Dat Face hahaha. You are awesome man! Keep it up!

  • @joseaquino96
    @joseaquino96 8 років тому

    Is that a painting of Yoshi in a go cart from Mario Kart in the background? if so, I love it and love your videos, very interesting and relevant subjects, even when they are wrong!

    • @funfunfunction
      @funfunfunction  8 років тому +1

      It's actually a yoshi made with fuse beads glued to an vintage cross-stitch. It's made by @pappasparlor on Instagram, they are amazing.

    • @joseaquino96
      @joseaquino96 8 років тому

      It is amazing! just started following him on Insta, and thanks for all the videos, been binge watching them

  • @azs06
    @azs06 7 років тому +1

    Fun and educational, what else you need.

  • @haojiang4882
    @haojiang4882 4 роки тому

    It makes sense that you used to work in the theatre industry.

  • @peripona
    @peripona 7 років тому

    hahahah that sigh, on watching Beard... @3:32

  • @alexandersobolev5284
    @alexandersobolev5284 7 років тому

    +funfunfunction,
    I don't think you need to do any transformation to be qualified as a functor. Functor - as mentioned in this video - is a sort of a Container object which has map (for chaining) and flatMap (to unfold the final value when chaining is finished) methods on it. That's it. Check out my own quick functor:
    const Container = x => ({
    map: f => Containter( f ( x ) ),
    flatMap: f => f( x )
    });
    But as far as I understand it 'map' method is there just to run a function in the given context. That's it!!! For example, Container( 7 ).map(num => num). I don't modify anything here, I just return the same thing back into my Container. But it's still a functor, because my Container is a functor no matter what function I choose to run in the given context (i.e. whatever function I choose to map).

  • @evariste6116
    @evariste6116 5 років тому

    Can't make the 'bluebird' example work as listed. Get "Unhandled rejection TypeError:expecting an array or an iterable object but got [object Null]...." (node v10.4.1, bluebird 3.5.2). Adding a reject method to promise only delayed the error! ?....but learnt something in trying so thanks!

  • @przemysawlib4309
    @przemysawlib4309 7 років тому

    Laws are must have when writing Your own Functor. Braking laws means composition will be broken.

  • @chromosundrift
    @chromosundrift 4 роки тому

    "if I had a bigger beard... " LOL

  • @bartsmykla
    @bartsmykla 8 років тому +1

    One insignificant hint from me. I like your videos, but think to use another sounds from this YT defaults. I think, that I hear them in almost every YT video. :-D
    Great video. You are making good work! :-)

    • @funfunfunction
      @funfunfunction  8 років тому

      +Bartłomiej Smykla Not from the UA-cam defaults. :)

    • @bartsmykla
      @bartsmykla 8 років тому

      +mpjme Oh, then apologise. But still I hear that so many times a day, so I just thought it's YT defaults. :-D

  • @CarlosBaraza
    @CarlosBaraza 8 років тому +3

    So if I understood it right, you Christmas hat is a Monad.

    • @funfunfunction
      @funfunfunction  8 років тому +7

      +Carlos Baraza Well, it certainly encapsulates some messed up behavior.

  • @p4chw1n
    @p4chw1n 8 років тому

    I really enjoyed the video (as well as any previous one) but I am paranoid (JSX thaught me that). You have Syntax Error in second example, resolve function missing closing ). You also returning there {{name: 'Fluffykins', health: 70}} which is another syntax error and then call on this value .map() which would throw one more error.
    As a punishment I sentence you to do this great videos until the universe ends! :)

    • @funfunfunction
      @funfunfunction  8 років тому

      +p4chw1n I accept your punishment! :) Great catch, btw. I actually knew about the first one but ignored it in the edit in favor to get the episode out on time, but the second one completely passed me by.

  • @elie2222
    @elie2222 8 років тому +1

    Great video as always

  • @armanb9778
    @armanb9778 7 років тому

    MPJ, can you please bring back the "fluhluhluhluh!" at 2:57? It's the best.
    EDIT:
    Actually, "Blehelele!" at 3:28 is best.

  • @vedovelli
    @vedovelli 8 років тому

    Thanks for the series! It's awesome!

  • @Ashiq619
    @Ashiq619 6 років тому

    I enjoy ur videos ... Just one thing that I would like to tell u is please correct that example where u r using promise from bluebird ... One should pass an array of dragon in to resolve function and not single dragon object ...othetwise ur map will fail ... Try it :)

  • @djxak
    @djxak 7 років тому +1

    So, the Functor is just an object, that has a "map" function (where "map function" is a function, that returns new object of the same type and shape)? If that is correct, then the only one required operation of functor is a "map" operation, right?

  • @karrorri1097
    @karrorri1097 3 роки тому

    any value on which "map" can be implemented is a functor?

  • @JohnMichaelReed
    @JohnMichaelReed 8 років тому +3

    That was really interesting. So what is a Monad? Because I thought that a Monad was like a composable thing that takes in a function, but then I realized that the thing I thought was a Monad was basically the same as what you are describing.

    • @funfunfunction
      @funfunfunction  8 років тому +12

      +John Michael Lafayette You will be able to watch that in the upcoming video about monads, and the undoubtedly subsequent video about how I was wrong about monads. :)

    • @funfunfunction
      @funfunfunction  8 років тому +1

      +mpjme A monad is Functor with some specific behaviour, by the way. That is why I'm explaining functors at all, it's a stepping stone to monad.

    • @JohnMichaelReed
      @JohnMichaelReed 8 років тому

      +mpjme Oh. So the Monad isn't the thing that is doing the propagating. It's the thing that is being propagated (the specific behavior that is being propagated). Monad encapsulates behavior.

    • @JohnMichaelReed
      @JohnMichaelReed 8 років тому

      +mpjme Too soon.

    • @gabrielvergnaud2432
      @gabrielvergnaud2432 8 років тому

      +John Michael Lafayette here is the great mostly adequate guide to functionnal programing from Dr. Boolean, which explains very well what it is => drboolean.gitbooks.io/mostly-adequate-guide/content/ch9.html
      Basically it's just a wrapper like a functor, with a "join" method on it.

  • @barbarakendal5739
    @barbarakendal5739 6 років тому

    I tried to scroll down to see the rest of the t-shirt :)

  • @jnfsmile
    @jnfsmile 8 років тому

    If it maintains structure doesn't it follow that it returns a functor? What's the difference between 2 & 3?

  • @blazedb420
    @blazedb420 7 років тому

    Your videos rock sir! Thank you

  • @jamma246
    @jamma246 6 років тому

    It's pretty simple. A functor is the following:
    - a type constructor F, which takes one type and returns another (that is, for a type a we have another type F a);
    - for which we also need some function fmap :: (a -> b) -> (F a -> F b) for all types a and b.
    That's all it is. But we do require the following two axioms:
    - if id is the identity map id :: a -> a on the type a (that is, id x = x) then fmap id is the identity map on F a
    - if f :: a -> b and g :: b -> c, as usual we can compose them to g . f :: a -> c. Then we require that
    fmap (g . f) = (fmap g) . (fmap f).
    I come at this more from mathematics, so I think having the axioms is important. But programmers are likely going to be more interested in just _implementing_ functors, so defining the type constructor F and the fmap. It turns out that if you can define a type constructor F and implement the fmap as above, then you only need to check the first axiom to get the second, seemingly more complicated one, for free (I could be wrong about that, but I think this is a so-called "theorem for free". It comes from the restriction of having to define fmap parametrically, so strictly speaking I'm restricting to certain kinds of languages here, like Haskell).
    The list functor is an example. Given a type a, we can define the type [] a, more commonly written [a], which is the type of lists of elements in a. For example, x = [1,15,-2,3] is an element of [Int]. If you have a map f :: a -> b, then you get in an obvious way a map which takes lists of a's and returns lists of b's: just apply f to each element in the list. For example, if f :: Int -> Int is the times two map, then fmap f x = [2,30,-4,6].
    Now applying the identity map to each element of a list gives back the same list, so fmap id is the identity. And if you have two composable functions, f and g, then mapping each element of a list by g after f is the same thing as first applying f to each element of the list, and then applying g to each element of that list. That is, fmap (g . f) = (fmap g) . (fmap f). So the functor axioms are satisfied.
    Whenever you have a type constructor as above with some parametric fmap implemented, the chances are that it will be a functor.

  • @ChuckJHardy
    @ChuckJHardy 7 років тому

    Great episode. Thanks 😜

  • @trex_son5705
    @trex_son5705 7 років тому

    in angular 2, is this also considered a functor:
    this.http.get('....json?auth='+ token)
    .map(...)

  • @mikoajg7534
    @mikoajg7534 7 років тому

    I LOVE this!

  • @mfaine
    @mfaine 6 років тому

    +1 for correcting yourself.

  • @erikw4193
    @erikw4193 7 років тому

    that drag at 5:41 tho

  • @Goofy8907
    @Goofy8907 7 років тому +1

    Hello everyone, I've been having some trouble with some of the code in the video for some time
    let Promise = require('bluebird')
    const whenPkmnIsLoaded = new Promise((resolve, reject) => {
    setTimeout(() => resolve({
    { name: 'Pikachu', type: 'electric' }
    }, 2000)
    });
    const thing =
    whenPkmnIsLoaded
    .map(pkmn => pkmn.name)
    .then(name => console.log(name))
    // Also, I installed bluebird like so:
    // npm install bluebird --save
    Where am I screwing up?
    Thanks

    • @t0m4_
      @t0m4_ 7 років тому

      Hello!
      I think his example was wrong too, because he gave timeout value to the resolve, instead of setTimeout, also you cant put an object inside of an object like that, that will cause a javascript error, so i've corrected your code and this should work nicely:
      let Promise = require('bluebird')
      const whenPkmnIsLoaded = new Promise((resolve, reject) => {
      setTimeout(() => resolve([
      { name: 'Pikachu', type: 'electric' }
      ]), 2000)
      })
      const thing =
      whenPkmnIsLoaded
      .map(pkmn => pkmn.name)
      .then(names => console.log(names))

    • @shadowdrakon9913
      @shadowdrakon9913 6 років тому

      Goofy8907 I think it may because he’s using ES6

    • @winterrcore
      @winterrcore 6 років тому

      It should be like this, since bluebird's map method expects an iterable.
      setTimeout(() => resolve([{ name: "Fluffykins"}]), 2000);
      from what I understood it works just like Array.prototype.map, except that it waits until the promise resolves and you can use it directly
      So basically this
      whenDragonLoaded
      .map(dragon => dragon.name)
      .then(console.log);
      Is equivalent to this
      whenDragonLoaded
      .then(dragons => dragons.map(dragon => dragon.name))
      .then(console.log);

  • @RevisionSeventeen
    @RevisionSeventeen 8 років тому

    I think in the parts of this video where you pop up in the back of the room you're out of focus a bit.
    Other than that great video! Keep up the good work!

  • @luisraulaceretoricalde4536
    @luisraulaceretoricalde4536 8 років тому +2

    Great channel. I love it.
    Is it me, or the code for the second example does not work?

    • @Bigdaddy91982
      @Bigdaddy91982 7 років тому

      Just you buddy!

    • @ericlif1
      @ericlif1 4 роки тому

      4 years later, it's not just you. Map is not a function declared on Promise prototype object. As you can see on the video, at the very top of the example is using Bluebird (external library) Promise which has a map defined.

  • @iAmTheSquidThing
    @iAmTheSquidThing 7 років тому

    I've started learning Haskell recently. And perhaps the reason for this confusion in the community is that in pure functional programming, the boundaries between objects and functions get a bit blurry. They seem to be both referred to more vaguely as a "computational context" which I think is something slightly esoteric stemming from category theory.

  • @AndersRapp
    @AndersRapp 8 років тому

    Apart from the great content let me give a shout out to the awesome t-shirts! :-P Also, do a video on prototypes! It might not be functional programming in itself but it's a concept that even the best books (like Learning Ecmascript 6) get confused.

    • @funfunfunction
      @funfunfunction  8 років тому

      +Anders Rapp I have made a video about that already. ;)

    • @AndersRapp
      @AndersRapp 8 років тому

      +funfunfunction I'll go check that out then. Thanks and keep up the good work! :)

  • @smbossco
    @smbossco 8 років тому

    doesnt the map function take three parameters? the last one being the array itself... or does that not count as a 3rd parameter for some reason?

  • @jt0308
    @jt0308 2 роки тому

    I GET IT NOW!! thank you

  • @lifematch
    @lifematch 8 років тому

    I like how you say functor!!!

  • @osakaandrew
    @osakaandrew 7 років тому

    If `String` + `map` is not a functor because the `map` only works for string-returning transformations, then JS's Typed Arrays must not be functors either:
    let x = new UInt8Array([1,2,3,4]);
    x.map(x => false) // [0, 0, 0, 0]
    I'm not saying that this distinction is wrong, but that the distinction has to be implementation-specific or the definition is useless. If Typed Arrays are functors (and they must be, to my thinking) then `String+map` must be as well. Just a thought.

    • @funfunfunction
      @funfunfunction  7 років тому

      +Andrew Adamson its not string specifically, it must maintain form. The .map of a Widget must return another Widget, or it is not a functor. If UInt8Array.map would have returned say, a normal array, instead of a UInt8Array then it would violate the functor contact. This is bad because then chainability has been compromised and we would not be able to do:
      myByteArray.map(someTransformationFunc).buffer()

  • @IonMasuna
    @IonMasuna 7 років тому

    I laughed so hard in the hat part that I cried xDD

  • @ladrillopendejo
    @ladrillopendejo 8 років тому +1

    Hi Matt, I was just wondering but did you get that horse shirt online? If so can you tell us all where you got it!!!

    • @funfunfunction
      @funfunfunction  8 років тому +1

      +ladrillopendejo Glad you asked. It's Pony, one of the characters that the amazing Kate Beaton draws. It's in her store here: www.topatoco.com/merchant.mvc?Screen=CTGY&Store_Code=TO&Category_Code=BEAT She is a comic book author that I'm a very big fan of. Check her out: www.harkavagrant.com/index.php?id=131

  • @juliankrispel-samsel218
    @juliankrispel-samsel218 8 років тому

    I adore your videos

  • @graymur07
    @graymur07 8 років тому

    Actually, callback to Array.prototype.map() takes three arguments. *Flies away to do homework*

  • @NipunaGunathillake
    @NipunaGunathillake 8 років тому

    Doesn't filter function transform the content, Maintain structure and can be chained?
    So does that mean an object containing a filters are functors as well?

    • @funfunfunction
      @funfunfunction  8 років тому +1

      +Nipuna Gunathillake Map and filter reminds about each other, but they are actually very different. map and filter seem more similar than they are. Filter changes the array structure, not the contents, while map does the opposite. This might be hard to grasp if you are thinking of an array as bunch of items - instead, in your mind, try to separate the concepts of the items and the structure that is the array. Let me explain:
      Imagine array as a container that looks like this:
      img1.etsystatic.com/000/0/6248605/il_340x270.329955061.jpg
      The balls are the array items, and the steel frame that the balls lie in is the array. I know that it's easy to think of array as just a set of items because you can add and remove items from an array in JavaScript, but instead try to separate the items and the structure in your mind. Think of an array not as a set of items, but as a tray that contains the items.
      The tray is a functor, so it provides a slot on the side, labeled map, into which you can insert a transformation function. If you do, a new tray of the *exact same size* is created, but with all items transformed through the transformation function. The structure, the tray, has NOT been changed, but all items have been changed.
      There is also a slot for filter, but filter does almost the exact opposite, it passes every item through the filter function, and if the filter function returns true, that items is deemed to make the cut to be in the new tray, which is created. The individual items are NOT transformed (some are thrown away though), but the tray has been changed into a smaller tray.

    • @NipunaGunathillake
      @NipunaGunathillake 8 років тому

      Got it! Thanks for the awesome explanation. Filters change the array(container) not just it's container.
      It's a bit difficult getting my head around all the functional programming concepts but your videos have definitely made it far more fun! Thanks for all the videos.

  • @cpakken
    @cpakken 8 років тому

    Wouldn't chaining on map be non-optimal since it will loop over the array again? (Imagine a very BIG array)

    • @funfunfunction
      @funfunfunction  8 років тому

      +Chris Kwong in a real life situation, you'd chain array.filter().map() if the array was very large and the map was very expensive. But it would have to be very large for that to matter - a modern JS engine on a smartphone is very fast - I just tried and my phone maps over 22 million items per second. I'm not a fan of discussing performance on a line by line level because I don't think that is how performance works (see my elaboration on the topic here ua-cam.com/video/PhUb7y9WZGs/v-deo.html).

  • @Gabriel-Aggrey
    @Gabriel-Aggrey 8 років тому +1

    what are the things that make up your media station ( mic, camera, software for editing )? please respond

    • @funfunfunction
      @funfunfunction  8 років тому +1

      Standard UA-cam stuff all of it. Midrange DSLR, cheap soft lights, blue yeti, FCPX

  • @leandrogoncalvesdeoliveira9201
    @leandrogoncalvesdeoliveira9201 8 років тому

    Hahaha, you're crazy!
    Awesome video!
    Thanks!

  • @leftover7766
    @leftover7766 6 років тому

    I bet that pooch runs away next time you whip out your wood. (cause you really screw the pooch!)
    BUT mistakes often provide more opportunity for learning than slick perfection. I got a LOT out of this.

  • @lukewright5544
    @lukewright5544 7 років тому

    I like your clips very much and the way how you marry fun and function ;) ever thought about teaching yound "want-to-be"-programmers?!

    • @funfunfunction
      @funfunfunction  7 років тому +1

      That is who the channel is targeted to. Or rather, programmers that are about 1 year into their first job - that is who I imagine when I make the videos.

  • @47beethoven
    @47beethoven 7 років тому

    Yo mpj (or anyone really). Just a question: if i chain maps (sometimes ppl do for the sake of composing), will it traverse the collection multiple times? Like: square_list.map(square => square.base).map(size => Math.pow(size, 2)) (and later i could perform a reduce or whatever for exemple). I mean, it probably should but compilers have cool tricks and all... so i'd sleep better at night if it wouldnt (in the case that the execution of those statements wouldnt produce colateral effects).

    • @funfunfunction
      @funfunfunction  7 років тому

      +47beethoven it will. There is no way for the compiler to optimize this for you in JavaScript, but you can compose the two functions into one before passing it to map and thereby have only a single map. See compose function in the rambda library, for example.

    • @47beethoven
      @47beethoven 7 років тому

      Cool, i will check it out. Nice that you answer questions on year's old videos.

  • @Luxalpa
    @Luxalpa 6 років тому

    I approve the dragon objects!

  • @nicolasschurmann
    @nicolasschurmann 8 років тому

    Fantastic! :)

  • @yasaamoin4882
    @yasaamoin4882 7 років тому

    I need to sleep on this

  • @markuswilson3778
    @markuswilson3778 5 років тому

    I need that shirt! xD

  • @maxc101
    @maxc101 7 років тому

    Haha love ya brother!

  • @minispecs123
    @minispecs123 8 років тому

    could a POJO be a functor?

    • @funfunfunction
      @funfunfunction  8 років тому

      +minispecs123 It could if it implemented map.

  • @JasonCtutorials
    @JasonCtutorials 8 років тому +1

    HAHA I LOVE THIS GUY!

  • @jaimeg.8855
    @jaimeg.8855 8 років тому

    where do you get all these cats shirts..gear

    • @iAmTheSquidThing
      @iAmTheSquidThing 7 років тому +1

      I'm no zoologist, but I'm pretty sure those are horses.

    • @jaimeg.8855
      @jaimeg.8855 7 років тому +1

      hah yeh im not sure where the cat came from

  • @Mike-rt2vp
    @Mike-rt2vp 6 років тому

    I now have no idea what a functor is... I wanted to simplify it to a function that operates on a collection and takes another function as a argument. But now I have to clean my brain off the floor because it exploded.

    • @funfunfunction
      @funfunfunction  6 років тому +1

      You might want to look at my functor/monad talk if you're still confused:
      ua-cam.com/video/2jp8N6Ha7tY/v-deo.html

  • @anler2b
    @anler2b 8 років тому +2

    Dr. Boolean is just amazing, great recommendation, follow him!

  • @aleksd286
    @aleksd286 3 роки тому

    Fun fun functor

  • @johnywhy4679
    @johnywhy4679 4 роки тому

    hi, love your videos, but i won't watch this one or the previous one. As a beginner, listening to wrong information, and then listening to a correction, and then attempting to make sense of it all, will just CONFUSE me. Omg, plz don't tell me i "need" to watch a video that has wrong info in it.
    i hope you will make one Functor video which is all correct, which isn't littered with "the wrong thing i said in the last video was ..." (GAH, I'M PLUGGING MY EARS!), and is designed to be a complete Intro to Functors which stands on it's own.
    Hope you understand!

    • @funfunfunction
      @funfunfunction  4 роки тому +2

      hey Johny. Happy that you are learning programming! I understand that you have a need for cohesion and predictability at this point, where everything seems confusing, that must be very stressful. However, other people, me included, also exist and have needs and limitations. :) I did these two videos a weekend on my spare time about five years ago, and while I am pretty proud of this series it's also the very first videos I ever made on programming, and there were some mistakes made. The mistakes are done and there is nothing to be done about it, and mistakes will always be done - I think correcting it was the right way to go.
      An advice I can give as a programmer is to accept that the world is an inelegant messy place and that it is in fact our job as engineers build systems that give the illusion of elegance to other people. Fundamentally the world is a mess and it's important to remember that a CPU is just a stone that we have managed to somehow flatten an put lightning inside of it.

  • @krzysztofkabat9166
    @krzysztofkabat9166 8 років тому

    To the monad-mobile!

  • @kazaakas
    @kazaakas 5 років тому

    6:58
    According to Brian Lonsdorf (Dr Boolean)'s book, you're making a number one rookie mistake here. Should be:
    then(console.log)
    not:
    then(name => console.log(name))
    wrapping stuff like this is redundant.

    • @funfunfunction
      @funfunfunction  5 років тому

      The two are not equivalent because .log is property on the console object and depending on the implementation of console in the runtime environment, I've had what you're suggesting sometimes break, which is why I've adopted the habit to just always wrap the console.log calls in a wrapper.

  • @Iwillownyouandbehappy
    @Iwillownyouandbehappy 6 років тому

    Fun Fun Functors.

  • @user-rt9ko6jt1u
    @user-rt9ko6jt1u 8 років тому

    magic

  • @herp_derpingson
    @herp_derpingson 6 років тому

    4:20 The `dragon => dragon.name` is the functor. Functors are basically stateful functions which maintain the state themselves.
    stackoverflow.com/questions/356950/c-functors-and-their-uses

  • @Yetipfote
    @Yetipfote 5 років тому

    Idk. Functors are objects that have methods? Sounds a lot like OOP for me not FP where state and operations are separate.

  • @99vishaal
    @99vishaal 3 роки тому

    It's a generic type not an object.

  • @alexminser1482
    @alexminser1482 8 років тому

    That hat though

  • @stevemao1368
    @stevemao1368 7 років тому

    The definition is still not correct. A functor is any type (NOT object) with a map method. It must obey the law that preserves function composition while mapping.
    map() doesn't return anther functor but transforming elements in the same functor.

  • @_the_one_1
    @_the_one_1 6 років тому

    Ok lets skip this video

  • @ctrlaltdebug
    @ctrlaltdebug 8 років тому

    Still wrong. A Functor is an object that acts as a function. web.stanford.edu/class/cs106l/course-reader/Ch13_Functors.pdf

    • @funfunfunction
      @funfunfunction  8 років тому +2

      +CtrlAlt Debug that article is talking about functor in the sense of function object in C++. That is not at all the same thing as what we are talking about here, but unfortunately they are using the same name.