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

Add documentation for List#collect #159

Merged
merged 1 commit into from
Dec 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions docsite/source/list.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,42 @@ M::List[1, 2].bind(-> x { [x, x + 1] }) # => List[1, 2, 2, 3]
M::List[1, nil].bind { |x| [x + 1] } # => error
```

### `collect`

Works differently than `Enumerable#collect`: the block must return `Maybe` types, `Some` values are retained and `None` is discarded. As in other monads if no block given the first argument will be treated as callable and used instead.

```ruby
require 'dry/monads/list'

M = Dry::Monads

n = 20
M::List[10, 5, 0].collect do |divisor|
if divisor.zero?
M::None()
else
M::Some(n / divisor)
end
end
# => List[2, 4]

M::List[M::Some(1), M::None(), M::Some(2), M::None(), M::Some(3)].collect # => List[1, 2, 3]

leap_year = proc do |year|
if year % 400 == 0
M::Some(year)
elsif year % 100 == 0
M::None()
elsif year % 4 == 0
M::Some(year)
else
M::None()
end
end

M::List[2020, 2021, 2022, 2023, 2024].collect(leap_year) # => List[2020, 2024]
```

### `fmap`

Maps a block over the list. Acts as `Array#map`. As in other monads, if no block given the first argument will be treated as callable and used instead.
Expand Down