Library Design: Naming Conventions
Part 5: Streaming
Published on 2022-06-08. Last updated on 2025-12-14
Projections
| Function Name | Code Example | Explanation |
|---|---|---|
map(<fun>) |
List(1, 2, 3).map(_ + 1) --> List(2, 3, 4) | Returns a stream in which fun is applied to each element. |
|
List(1, 2).mapMany(x -> List(x, x)) --> List(1, 1, 2, 2) List(1, 2).mapMany(x -> Some(x)) --> List(1, 2) List(1, 2).mapMany(x -> None) --> List() | Returns a stream in which fun is applied to each element, producing a sequence of elements that is subsequently flattened. |
Filters
| Function Name | Code Example | Explanation |
|---|---|---|
first |
List(1, 2, 3).first --> Option(1) | Returns the first element of the stream. |
last |
List(1, 2, 3).last --> Option(3) | Returns the last element of the stream. |
|
List(1, 2, 3, 4).retainFirst(2) --> List(1, 2) | Returns a stream that produces the first num elements of the input stream. |
|
List(1, 2, 3, 1).retain(_ < 2) --> List(1, 2, 1) |
Returns a stream that produces only elements for which pred evaluates to true.Alternatives considered:
|
|
List("a", "b", "c").retainIndex(_ % 2 == 0) --> List("a", "c") | Returns a stream that produces only elements for which pred evaluates to true. |
|
List(1, 2, 3, 1).retainWhile(_ < 3) --> List(1, 2) | Returns a stream that produces elements of the input stream until the pred evaluates to false. |
|
List(4, 3, 2, 4).retainUntil(_ < 3) --> List(4, 3) |
Returns a stream that produces elements of the input stream until the pred evaluates to true.
|
|
List(1, 2, 3, 4).rejectFirst(1) --> List(2, 3, 4) | Returns a stream without the first num elements of the input stream. |
|
List(1, 2, 3, 1).reject(_ < 2) --> List(3) |
Returns a stream that produces only elements for which pred evaluates to false.Alternatives considered:
|
|
List("a", "b", "c").rejectIndex(_ % 2 == 0) --> List("b") | Returns a stream that produces only elements for which pred evaluates to false. |
|
List(2, 3, 4, 1).rejectWhile(_ < 2) --> List(3, 4, 1) | Returns a stream that skips elements of the input stream until pred evaluates to false. |
|
List(3, 2, 1, 4).rejectUntil(_ < 2) --> List(1, 4) |
Returns a stream that skips elements of the input stream until pred evaluates to true.Alternatives considered:
|
distinct |
List(1, 2, 3, 1).distinct --> List(1, 2, 3) | Returns a stream that produces only the first occurrence of elements occurring multiple times. |
Folds
| Function Name | Code Example | Explanation |
|---|---|---|
fold(<num>, <start>) |
||
reduce(<fun>) |
||
combine[Monoid] |
||
sum[Numeric] |
List(1.0, 2.0, 3.0, 4.0).sum --> 10.0 | Computes the sum of the list of numbers. |
product[Numeric] |
List(1.0, 2.0, 3.0, 4.0).product --> 24.0 | Computes the product of the list of numbers. |
average[Numeric] |
List(1.0, 2.0, 3.0, 4.0).average --> 2.5 | Computes the average of the list of numbers. |
|
Returns true if pred returns true for all elements. |
|
|
Returns true if pred returns true for any element. |
|
|
Returns true if pred returns false for all elements. |
Injects
| Function Name | Code Example | Explanation |
|---|---|---|
joinInner[Record] |
||
joinLeft[Record] |
||
joinRight[Record] |
||
joinFull[Record] |
||
groupBy(<fun>) |
||
partitionBy(<fun>) |
Fan-Ins
| Function Name | Code Example | Explanation |
|---|---|---|
concat |
Returns a stream that produces the values from the first stream and then the values of the second stream. | |
interleave |
Returns a stream that produces a value, alternating between the first and second stream.
|
|
zip |
Returns a stream that produces a tuple value, where the first element is from the first stream and the second element is from the second stream.
|
|
zipWithIndex |
Returns a stream that produces a tuple value, where the first element is from the stream and the second element is the index at which the value was produced. |