... where does the default `S` of the result even ...
# functional
r
... where does the default
S
of the result even come from? O.o
k
The first element. (And it throws if you have 0 elements)
r
So it upcasts the first element to
S
? Ok, weird design.
e
how is it weird? you wouldn't be able to
Copy code
sealed class Node<out T>
data class Leaf<out T>(val data: T) : Node<T>()
data class Tree<out T>(val left: Node<T>, val right: Leaf<T>) : Node<T>()
Copy code
listOf(Leaf(1), Leaf(2), Leaf(3)).reduce(::Tree)
otherwise
r
... call it unexpected. In Scala, reduce/fold were similar, the distinction was over
reduce
vs.
reduceRight
. Haskell has only one anyway IIRC.
e
no, Haskell has both
the names are closer to match most other languages such as JS and Python
r
Didn't read the type signature
<S, T: S>
on reduce correctly.