Just guessing that TCO is tail call optimization. What would that exactly do?
TCO is a pretty standard term and there's available literature for how TCO works that's outside the scope of this article, which is why I linked to Wikipedia.
Loop management by Promise implementation? What does that mean?
It means that the Promise implementation manages the continuation callback and not JavaScript's call stack.
Again, there's available literature on how call stacks work or how Promise/Future implementations work, but in case you're inclined to read some source code instead, I think the code that I wrote for Funfix is pretty readable, so here you go, this is what "call management" looks like: future.ts.
Mhm OK. What has chaining then calls to do with heap memory abuse?
Compiler error, does not parse.
What? Eliminating frames (which frames? what is a frame here?)
Promise, and all alternative implementations, are essentially building a call stack, either managed explicitly, or implicitly by means of runtime facilities for doing asynchronous calls (i.e. setTimeout, setImmediate or a thread-pool in Java).
But regardless, the effect is that you're substituting one stack representing active call sites for another stack, which keeps track of where the code pointer needs to jump or (in our case) what callbacks need to be called once the result is ready. And in this instance, a "frame" is one such entry in the stack.
This should be taught in computer science, although I've seen lately that universities are dropping assembly programming courses, which is a pitty.
Eliminating when and why?
Eliminating in tail call position because otherwise the process crashes and that makes Jack a dull boy.
But using heap memory and a stack should be an important difference. At least you've said, that Promises do pollute the heap memory, not the (call) stack. It would be nice to consider the difference - or to explain what is meant by abusing the heap memory.
There are languages that don't use the stack at all, e.g. Haskell. In the larger scheme of things, the difference is not that big.
What I'm saying is that for functional programming, which helps with reasoning about the ordering of asynchronous / concurrent calls, there's no way to express loops (without mutation) unless you express them as tail recursive calls.
And it's really easy btw to end up with long loops that need to be processed recursively (TypeScript syntax to make the definition clear, but that's irrelevant):
type Task<A> = () => Promise<A>
interface List<A>
class Cons<A> implements List<A> {
constructor(readonly head: A, readonly rest: Task<List<A>>) {}
}
class Nil implements List<A> {
constructor(readonly error?: any) {}
}
There. I've just described a potentially infinite, asynchronous, lazy, referentially transparent list.
Describing operations on this with async / await or callbacks works, sort of, but it's an exercise in frustration. And by using the Promise pattern as it was meant to be used, you end up with a blown heap.
What about this list makes it lazy or asynchronous? Or for that matter, referentially transparent? JavaScript is strictly evaluated, you can't even construct a value in that list unless you have the value to want to add to it already. I see no function signatures indicating callbacks for asynchronous operations either. There's nothing preventing one from mutating one of the references within the list, so I'm not sure where the referentially transparent claim is coming from.
The fact that the rest of the stream is described with a parameter-less function, also called a thunk, which is how you turn strict things into lazy ones, obviously.
asynchronous
The fact that the rest of the stream can be processed with a Promise.
Or for that matter, referentially transparent?
The fact that side effects get suspended in that parameter-less function and there's no mutation involved for building it.
I see no function signatures indicating callbacks for asynchronous operations either
That's what the Promise is for. If it would work properly that is.
There's nothing preventing one from mutating one of the references within the list
I've modified the original definition to have readonly on those members, if that makes you happy :-)
I missed the Task<A> you had in the constructor signature the first time, my bad.
Also, I know what a thunk is, and your tone in these comments comes off as pretty condescending. I think you'd be better received here if you abstained from talking to people like they have no idea what they're talking about.
I apologize for sounding condescending or aggressive, I do try to explain things using less jargon (thunk is less understood jargon).
I'm also incredibly annoyed. Somebody here called me a scumbag for talking about a library I've been working on in my free time at personal sacrifice, so I don't necessarily know what I'm doing here, instead of going home to my wife and kid, but I guess I'll just pass by the bank first to retrieve my million dollars.
As you can see, my humor doesn't help much either.
1
u/alexelcu Oct 11 '17 edited Oct 11 '17
TCO is a pretty standard term and there's available literature for how TCO works that's outside the scope of this article, which is why I linked to Wikipedia.
It means that the
Promiseimplementation manages the continuation callback and not JavaScript's call stack.Again, there's available literature on how call stacks work or how Promise/Future implementations work, but in case you're inclined to read some source code instead, I think the code that I wrote for Funfix is pretty readable, so here you go, this is what "call management" looks like: future.ts.
Compiler error, does not parse.
Promise, and all alternative implementations, are essentially building a call stack, either managed explicitly, or implicitly by means of runtime facilities for doing asynchronous calls (i.e.setTimeout,setImmediateor a thread-pool in Java).But regardless, the effect is that you're substituting one stack representing active call sites for another stack, which keeps track of where the code pointer needs to jump or (in our case) what callbacks need to be called once the result is ready. And in this instance, a "frame" is one such entry in the stack.
This should be taught in computer science, although I've seen lately that universities are dropping assembly programming courses, which is a pitty.
Eliminating in tail call position because otherwise the process crashes and that makes Jack a dull boy.