Alan B
07/05/2022, 1:56 PMmap while supplying the map-like operation with a list of functions (or vararg) to apply to each element, rather than a single transformation? basically collection.mapAll(transformers: List<(T) -> R>)sequence.mapAll(transformers: List<(T) -> T>) I want to supply a list of transformers so I don’t have to do something like:
sequence.map(::first)
.map(::second)
.map(::third)
would be:
sequence.map(tranformers)Joffrey
07/05/2022, 1:59 PMList<(T) -> R> cannot work in general, though, because you would need each function's output type to be the same (or a subtype) of the next function's input type.wbertan
07/05/2022, 1:59 PMwbertan
07/05/2022, 2:01 PMSequence not handle this case without having to make multiple iterations? 🤔denis
07/05/2022, 2:02 PMAlan B
07/05/2022, 2:02 PMAlan B
07/05/2022, 2:02 PMAlan B
07/05/2022, 2:03 PMdenis
07/05/2022, 2:06 PMAlan B
07/05/2022, 2:10 PMJoffrey
07/05/2022, 2:10 PMfun <T> Sequence<T>.map(transforms: List<(T) -> T>): Sequence<T> =
transforms.fold(this) { s, t -> s.map(t) }
See: https://pl.kotl.in/clSuDg4m0Alan B
07/05/2022, 2:10 PMJoffrey
07/05/2022, 2:11 PMfold is not operating on the sequence, but on the list of transformations, so it builds a new sequence by repeatedly applying map with each transform. The initial value for the fold is the initial sequence.Alan B
07/05/2022, 2:12 PMAlan B
07/05/2022, 2:16 PMAlan B
07/05/2022, 2:18 PMAlan B
07/05/2022, 2:30 PM(T) -> T not (T) -> R (for my example)Klitos Kyriacou
07/05/2022, 2:43 PMsequence.map {
it.let(::transform1)
.let(::transform2)
.let(::transform3)
}
This also works just as well with collections, as it transforms it element-by-element.Alan B
07/05/2022, 2:55 PMephemient
07/05/2022, 3:50 PMfun <T> Sequence<T>.map(transforms: Iterable<(T) -> T>): Sequence<T> = map {
transforms.fold(it) { s, t -> s.map(t) }
}