Ah, Joey beat me to it again :-) On Mon, Jul 25, 2016 at 12:14 PM, Joey Eremondi <[email protected]> wrote:
> Being fully parallelizable is a nice side-effect for the List version (and > functional programming in general), bit that's not what actually makes a > map a map. > > First, realise that List, (Result a), Maybe, etc. are all "type > constructors". You give them a type argument, then they are a type. List is > a constructor, List Int is a type. Maybe is a type constructor, Maybe Bool > is a type. > > All of these types are "wrappers" of a sort, they're taking a type and > doing something special with it: collecting them in groups, wrapping them > in a possible error, etc. > > Now, what is a "map" operation? It's an operation that lets you apply a > function that's meant for a type, to something wrapped around that type. > > Specifically, they all have this form: if Foo is a type constructor, > they're of the form > (a -> b) -> Foo a -> Foo b > where a and b are type variables. > > You'll notice that we have operations like this for Dict, List, Array, > Maybe, Result, and many more. > > They let us re-use operations that we already have. You have a Maybe Int, > and you want to double the int stored, if it's not null? Use "Maybe.map (\x > -> x * 2)". This will double the Int, unless it's Nothing, in which case it > gives you nothing back. > > You want to append a period to each string in your Dict? Then use > 'Dict.map (\x -> x ++ ".") '. > > The map2, map3 etc. versions are just nice ways of extending this to > things that take multiple arguments. If we have a Maybe Int and a Maybe > Int, and we want to add them and get a result that's null if either one of > them is, then map2 lets us do that. > > They're not really a Fold, which is more about iterating over a > collection, than about applying a function with multiple arguments. In the > same way it doesn't make sense to loop over a single, possibly null value > in JavaScript, it doesn't make sense to fold over a Result in Elm. > > I don't know where the name comes from, but Elm didn't make it up. Maybe > Math, maybe Category Theory, maybe Lisp. In Haskell, these are called > "fmap" (because they used up "map" for the List version before anybody > really used the general version). The pattern is called a Functor, and a > more advanced version is called an Applicative Functor. **But you don't > need to know about this to use them in Elm!** > > I hope this makes things a little more clear. Feel free to ask questions, > and I'll hopefully be able to clarify anything that's unclear. > > > On Mon, Jul 25, 2016 at 11:56 AM, Duane Johnson <[email protected]> > wrote: > >> FWIW, also as a new Elm user, I find the Maybe.map and Result.map >> functions to be confusing names. It may just be my use of lodash and Ruby >> arrays that are sending my brain off track, but I find myself resisting >> this naming a lot (and still don't naturally reach for it on these modules). >> >> On Mon, Jul 25, 2016 at 12:47 PM, Brad Grzesiak <[email protected]> >> wrote: >> >>> OK. For those of you following along, I hope the third time's a charm! >>> (I already inadvisedly submitted this to the Issue tracker and elm-dev) >>> >>> >>> It seems to me that Result.map2, Result.map3, etc should actually be >>> called Result.foldl2, Result.foldl3, etc. >>> >>> >>> As I understand it, map should be fully parallelizable, applying a func >>> to items in a list without regard to the other items in the list. On the >>> other hand, foldl reduces the dimensionality of the input values, often >>> from 2 to 1. >>> >>> >>> Going further, perhaps there should be a Result.foldl with the type >>> signature: foldl : (a -> value -> value) -> value -> List (Result x a) >>> -> Result x value >>> >>> >>> *NB:* Forgive my inexperience in the FP world... this issue is coming >>> from my experience as an OO programmer. For example, I nearly submitted >>> this issue with reduce rather than foldl. :P >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Elm Discuss" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Elm Discuss" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> For more options, visit https://groups.google.com/d/optout. >> > > -- > You received this message because you are subscribed to the Google Groups > "Elm Discuss" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Elm Discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
