elm-community / list-extra

Convenience functions for working with List.
http://package.elm-lang.org/packages/elm-community/list-extra/latest
MIT License
135 stars 59 forks source link

andMap doesn't behave like an applicative apply #59

Closed slimey closed 7 years ago

slimey commented 7 years ago

In most of the -extra packages where andMap appears (eg elm-community/elm-json-extra, elm-community/maybe-extra, elm-community/result-extra), it's documented to be applicative apply. In which case one would expect map f x and (pure f) |> andMap x to be the same (the "fmap law").

This isn't currently the case with list-extra (assuming pure x is [x] as usual):

> x = [1,2,3,4,5]
> d x = 2 * x
> List.map d x
[2,4,6,8,10] : List number
> [d] |> List.andMap x
[2] : List number

This seems counter-intuitive - is it intentional?

jvoigtlaender commented 7 years ago

I guess your assumption is wrong. There are different ways of defining applicative instances on lists. In particular, for what is called the "ziplist applicative", pure x is not [x], but is instead the infinite list [x,x,x,x,x,x,...].

slimey commented 7 years ago

Ah yes, makes sense - thanks. I also see that this is a better fit for List.map2 etc.