I'm working on porting to Kotlin one of my on C# projects which uses certain features of linq. As an example lets take the following linq query:
from a in A
from b in B
from c in C
select fun(a,b,c);
In C# this allows to chain functions of any type then collect the results in an easy to read way which is preetty much may requirement. This is equivalent (more or less) to:
A.SelectMany(a => B, (a, b) => new {a, b}).SelectMany(t => C, (t, c) => fun( t.a, t.b, c));
It is not a problem to achieve functionality of Enumerable.SelectMany in Kotlin but it is still as noisy as the C# equivalent.
Is there any way to achieve something similar in Kotlin without fiddling explicilty with nested tuples but closer to the linq?
Marko Topolnik provided the following as a comment, but it is actually a valid solution:
A.flatMap { a ->
B.flatMap { b ->
C.map { c ->
fun(a, b, c)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With