Skip to content

Commit d2a0c31

Browse files
committed
Update README with more documentation
1 parent 7cfe718 commit d2a0c31

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ operators, then we use flat map.
170170
```ts
171171
// stream of two arrays
172172
const individualItems = Stream.of([1, 2, 3], [4, 5, 6])
173-
.flatMap(array => Stream.ofArray(array))
174-
.count(); // will receive 6 items
173+
.flatMap((array) => Stream.ofArray(array))
174+
.count(); // will receive 6 items
175175
```
176176

177177
#### Reducing
@@ -257,7 +257,7 @@ expect(Stream.of(1, 2, 3).allMatch((item) => item < 100)).toBeTruthy();
257257
### Terminal Operations
258258

259259
> Calling a terminal operation causes the iterators to run. These cannot be run more than
260-
> once, so the stream is used up. Do not reuse a stream after a terminal operation is used.
260+
> once, as the stream is used up. Do not reuse a stream after a terminal operation is used.
261261
262262
#### Embedded Collectors
263263

@@ -284,6 +284,30 @@ expect(
284284

285285
We need to provide a `keyMapper` and a `valueMapper` function. In this example, the utility function `identity` is called to map the object to itself as the value in the map.
286286

287+
To get the maximum item in a `Stream` we use `max` (and for the minimum we use `min`):
288+
289+
```ts
290+
const maxString = Stream.of('a', 'b', 'c)
291+
.max(compareString); // an optional, containing 'c'
292+
```
293+
294+
The result is an `Optional` which is empty when the `Stream` is empty.
295+
296+
We need to provide a comparator here, for which the utility function `compareString` can help us. If we're
297+
using a `NumberStream` the comparator defaults to `compareNumber` and, thus, is optional.
298+
299+
We can build a comparator of a property using `comparingBy`:
300+
301+
```ts
302+
const maxElement = Stream.of({ name: 'a', val: 1 }, { name: 'b', val: 2 }).max(
303+
comparingBy((element) => element.name, compareString)
304+
);
305+
```
306+
307+
`comparingBy` lets us compose a comparator from a function to select the property of the item, and then another
308+
comparator. Or we can build our own comparator from scratch, following the same rules as a [sorting `compareFn`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description) in
309+
JavaScript.
310+
287311
#### Collectors
288312
289313
The `collect` function uses a `Collector` to produce a final value from the contents of the Stream. This is

0 commit comments

Comments
 (0)