ionspin / kotlin-multiplatform-bignum

A Kotlin multiplatform library for arbitrary precision arithmetics
Apache License 2.0
345 stars 41 forks source link

Provides kotlin collection operators like sumOf (convenience) #249

Open glureau opened 1 year ago

glureau commented 1 year ago

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 }