how to map a binary tree in kotlin?
# announcements
i
how to map a binary tree in kotlin?
d
Which binary tree class?
k
And map it to what
i
data Tree a = Leaf a | Node (Tree a) (Tree a) maptree :: (a->b) -> Tree a -> Tree b maptree f (Leaf a) = Leaf (f a) maptree f (Node xl xr) = Node (maptree f xl) (maptree f xr) does this relate to Reader Monad?
g
What is exactly not clear on Kotlin implementation? There is no pattern matching on Kotlin, but should be very close to this Haskell snippet with sealed class for data type and
when
for pattern matching
i
it would look like ? with Kotlin
g
Something like this https://pl.kotl.in/Tp_AvUvQV
i
cool!
k
Copy code
fun <T, R> Tree<T>.map(transform: (T) -> R): Tree<R> {
    return when (this) {...}
}
Why not use an expression body?
g
Why not use an expression body?
Because I don’t like to use expression body for long complicated declarations (like in this with multiple generics and lambda) it make it even more complicated and
return when
reads better in this case, but I don’t have any specific rules, just a matter of style, tend to make everything as readable as possible
k
Mmh, I actually like to use with when expressions; You still get a nice looking block.
g
which is non-readable in some cases
it will be more readable if you omit return type, but it also hurts readability