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

Daniel

05/08/2017, 11:09 PM
Suppose I want to get the lowest property of an object in an array of said objects, might there be a better way than:
source.minBy { it.startX }?.startX ?: 0
Maybe a way to get the value of the property directly from the collection instead the object which it belongs to?
k

kevinmost

05/08/2017, 11:20 PM
ferranis:
source.map { it.startX }.min() ?: 0
d

Daniel

05/08/2017, 11:21 PM
Thank you for your reply! This seems to at least remove the indirection over the object
👍 1
k

kevinmost

05/09/2017, 1:44 AM
you can also use a method reference here. I dunno what your source class name is, but if the class name is
Foo
,
source.map(Foo::startX).min() ?: 0
, if you think that looks cleaner
d

Daniel

05/09/2017, 6:46 AM
Thank you again! I didn't use this till now but this is a nice way to flatten the expression a bit! 🙂