-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Documentation for arrow-kt/arrow#3280
- Loading branch information
Showing
7 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
sidebar_position: 2 | ||
description: Better aggregation over sequences | ||
--- | ||
|
||
# Collectors | ||
|
||
Collectors help build complex computations over sequences of values, | ||
guaranteeing that those values are consumed only once. | ||
|
||
Take for example the computation of the average of a list. You can | ||
certainly write a simple version using the built-in functions, | ||
|
||
<!--- INCLUDE | ||
val list = listOf(1, 2, 3) | ||
--> | ||
|
||
```kotlin | ||
val average = list.sum() / list.size | ||
``` | ||
<!--- KNIT example-collectors-01.kt --> | ||
|
||
Note however that this implementation traverses the list _twice_, | ||
one per operation over the list. This may not be a problem for small | ||
lists but could become more problematic with longer collections. | ||
Some data structures, like `Sequence` or `Flow`, impose an | ||
even larger footprint, as their elements are computed every time | ||
you need a new one. | ||
|
||
Collectors separate the description of the aggregation you want | ||
to perform from the actual collection. To create a new collector | ||
you use one of the built-in ones, and combine them using `zip`. | ||
|
||
```kotlin | ||
import arrow.collectors.Collectors | ||
import arrow.collectors.collect | ||
import arrow.collectors.zip | ||
|
||
fun divide(x: Int, y: Int): Double = x.toDouble() / y.toDouble() | ||
|
||
val averageCollector = zip(Collectors.sum, Collectors.length, ::divide) | ||
``` | ||
|
||
<!--- INCLUDE | ||
val list = listOf(1, 2, 3) | ||
--> | ||
|
||
You then may apply the collector to the sequence or collection you want. | ||
|
||
```kotlin | ||
val average = list.collect(averageCollector) | ||
``` | ||
<!--- KNIT example-collectors-02.kt --> | ||
|
||
:::note Influences | ||
|
||
The API implemented in `arrow-collectors` is heavily influenced by | ||
Java's [`Collector`](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collector.html) | ||
and Haskell's [`foldl` library](https://hackage.haskell.org/package/foldl/docs/Control-Foldl.html). | ||
|
||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 3 | ||
sidebar_position: 4 | ||
description: Avoiding duplicate work for pure functions | ||
--- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 2 | ||
sidebar_position: 3 | ||
description: Making functions stack-safe and efficient | ||
--- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 4 | ||
sidebar_position: 5 | ||
description: Composition, partial application, and currying | ||
--- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// This file was automatically generated from collectors.md by Knit tool. Do not edit. | ||
package arrow.website.examples.exampleCollectors01 | ||
|
||
val list = listOf(1, 2, 3) | ||
|
||
val average = list.sum() / list.size |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// This file was automatically generated from collectors.md by Knit tool. Do not edit. | ||
package arrow.website.examples.exampleCollectors02 | ||
|
||
import arrow.collectors.Collectors | ||
import arrow.collectors.collect | ||
import arrow.collectors.zip | ||
|
||
fun divide(x: Int, y: Int): Double = x.toDouble() / y.toDouble() | ||
|
||
val averageCollector = zip(Collectors.sum, Collectors.length, ::divide) | ||
val list = listOf(1, 2, 3) | ||
|
||
val average = list.collect(averageCollector) |