Hi, I'm lookin the way to implement flow which will emit values according to input parameter known when collect is call.
Let's say that I have some operation
(P)->R
and I want to use flow api to handle it.
I can't create flow standard way through flow builder because parameter P is not known at this point.
Is there a way to achieve result similar to this:
Copy code
fun Flow<R>.asLambda(operation: (P)->R): (P)->Unit = {
// some magic that allow me to use P parameter to modify existing flow emission
collect()
}
s
Shawn
12/19/2023, 7:56 PM
Copy code
fun <P, R> Flow<R>.asLambda(operation: (P) -> R): (P) -> Unit = {
operation(it)
...
}
d
Daniel Pitts
12/20/2023, 12:56 AM
Hmm, why not
Copy code
fun makeFlow(parameter: P) = flow {
val r = operator(P)
k
Karol Kulbaka
12/20/2023, 2:34 PM
I don't have parameter at the moment of flow creation. That's the point, I'm not even sure is effect I want to achieve in possible, since flow immutability principle
d
Daniel Pitts
12/20/2023, 6:13 PM
My suggestion is to not create the flow until you do have the parameter.
k
Karol Kulbaka
12/21/2023, 7:53 PM
I can't use flow api then, which was the main goal, mission failed 😕
d
Daniel Pitts
12/21/2023, 10:42 PM
It's a lot like the Sequence API. If you think of it that way, you'd probably be better able to make use of it.