File tree 3 files changed +278
-541
lines changed
main/java/com/kirekov/juu/monad
test/java/com/kirekov/juu/monad
3 files changed +278
-541
lines changed Original file line number Diff line number Diff line change @@ -102,7 +102,7 @@ This library has two monads: [`Try`](#try) and [`Lazy`](#lazy).
102
102
###### Try
103
103
104
104
` Try ` allows you to work with methods that may throw an exception
105
- in the same way as ` Optional ` . For instance, suppose we have such code:
105
+ in the same way as ` Optional ` in a ** lazy ** way . For instance, suppose we have such code:
106
106
107
107
``` java
108
108
int num;
@@ -127,7 +127,7 @@ JUU allows to rewrite this snippet as two equations:
127
127
128
128
``` java
129
129
int num = Try . of(() - > Integer . parseInt(getStringValue()))
130
- .orElse( getDefaultIntValue());
130
+ .orElseGet(() - > getDefaultIntValue());
131
131
return Try . of(() - > executeRpc(num))
132
132
.orElse(SOME_DEFAULT_VALUE );
133
133
```
@@ -190,6 +190,30 @@ The class only catches `Exception` type.
190
190
It means that all `Throwable` instances are skipped.
191
191
The motivation is that `Error` extends from `Throwable ` but these exceptions should not be caught manually.
192
192
193
+ The fact that `Try ` monad acts *lazily* means
194
+ that you build a pipeline of execution that triggers on any *terminal* operation.
195
+
196
+ ```java
197
+ Try<Integer > t = Try .of(() -> {
198
+ println ("First step ");
199
+ return 1 ;
200
+ }). map(val - > {
201
+ println(" Second step" );
202
+ return val + 1 ;
203
+ }). filter(val - > {
204
+ println(" Third step" );
205
+ return val > 0 ;
206
+ });
207
+ // nothing prints here
208
+
209
+ assert 2 == t. orElseThrow();
210
+ // First step
211
+ // Second step
212
+ // Third step
213
+ ```
214
+
215
+ All terminal operations are listed in the [ javadoc] ( ./src/main/java/com/kirekov/juu/monad/Try.java ) .
216
+
193
217
###### Lazy
194
218
195
219
The name of the monad defines its purpose.
You can’t perform that action at this time.
0 commit comments