Is there some API in stdlib to do that in a cleane...
# announcements
d
Is there some API in stdlib to do that in a cleaner way?
Copy code
@Suppress("UNCHECKED_CAST")
pairsListOfNullableApiParamAndNullableString
        .filterNot { it.first == null || it.second == null }
        .map { it as Pair<ApiParam, String> }
e
Could be this:
Copy code
pairsListOfNullableApiParamAndNullableString
        .mapNotNull { if (it.first != null && it.second != null) Pair(it.first, it.second) else null }
d
Better but still not what I was hoping for 😋 I'll go with an extension, thank 🥂
a
@elizarov should that smart cast work if
pairsListOfNullableApiParamAndNullableString: List<Pair<ApiParam?, String?>>
? I’d think that it should, since
first
and
second
are
val
, but somehow it doesn’t work https://pl.kotl.in/BJqjvQPXE
for what it’s worth, @Davide Giuseppe Farella you can also do:
Copy code
pairsListOfNullableApiParamAndNullableString
    .mapNotNull { (first, second) ->
        first?.let { second?.let { first to second } }
    }
h
Copy code
inline fun <A, B, R> zip(a: A?, b: B?, func: (A, B) -> R): R? =
  if (a != null && b != null)
    func(a, b) else null

val list: List<Pair<String?, String?>> = emptyList()
val result: List<Pair<String, String>> = list.mapNotNull { (first, second) -> zip(first, second, ::Pair) }
d
Yep, thanks for the hints, I was just wondering if there was something on the stdlib 🥂