Harold Scissors
06/27/2023, 9:45 PM@Composable
function from a .map{}
but not .flatMap{}
?Stylianos Gakis
06/27/2023, 9:50 PMHarold Scissors
06/27/2023, 10:10 PMdata class
.
Oddly enough if we I just write this extension function and use this instead of Map.flatMap
it works
public inline fun <K, V, R> Map<out K, V>.composableFlatMap(transform: (Map.Entry<K, V>) -> Iterable<R>): List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
Stylianos Gakis
06/27/2023, 10:17 PMHarold Scissors
06/27/2023, 10:18 PMStylianos Gakis
06/27/2023, 10:21 PMHarold Scissors
06/27/2023, 10:24 PMHarold Scissors
06/27/2023, 10:25 PM@Composable
Loney Chou
06/28/2023, 1:18 AMHarold Scissors
06/28/2023, 3:17 AMAlex Vanyo
06/28/2023, 6:33 PM.flatMap
should work the same way as .map
. inline fun
are pretty neat in that they allow calling back and forth from colored functions like suspend
and @Composable
without needing to be either themselvesJohn Petitto
06/28/2023, 6:53 PM@Composable
from flatMap
but funny enough you can wrap flatMap
and it works!
@Composable
fun SimpleComposable() {}
inline fun <T, R> Iterable<T>.flatMapWrapper(transform: (T) -> Iterable<R>): List<R> = flatMap(transform)
@Composable
fun Run() {
listOf(1, 2, 3)
.map { SimpleComposable() } // works
.flatMap { listOf(SimpleComposable()) } // compilation error
.flatMapWrapper { listOf(SimpleComposable()) } // works
}
Harold Scissors
06/29/2023, 12:26 AMAlex Vanyo
06/29/2023, 6:21 PMJohn Petitto
06/29/2023, 6:44 PMLoney Chou
06/30/2023, 8:16 AMflatMap
is definitely inline
.Harold Scissors
07/03/2023, 5:09 PMAlex Vanyo
07/03/2023, 5:41 PM