Skip to content

Commit

Permalink
updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
evpl committed Feb 20, 2024
1 parent 006c0d0 commit c531b38
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions README.md
Expand Up @@ -30,8 +30,10 @@ Java scope functions inspired by Kotlin
* [Unchecked functions](#unchecked-functions)
* [Examples](#examples)
* [Collection initialization](#collection-initialization)
* [Argument in a method chain](#argument-in-a-method-chain)
* [Nth Fibonacci number](#nth-fibonacci-number)
* [Method argument processing](#method-argument-processing)
* [Safe resources](#safe-resources)

## Motivation

Expand Down Expand Up @@ -217,11 +219,11 @@ The `Opt` monad is similar in meaning to Java `Optional`, but allows the null va
`Opt` monad contains some `Optional` methods and scope functions methods:

```
String result = Opt.of("value").takeIf(it -> it.length() > 10).orElse("");
String result = Opt.of(value).takeIf(it -> it.length() > 10).orElse("");
String result = Opt.of("value").takeIf(it -> it.length() > 10).orElseGet(() -> "");
String result = Opt.of(value).takeNonNull().orElseGet(() -> "");
String result = Opt.of("value").takeIf(it -> it.length() > 10).orElseThrow(() -> new IllegalArgumentException());
String result = Opt.of(value).takeIf(it -> it.length() > 10).orElseThrow(() -> new IllegalArgumentException());
```

### Unchecked functions
Expand Down Expand Up @@ -250,6 +252,20 @@ List<String> list = let(new ArrayList<>(), it -> {
});
```

#### Argument in a method chain

```
new MyBuilder()
.setFirst("first")
.setSecond("second")
.setThird(let(() -> {
//...
return "third";
}))
.setFourth("fourth")
.build()
```

#### Nth Fibonacci number

```
Expand All @@ -271,3 +287,15 @@ public static String checkNonNullNonEmptyStr(String value) {
.get();
}
```

#### Safe resources

```
class MyResource implements AutoCloseable {
//...
}
withResource(new MyResource(), it -> {
//...
});
```

0 comments on commit c531b38

Please sign in to comment.