https://kotlinlang.org logo
#functional
Title
# functional
r

reactormonk

11/22/2023, 6:34 PM
... where does the default
S
of the result even come from? O.o
k

Klitos Kyriacou

11/22/2023, 6:35 PM
The first element. (And it throws if you have 0 elements)
r

reactormonk

11/22/2023, 6:36 PM
So it upcasts the first element to
S
? Ok, weird design.
e

ephemient

11/22/2023, 6:46 PM
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

reactormonk

11/22/2023, 6:48 PM
... call it unexpected. In Scala, reduce/fold were similar, the distinction was over
reduce
vs.
reduceRight
. Haskell has only one anyway IIRC.
e

ephemient

11/22/2023, 6:49 PM
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.
2 Views