Geert
05/20/2025, 7:40 AMperson.copy {
Person.names.last transform { it.replaceFirstChar(Char::uppercase) }
}
Of course the last
val does not exist, but is there some way to do this?Alejandro Serrano.Mena
05/20/2025, 8:38 AMOptional
that matches the last element of a collection. It should be something rought equivalent to:
fun <A> last(): Optional<List<A>, A> = Optional(
getOption = { it.lastOrNull().toOption() },
set = { lst, elt ->
if (lst.isEmpty()) lst
else lst.dropLast(1) + listOf(elt)
}
)
fun <A, B> Optional<A, List<B>>.last(): Optional<A, B> =
this compose Optional(
getOption = { it.lastOrNull().toOption() },
set = { lst, elt ->
if (lst.isEmpty()) lst
else lst.dropLast(1) + listOf(elt)
}
)
Geert
05/20/2025, 2:03 PM