David Kubecka
12/01/2023, 3:00 PMmyList.map { transform(it) }
myString.takeIf { it.isNotEmpty() }
As an alternative, however, one could also use function/method references, e.g.
myList.map(::transform)
myString.takeIf(String::isNotEmpty)
Which convention do you prefer and why?LeoColman
12/01/2023, 3:11 PMLeoColman
12/01/2023, 3:11 PMLeoColman
12/01/2023, 3:12 PMdaoObjects.map(::toJsonResponse).filter(::onlyErrors)
LeoColman
12/01/2023, 3:14 PMDavid Kubecka
12/01/2023, 3:21 PMString::isNotEmpty
LeoColman
12/01/2023, 3:22 PMLeoColman
12/01/2023, 3:22 PMMyJsonObject::parse
David Kubecka
12/01/2023, 3:28 PMMap<String, Int>::size
Shawn
12/01/2023, 3:37 PM.map(AbstractMap.SimpleEntry::getKey)
.map { it.key }
Jacob
12/01/2023, 3:37 PMLeoColman
12/01/2023, 4:01 PMLeoColman
12/01/2023, 4:03 PMMap<String, Int>::size
That is a very case-by-case situation. I think generics are complex enough to require their own lambda `{}`block 🙂ephemient
12/02/2023, 12:30 AMthis
-bound references might be the only ones I use occasionallyephemient
12/02/2023, 12:34 AM::
actually involves a bit more machinery (foo::bar == foo::bar
, { foo.bar() } != { foo.bar() }
) which is useful in some scenarios but unnecessary in mostDavid Kubecka
12/03/2023, 9:28 PMMark
12/06/2023, 8:05 AMfoo
is using lazy
delegate: https://pl.kotl.in/SFXtWGMzr
i.e. we avoid an unnecessary instantiation by using a lambda.
val foo1 by lazy {
println("foo1 instantiated")
Any()
}
val block1 = foo1::toString
println("foo1")
val foo2 by lazy {
println("foo2 instantiated")
Any()
}
val block2 = { foo2.toString() }
println("foo2")
output:
foo1 instantiated
foo1
foo2
David Kubecka
12/06/2023, 8:50 AMMark
12/06/2023, 8:51 AMFoo
in my case is an event handler, so it only needs to be instantiated when there is an event, hence lazy
.ribesg
12/06/2023, 3:38 PMephemient
12/06/2023, 3:40 PMephemient
12/06/2023, 3:40 PMMark
12/07/2023, 4:34 AMequals
/`hashCode` important in that case?ephemient
12/07/2023, 4:42 AM