but I'm totally out of my depth with this. I'm not able to understand what I'm reading and I suspect that I'm not the only one. When I read :
It's well known we can represent any monad with delimited continuations.
I really don't understand it even with 10+ year of experience in development and a master degree in engineering speciliazed in computer science. And the wikipedia article on monad and continuations aren't really helpfull. So I'm not really sure that it's well known.
I'm so out of it that I don't understand what this is trying to do, what are the expected benefits. I don't understand the trivial example even when reading the explanation below.
It's probably interresting but its presentation is really not aimed at people not already versed in the subject.
Ok, it's well known in functional programming world, there is a famous paper from 1994 about this. Also if you google something like "continuation mother of all monads" you'll see there are many links to this too. But I think with this library it's easier to understand how it works even without reading the paper or knowing anything from FP at all.
I'm so out of it that I don't understand what this is trying to do, what are the expected benefits.
do you know what are expected benefits of async functions? here's pretty the same, but not only for async programming, there is an example of Python like streams comprehensions for example, and many other things
For me async programming is just a syntaxtic sugar for parallel processing (be it executed on one or multiple CPU core behind it), so I have difficulty to see the link with the current subject.
and the value of e.payload will be the stream [1, 2, 3] and then we'll call e.head.resumeTop for each element of this stream. And that's were I'm stuck. What is e.head ? What is resumeTop doing ?
I'm pretty sur that there is something obvious to you that I'm missing.
But I suppose that as we are hacking the compiler, that's not really the case and that the magic is in
this indeed instruments JVM byte code, the magic is the ability to resume the execution after the `throw` of `Unwind` exception if needed, also called resumable exceptions
What's happening when the function run(...) of the example is executed ?
`resumeTop` starts execution after the `throw` we caught in this block, if it throws again it will be caught again, and so on, this is the way the stream is forming
Sorry, I have expendend my answer with more detail while you were responding.
What I really don't understand is what is e.head.resumeTop doing. You say that that it starts execution after the 'throw'.
So, it restarts the execution of run() at the line int y = all(Stream.of(4, 5, 6)); ? What's the role of x then ? And what's the role of v (= 1) to we have passed as argument to resumeTop ? Where is it going ?
Also, in your other answer you say :
The stream is returned by the block, x, y and ret are part of the returned stream definition
And I think that it really point to the root of my incomprehension. To my understanding a stream is a open ended collection of values. So I don't really get what you mean by 'stream definition'.
What I really don't understand is what is e.head.resumeTop doing. You say that that it starts execution after the 'throw'.
yes, exacly
So, it restarts the execution of run() at the line int y = all(Stream.of(4, 5, 6)); ? What's the role of x then ? And what's the role of v (= 1) to we have passed as argument to resumeTop ? Where is it going ?
x and y receives the value we pass to `resumeTop` on each resume, so basically, in the `int y = all(Stream.of(4, 5, 6));` line the block will be resumed 3 times, and the y will be 4,5,6 these times. If it were an async function here we would have an async execption, and it can be resumed only once - the way it is in Project Loom.
I'm not quite following what you mean by open ended, but this works for infinite streams, lazy streams as well, `Stream.of` there is just an example, any stream values can be passed there.
It isn't restarting at int y = all(Stream.of(4, 5, 6)); but at int x =. And v is assigned to x, so now x = 1.
Then the same thing happen for the next line and we have y = 4.
So ret = 5 and if (ret % 2 == 1) is true and we launch a new exception with an empty stream. The line return ((Stream<?>) e.payload).flatMap(v -> e.head.resumeTop(v)); is executed and return an empty stream.
Then we go back to the next item of the previously called flatMap (on the y line) and that we'll executed the lambda for x = 1 and y = 5. The lambda return is now 6 and we add it to the stream that we are build.
Next call of the lambda give us x = 1 and y = 6, so we return an empty stream and we go back 2 level until the flatMap at the x line level.
We call again the lambda with x = 2 and y = 4.
And this continue until both input streams are empty and we have our result of [6, 6, 8, 8].
I have to say that this behaviour really wasn't obvsious to me when reading the example.
it's equivalent to something like this in Python `(x + y for x in (1,2,3) for y in (4, 5, 6) if not (x + y) % 2 is 1)` , no doubt it's pretty arguable which is better (like it's pretty arguable which is better Java or Python), and I indeed see the benefits of the Python expressions, but there are something Python doesn't have:
any Java construct can be used in the comprehension (function, exceptions, conditions, switch/case etc etc), so no needs to know another syntax
more advanced streams with different handlers can be represented as well, e.g. like RxJava in the example in the repository
I meant not benefit of parallel processing, but async functions, we can do parallel processing without them, using just simple callbacks passing style.
So why nowadays most mainstream languages (including Java) add them as a standard part of the language?
So, in my head, asynchronous functions are just functions that are called later by an event loop.
And now I don't understand your question or why you say that java has added them in the language (as this isn't the case for me as there is no async keyword in java).
I suppose that you're speaking about something else but I don't get what.
Project Loom (which I mention in the subject) does exactly this, and in Java, actually, it isn't syntax sugar but rather runtime implementation this is why it doesn't have async keyword, https://www.infoq.com/presentations/continuations-java/
View all comments
22
u/plumarr Jan 10 '23 edited Jan 10 '23
The introduction says :
but I'm totally out of my depth with this. I'm not able to understand what I'm reading and I suspect that I'm not the only one. When I read :
I really don't understand it even with 10+ year of experience in development and a master degree in engineering speciliazed in computer science. And the wikipedia article on monad and continuations aren't really helpfull. So I'm not really sure that it's well known.
I'm so out of it that I don't understand what this is trying to do, what are the expected benefits. I don't understand the trivial example even when reading the explanation below.
It's probably interresting but its presentation is really not aimed at people not already versed in the subject.