Hi <@U3DE1TXKP> here is how I would do it.
# announcements
a
Hi @robstoll here is how I would do it.
r
I don't want a map function but a filter function (but otherwise I would do the same). I have a solution which uses an unchecked cast btw.
m
What do you want it to return if
R
is null?
r
I want filter those
listOf(1 to null, 1 to "").filter...
=> only
list(1 to "")
is left and has type
Pair<Int, String>
a
Copy code
kotlin
val x = p.safer()
if (x != null) // right side is not null
m
Oh, so you have a list of them.
r
yes or a sequence
or something like that
m
You could use that function to filter with.
list.filterNotNull { it.safer() }
a
yes.. so you use the function to filter out not null.
yes!
r
filterNotNull
does not expect a lambda in my version
does it exist in 1.3?
the pairs as such are not null
d
Consider having
<L: Any, R: Any>
upper bounds on those type parameters.
a
haven check out 1.3 yet. let me check on my side.
m
Oh, sorry. It should be
list.mapNotNull { it.safer() }
r
ah nice... was not aware of this one 🙂
a
i like Kotlin,… many gems
Copy code
val unsafeList = listOf("1" to 1, "yes" to null, "no" to 10)
    val safeList = unsafeList.asSequence().mapNotNull { it.safer() }.toList()
    println("unsafeList = $unsafeList")
    println("safeList = $safeList")