Skip to content

Commit e2b762a

Browse files
committed
Add docs
1 parent f5808cf commit e2b762a

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

docsite/source/maybe.html.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ Maybe(nil).bind(add_two).or(Some(0)) # => Some(0)
105105
Maybe(nil).bind(add_two).or { Some(0) } # => Some(0)
106106
```
107107

108+
There's an alias operator for `or`:
109+
110+
```ruby
111+
extend Dry::Monads[:maybe]
112+
113+
None() | Some(1) | Some(2) # => Some(1)
114+
```
115+
108116
### `and`
109117

110118
Two values can be chained using `.and`:
@@ -144,3 +152,14 @@ None().to_result # => Failure()
144152
None().to_result(:error) # => Failure(:error)
145153
None().to_result { :block_value } # => Failure(:block_value)
146154
```
155+
156+
### `filter`
157+
158+
`Maybe#filter` runs a predicate against the wrapped value. Returns `None` if the result is false:
159+
160+
```ruby
161+
Some(3).filter(&:odd?) # => Some(3)
162+
Some(3).filter(&:even?) # => None
163+
# no block given
164+
Some(3 == 5).filter # => None
165+
```

docsite/source/result.html.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ Success(1).either(-> x { x + 1 }, -> x { x + 2 }) # => 2
157157
Failure(1).either(-> x { x + 1 }, -> x { x + 2 }) # => 3
158158
```
159159

160+
### `alt_map`
161+
162+
`alt_map` channels failure values, it's an `fmap` for `Failure`:
163+
164+
```ruby
165+
Failure("oops").alt_map(&:upcase) # => Failure("OOPS")
166+
```
160167

161168
### Adding constraints to `Failure` values.
162169
You can add type constraints to values passed to `Failure`. This will raise an exception if value doesn't meet the constraints:

docsite/source/try.html.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,31 @@ Use `value!` for unwrapping a `Success` and `exception` for getting error object
7373
### `to_result` and `to_maybe`
7474

7575
`Try`'s `Value` and `Error` can be transformed to `Success` and `Failure` correspondingly by calling `to_result` and to `Some` and `None` by calling `to_maybe`. Keep in mind that by transforming `Try` to `Maybe` you lose the information about an exception so be sure that you've processed the error before doing so.
76+
77+
### `recover`
78+
79+
Recovers from an error:
80+
81+
```ruby
82+
extend Dry::Monads[:try]
83+
84+
Try { 10 / 0 }.recover(ZeroDivisionError) { 1 } # => Try::Value(1)
85+
```
86+
87+
No explicit list of exceptions required, StandardError will be the default:
88+
```ruby
89+
extend Dry::Monads[:try]
90+
Try { Hash.new.fetch(:missing) }.recover { :found } # => Try::Value(:found)
91+
```
92+
93+
Of course, it's a no-op on values:
94+
```ruby
95+
Try { 10 }.recover { 1 } # => Try::Value(10)
96+
```
97+
98+
Multiple exception types are allowed:
99+
```ruby
100+
extend Dry::Monads[:try]
101+
102+
Try { bang! }.recover(KeyError, ArgumentError) { :failsafe }
103+
```

0 commit comments

Comments
 (0)