https://kotlinlang.org logo
#codereview
Title
# codereview
z

zokipirlo

11/21/2018, 12:48 PM
Hi! Still learning functional things in Kotlin and would need some help. Can someone please check my example and tell me if this example can be done better? https://try.kotlinlang.org/#/UserProjects/h9ra2isjl4vudcq3mmo8gii4n8/fb6lvfgtq7ppf3es5tt36b5h0o Thanks! [moved from #general]
h

hho

11/21/2018, 1:13 PM
I'm not sure what you're looking for, but I made https://pl.kotl.in/ry51qAM0m
z

zokipirlo

11/21/2018, 1:44 PM
I was thinking if there exists any extra magical functional construct to do that in one step, not making multiple lists or maps 🙂 Thanks, looks much prettier solution
a

arekolek

11/21/2018, 3:14 PM
You can also do:
Copy code
users
    .filterNot { it.state == 5 }
    .groupingBy { it.id }
    .reduce { _, acc, user ->
        maxOf(acc, user, compareBy { it.state })
    }
    .values
z

zokipirlo

11/21/2018, 3:18 PM
Thanks! Looks nicer and reduce is not nullable (mapValues is).
e

edwardwongtl

11/22/2018, 3:27 AM
There is
mapNotNull{}
as a non-null version of
map{}
3 Views