https://kotlinlang.org logo
#announcements
Title
# announcements
i

Ifvwm

08/04/2019, 11:29 AM
how to map a binary tree in kotlin?
d

Dominaezzz

08/04/2019, 11:30 AM
Which binary tree class?
k

Kroppeb

08/04/2019, 12:10 PM
And map it to what
i

Ifvwm

08/04/2019, 2:00 PM
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

gildor

08/04/2019, 2:04 PM
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

Ifvwm

08/04/2019, 2:07 PM
it would look like ? with Kotlin
g

gildor

08/04/2019, 2:21 PM
Something like this https://pl.kotl.in/Tp_AvUvQV
i

Ifvwm

08/04/2019, 3:06 PM
cool!
k

Kroppeb

08/04/2019, 3:07 PM
Copy code
fun <T, R> Tree<T>.map(transform: (T) -> R): Tree<R> {
    return when (this) {...}
}
Why not use an expression body?
g

gildor

08/05/2019, 5:50 AM
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

Kroppeb

08/05/2019, 1:33 PM
Mmh, I actually like to use with when expressions; You still get a nice looking block.
g

gildor

08/05/2019, 1:43 PM
which is non-readable in some cases
it will be more readable if you omit return type, but it also hurts readability
4 Views