igor.wojda
11/08/2018, 5:47 PMCharSequence.reduce
function?dalexander
11/08/2018, 6:15 PMval aCount = "the rain in spain falls mainly in the plain".fold(0, {sum, cur -> sum + if(cur == 'a') { 1 } else { 0 }})
hudsonb
11/08/2018, 6:20 PMChar
. reduce
in Swift for example allows you to provide the initial value for the accumulator which is far more flexible/useful. I can't think of a use case for reduce
, as is, that couldn't be accomplished better using other methods.dalexander
11/08/2018, 6:21 PMr4zzz4k
11/08/2018, 7:16 PM.fold
, which allows you to use any type for accumulator and pass it's initial value.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/fold.html
(Same goes for collections and sequences, by the way)hudsonb
11/08/2018, 7:24 PMreduce
may just be included for consistency as Derek suggested.r4zzz4k
11/08/2018, 7:24 PMigor.wojda
11/08/2018, 9:48 PMreduce
may be not that useful for Char-sequence/String classes.
Thanks guys for the fold tip and thanks@r4zzz4k for info that fold
have different accumulator type then reduce
- this is what I neededkarelpeeters
11/08/2018, 10:18 PMreduce
at all yet, lots of fold
though.karelpeeters
11/08/2018, 10:19 PMreduce
on charsequences doesn't make less sense than on lists though, unless I'm missing something.igor.wojda
11/08/2018, 11:30 PMString/Charsequence
- accumulator/current is Char
List<Int>
- accumulator/current is Int
List<String>
- accumulator/current is String
So having eg. List<Int>
you could sum up the items or sum up only odd items. I guess in this case we could use reduce and fold, however with String/Charsequence
reduce is not an option (because accumulator/current is Char
) while fold accumulator is String
and current is Char
karelpeeters
11/08/2018, 11:32 PMList<Char>.reduce
is exactly the same as CharSequence.reduce
, right?r4zzz4k
11/09/2018, 12:38 AMf_reduce
and f_reduceSuper
are of interest)