Hildebrandt Tobias
11/05/2024, 11:07 AMmap
and fold
where I carry over an accumulated value from iteration to iteration, but transform the list?
So that a transformed iterable is returned and not the accumulated value?Youssef Shoaib [MOD]
11/05/2024, 11:09 AMrunningFold
I think it's called which gives you all the accumulated values.
But likely what you want is just a mutable variable that you write the values intoAron Zwaan
11/05/2024, 11:12 AMval (_, mappedList) = list.fold(Pair(initialAcc(), emptyList())) { (acc, mappedList), value ->
val newAcc = computeAcc(acc, mappedList, value)
val newMappedList = mappedList + map(acc, value)
Pair(newAcc, newMappedList)
}
Mario Niebes
11/05/2024, 11:13 AMHildebrandt Tobias
11/05/2024, 11:15 AMfold
and scan
return the accumulated value(s) not the transformed underlying iterable.
Aron's snipped looks promising, I'll check it out, thank you.ephemient
11/05/2024, 11:20 AMephemient
11/05/2024, 11:21 AMephemient
11/05/2024, 11:21 AMephemient
11/05/2024, 11:22 AMYoussef Shoaib [MOD]
11/05/2024, 11:25 AMinline fun <T, R, S> Iterable<T>.mapWithState(initial: S, transform: (S, T) -> Pair<S, R>): List<R>
but that can be implemented simply as:
{
var state = initial
return map { element ->
val (newState, transformed) = transform(state, element)
state = newState
transformed
}
}
So you should just use mutable state insteadHildebrandt Tobias
11/05/2024, 11:39 AMpublic inline fun <T, R> Iterable<T>.foldMap(initial: Pair<R, Iterable<T>>, operation: (acc: R, T) -> Pair<R, T>): Iterable<T> {
var (accumulatorValue, accumulatorIterable) = initial
for (element in this) {
val (value, iterable) = operation(accumulatorValue, element)
accumulatorValue = value
accumulatorIterable += iterable
}
return accumulatorIterable
}
@ephemient I need the accumulated value to transform the element in the list where I currently am while stepping through. The alternative would have been to have a map
inside of a for loop. I just wanted to see/ask if there is something out of the boxYoussef Shoaib [MOD]
11/05/2024, 11:42 AMpublic inline fun <T, R> Iterable<T>.foldMap(initial: Pair<R, Iterable<T>>, operation: (acc: R, T) -> Pair<R, T>): List<T> = buildList(initial.second.size) {
var (accumulatorValue, accumulatorIterable) = initial
addAll(accumulatorIterable)
for (element in this@foldMap) {
val (value, iterable) = operation(accumulatorValue, element)
accumulatorValue = value
add(iterable)
}
}
Hildebrandt Tobias
11/05/2024, 11:43 AMYoussef Shoaib [MOD]
11/05/2024, 11:45 AMpublic inline fun <T, R> Iterable<T>.foldMap(initial: R, operation: (acc: R, T) -> Pair<R, T>): List<T> = buildList(size) {
var accumulatorValue = initial
for (element in this@foldMap) {
val (value, iterable) = operation(accumulatorValue, element)
accumulatorValue = value
add(iterable)
}
}
// Usage
myPrefix + iterable.foldMap(...)
Youssef Shoaib [MOD]
11/05/2024, 11:46 AMpublic inline fun <T, R> Iterable<T>.foldMap(initial: R, operation: (acc: R, T) -> Pair<R, T>): List<T> {
var accumulatorValue = initial
return map { element ->
val (value, iterable) = operation(accumulatorValue, element)
accumulatorValue = value
iterable
}
}
which becomes simply what I presented aboveHildebrandt Tobias
11/05/2024, 11:46 AMIterable
doesn't provide a size
for it.Youssef Shoaib [MOD]
11/05/2024, 11:46 AMpairs
Youssef Shoaib [MOD]
11/05/2024, 11:47 AMsize
thoughYoussef Shoaib [MOD]
11/05/2024, 11:48 AMfoldMap
has a completely different meaning elsewhere. It's used as a map
then a fold
Hildebrandt Tobias
11/05/2024, 11:48 AMHildebrandt Tobias
11/05/2024, 12:32 PMwhich becomes simply what I presented aboveAnd like this the circle closes (German proverb)