Maybe.andThen order of arguments

Hello.
Elm newbie here.

The definition of Maybe.andThen is as follows:

andThen : (a -> Maybe b) -> Maybe a -> Maybe b

which is useful when we want to chain together many computations that may fail.

I would have expected for this to be defined with the inputs inverted, like so:

andThen : Maybe a -> (a -> Maybe b) -> Maybe b

When thinking about a scenario where computations are being chained I tend to want to read it like this :

given (a computation that returns a) Maybe a value then *do this other computation"

This seems to more natural to me :sweat_smile:

andThen (List.head [[1,2]]) List.head

rather than

andThen List.head (List.head [[1,2]])

Is the underlying choice related to the fact that piping with |> works out really nice

List.head [[[1,2,3]]]
    |> Maybe.andThen List.head 
    |> Maybe.andThen List.head

and is actually impossible to do otherwise?

What am I missing or not understanding about the overall approach?

Thank you.

Sergio

Yes. This is exactly the reason.

In an earlier version of Elm the arguments were reversed like that as it was common to use andThen as an inflix operator (like Haskell’s >=)

You could write something like:
(List.head [1]) andThen (\x -> x + 1)

The arguments were reversed when custom infix operators was removed in Elm 0.18.

4 Likes

Thank your for clarifying this.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.