New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change `Maybe.andThen` docs to use `List.head` over `String.toInt` #328

Merged
merged 1 commit into from Aug 30, 2015

Conversation

Projects
None yet
2 participants
@rtfeldman
Member

rtfeldman commented Aug 4, 2015

The current docs for Maybe.andThen use the example of String.toInt, but that function now returns a Result rather than a Maybe.

This revises the docs to use List.head for the example instead, as it does return a Maybe:

{-| Chain together many computations that may fail. It is helpful to see its
definition:

    andThen : Maybe a -> (a -> Maybe b) -> Maybe b
    andThen maybe callback =
        case maybe of
            Just value ->
                callback value

            Nothing ->
                Nothing

This means we only continue with the callback if things are going well. For
example, say you need to use (`head : List Int -> Maybe Int`) to get the
first month from a `List` and then make sure it is between 1 and 12:

    toValidMonth : Int -> Maybe Int
    toValidMonth month =
        if month >= 1 && month <= 12 then
            Just month
        else
            Nothing

    getFirstMonth : List Int -> Maybe Int
    getFirstMonth months =
        head months `andThen` toValidMonth

If `head` fails and results in `Nothing` (because the `List` was empty`),
this entire chain of operations will short-circuit and result in `Nothing`.
If `toValidMonth` results in `Nothing`, again the chain of computations
will result in `Nothing`.

This also updates the code examples to use the current style recommendations.

evancz pushed a commit that referenced this pull request Aug 30, 2015

Merge pull request #328 from rtfeldman/revise-maybe-docs
Change `Maybe.andThen` docs to use `List.head` over `String.toInt`

@evancz evancz merged commit 4d1d255 into elm:master Aug 30, 2015

1 check failed

continuous-integration/travis-ci/pr The Travis CI build could not complete due to an error
Details
@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Aug 30, 2015

Member

Thanks! :D

Member

evancz commented Aug 30, 2015

Thanks! :D

@rtfeldman rtfeldman deleted the rtfeldman:revise-maybe-docs branch Aug 30, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment