From 309a33eb94ebe8a90703a5e965c4227a34b91de3 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 9 Aug 2016 20:19:17 +0300 Subject: [PATCH] KEEP #23 Update list of the proposed function signatures --- proposals/stdlib/group-and-fold.md | 45 ++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/proposals/stdlib/group-and-fold.md b/proposals/stdlib/group-and-fold.md index 3bc698fb1..11404bc68 100644 --- a/proposals/stdlib/group-and-fold.md +++ b/proposals/stdlib/group-and-fold.md @@ -33,19 +33,24 @@ interface Grouping { } ``` -It represents a wrapper around a source of iterator with the `keySelector` -function attached to it. +It represents a wrapper around a source of elements which could be iterated +with the `keySelector` function attached to it. After that it becomes possible to provide various useful extensions for `Grouping`. ### Generic aggregation (fold or reduce) -The most generic form of aggreration, that other overloads delegate their implementation to. +The most generic form of aggregation, that other overloads delegate their implementation to. ``` public inline fun Grouping.aggregate( - operation: (key: K, value: R?, element: T, first: Boolean) -> R + operation: (key: K, value: R?, element: T, first: Boolean) -> R ): Map + +public inline fun > Grouping.aggregateTo( + destination: M, + operation: (key: K, accumulator: R?, element: T, first: Boolean) -> R +): M ``` ### Key-parametrized fold @@ -54,9 +59,15 @@ Here the initial value and the operation depend on the key of a group. ``` public inline fun Grouping.fold( - initialValueSelector: (K, T) -> R, - operation: (K, R, T) -> R + initialValueSelector: (key: K, element: T) -> R, + operation: (key: K, accumulator: R, element: T) -> R ): Map + +public inline fun > Grouping.foldTo( + destination: M, + initialValueSelector: (key: K, element: T) -> R, + operation: (key: K, accumulator: R, element: T) -> R +): M ``` ### Simplified fold @@ -65,23 +76,36 @@ The `initialValue` is a constant and the operation do not depend on the group ke ``` public inline fun Grouping.fold( - initialValue: R, - operation: (R, T) -> R + initialValue: R, + operation: (accumulator: R, element: T) -> R ): Map + +public inline fun > Grouping.foldTo( + destination: M, + initialValue: R, + operation: (accumulator: R, element: T) -> R +): M ``` ### Reduce ``` public inline fun Grouping.reduce( - operation: (K, S, T) -> S + operation: (key: K, accumulator: S, element: T) -> S ): Map + +public inline fun > Grouping.reduceTo( + destination: M, + operation: (key: K, accumulator: S, element: T) -> S +): M ``` ### Count ``` public fun Grouping.countEach(): Map + +public fun > Grouping.countEachTo(destination: M): M ``` ### SumBy @@ -90,6 +114,9 @@ public fun Grouping.countEach(): Map public inline fun Grouping.sumEachBy( valueSelector: (T) -> Int ): Map + +public inline fun > Grouping.sumEachByTo(destination: M, valueSelector: (T) -> Int): M + ``` ## Use cases