Skip to content
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

Updated List.filterMap documentation with more realistic examples #866

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

charukiewicz
Copy link

@charukiewicz charukiewicz commented May 16, 2017

I made a change to the filterMap documentation in the List module to provide more realistic examples of its use.

The old text:

Apply a function that may succeed to all values in the list, but only keep the successes.

onlyTeens =
 filterMap isTeen [3, 15, 12, 18, 24] == [15, 18]

isTeen : Int -> Maybe Int
isTeen n =
 if 13 <= n && n <= 19 then
   Just n

 else
   Nothing

To someone who may not have a good understanding of what Maybe is used for, I think this raises the question: "Why does my isTeen function even return a Maybe Int to begin with? Why not return Bool and use filter?" In my opinion this is a very contrived example.

As an alternative, my suggested example text is an application of the head and identity functions, where one can build a good intuition of why Maybe values are produced. My suggested text:

Apply a function that may succeed to all values in the list, but only keep
the successes.

    filterMap head [[1,2,3],[],[4,5,6],[],[]] == [1,4]

filterMap can be combined with identity to clean a list of Maybe values:

    filterMap identity [Just 1, Nothing, Nothing, Just 2, Just 3] == [1,2,3]
@process-bot
Copy link

Thanks for the pull request! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

@evancz evancz added the docs label Jul 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 participants