Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

group-and-fold implementation #1004

Closed
wants to merge 5 commits into from
Closed

group-and-fold implementation #1004

wants to merge 5 commits into from

Conversation

ilya-g
Copy link
Member

@ilya-g ilya-g commented Dec 27, 2016

An implementation of Kotlin/KEEP#23 proposal

@ilya-g ilya-g self-assigned this Dec 27, 2016
@ilya-g
Copy link
Member Author

ilya-g commented Dec 28, 2016

TODO

  • add SinceKotlin
  • add headers for platform specialized functions.

Docs for group-and-fold operations

Fix implementation for countEach and sumEachBy

countEach and sumEachBy implementations for JS

Rename keySelector to keyOf

Generate additional sources for groupingBy + headers

Rename countEach and sumEachBy to eachCount and eachSumOf.
@ilya-g ilya-g force-pushed the rr/stdlib/groupingBy branch from 80b5a39 to 16bff4c Compare December 28, 2016 15:06
@SinceKotlin("1.1")
public interface Grouping<T, out K> {
/** Returns an [Iterator] which iterates through the elements of the source. */
fun elementIterator(): Iterator<T>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@{18f15b45-4413-42a3-82e7-d057983d1e65,Ilya Gorbunov} sourceIterator() or just source() ? Need to be documented: whether it creates iterator every time or keep the same instance. If the behaviour is undefined we have to notice it here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe elementIterator being a fun conveys the intent well. If that was the same instance every time, it would be a property.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was called source in the beginning, that I renamed it to iterator and then to elementIterator

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some consideration, sourceIterator seems to look better.

public inline fun <T, K, R> Grouping<T, K>.aggregate(
operation: (key: K, value: R?, element: T, first: Boolean) -> R
): Map<K, R> {
val result = mutableMapOf<K, R>()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we use aggregateTo instead?

@SinceKotlin("1.1")
public inline fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.aggregateTo(
destination: M,
operation: (key: K, accumulator: R?, element: T, first: Boolean) -> R
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value vs accumulator, v vs acc

fold(
initialValueSelector = { k, e -> kotlin.jvm.internal.Ref.IntRef() },
operation = { k, acc, e -> acc.apply { element += valueSelector(e) } })
.mapValues { it.value.element }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as we already use internal API hack we could do even more: we can create replace IntRef to Int inplace and then do unchecked cast.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't call it internal API hack, because that API is used publicly by the compiler in the generated bytecode.
mapInPlace is possible as JVM-only optimization, otherwise it would make the code non-portable to platforms, where generics are reified.

* using the specified [keySelector] function to extract a key from each character.
*/
@SinceKotlin("1.1")
public header inline fun <K> CharSequence.groupingBy(crossinline keySelector: (Char) -> K): Grouping<Char, K>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why there is specialization here? the implementations look the same

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's how the code is generated now.

@ilya-g
Copy link
Member Author

ilya-g commented Jan 2, 2017

libraries/stdlib/src/kotlin/collections/Grouping.kt:35
I'm not sure how could it be possible without wrapping each value. And wrapping each value is very ineffective in terms of number of allocations.

@yole
Copy link
Contributor

yole commented Jan 2, 2017

libraries/stdlib/src/kotlin/collections/Grouping.kt:35
True. I don't have any better suggestion to improve this, so let's keep it as is.

@yole
Copy link
Contributor

yole commented Jan 2, 2017

libraries/stdlib/src/kotlin/collections/Grouping.kt:47
You're using inconsistent wording - "aggregates with" or "accumulates with" - and neither of those is a standard usage of the corresponding verbs with the preposition. I think you need to provide a more detailed explanation of what happens here - something like applies [operation] to the elements of each group sequentially, passing the previously accumulated value and the current element as arguments, and stores the results in the given [destination] map". This applies to other functions as well.

* they group elements by their keys and then fold each group with some aggregating operation.
*
* It is created by attaching `keySelector: (T) -> K` function to a source of elements.
* To get an instance of [Grouping] use one of `groupingBy` extension functions:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 32
"a group" -> "the group" here and below

@yole
Copy link
Contributor

yole commented Jan 2, 2017

libraries/stdlib/src/kotlin/collections/Grouping.kt:213
count of element_s_

@yole
Copy link
Contributor

yole commented Jan 2, 2017

libraries/stdlib/src/kotlin/collections/Grouping.kt:219
Why the space after the open parenthesis?

* It is created by attaching `keySelector: (T) -> K` function to a source of elements.
* To get an instance of [Grouping] use one of `groupingBy` extension functions:
* - [Iterable.groupingBy]
* - [Sequence.groupingBy]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 61
Any chance for some @sample's?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aggregate and aggregateTo are not intended to be used much in user code, they are here more to implement other folding functions when fold or reduce do not fit for some reason.
Other functions (fold, reduce, eachCount, etc) will get samples eventually.

@yole
Copy link
Contributor

yole commented Jan 2, 2017

libraries/stdlib/src/kotlin/collections/Grouping.kt:240
Why is this function called eachSumOf and not eachSumBy, which would be consistent with Iterable.sumBy?

@ilya-g
Copy link
Member Author

ilya-g commented Jan 3, 2017

Why is this function called eachSumOf and not eachSumBy, which would be consistent with Iterable.sumBy?

We're going to rename sumBy and sumByDouble to sumOf for Iterables, however it's blocked by KT-11265. Maybe we should not introduce eachSumOf until then.

@ilya-g ilya-g closed this Jan 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants