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

Provides kotlin collection operators like sumOf (convenience) #249

Open
glureau opened this issue Sep 28, 2022 · 0 comments
Open

Provides kotlin collection operators like sumOf (convenience) #249

glureau opened this issue Sep 28, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@glureau
Copy link

glureau commented Sep 28, 2022

Is your feature request related to a problem? Please describe.
Kotlin collections provides helper method like sumOf { }, but some methods are defined with the explicit types (there is a Iterable<T>.sumOf(selector: (T) -> Long): Long and a Iterable<T>.sumOf(selector: (T) -> Int): Int for example).

data class Foo(val data: Double)
val fooList = listOf(Foo(0.1), Foo(0.2))
val sum = fooList.sumOf { it.data }

It could be nice to provide those methods for BigInteger and BigDecimal.

Describe the solution you'd like

data class Bar(val data: BigDecimal)
val barList = listOf(Bar(BigDecimal.fromDouble(0.1)), Bar(BigDecimal.fromDouble(0.2)))
val sum = barList.sumOf { it.data }

Describe alternatives you've considered

It's still possible to use fold to compute a sum (just a bit less convenient).

val sum2 = barList.fold(BigDecimal.ZERO) { acc, it -> acc + it.data }
@ionspin ionspin added the enhancement New feature or request label Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants