Am I right in thinking that molecule basically pro...
# functional
y
Am I right in thinking that molecule basically provides monad comprehension over lists? Because it allows you to deal with each element of a flow as they come in and make a flow out of that, and you can do that for any number of flows you want, and so it's a monad comprehension, right?
n
I think it essentially gives you monad comprehensions over `StateFlow`s. It would be interesting to see if you could use the same mechanism to actually get a monad comprehension over lists / other nondeterministic monads though.
@Youssef Shoaib [MOD] - Do you have an example of trying to use molecule for a list monad comprehension? I'm trying to play around with this now, but so far I haven't been able to come up with anything reasonable.
Ok, after investigating this a bit more, I'm almost certain it doesn't work that way. It's essentially a comprehension syntax for
StateFlow
, sure -- but I don't think it easily extends to a comprehension syntax for
List
or list-like monads. You can definitely take two lists as input, put them into a flow, and attempt to use
collectAsState
to "bind" the list elements to a concrete list -- yet the results are not what you'd expect form the standard list monad. Rather than enumerating all possibilities like you'd want in the list monad, you're essentially temporally interpolating between the different list elements you
collectAsState
. Perhaps there's a less obvious way to make this work, but at least in the naive implementation I tried out, it doesn't work.
y
Very interesting, I wonder if there's sufficient hooks in compose to allow controlling the emissions of the flows so that it works like a list comprehension. Something that perhaps like waits until you fully process one item before it allows sending the next item through. Maybe some
Channel
based thing could be more helpful
@Nathan Bedell I think you should take a look at this... https://kotlinlang.slack.com/archives/C5UPMM0A0/p1713984299484519 I DID IT!! Proper delimited continuations in Kotlin, including clean syntax for list comprehensions. Check out this test file for examples
❤️ 1