-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
can a closure contain (or return?) a pure function #46
Comments
👍 |
@magopian When you say:
you are making assumptions, based on what you think is a necessary part of these closure statements, a To Make it more clear I thought why not make an actual working example, which does the exact same thing without using the Before getting into the main part of my example let's say we have a
I know the code block above may sound complicated, but let's use it as a black box and assume this allows us just to use closures without returning functions. the first code block I want to mention here is the code example in the book:
As we all know none of these functions is pure except for Using
This may seem much more code but I transformed it like this just to put comments, but the better syntax transformation could be:
Which is more like the first closure code block we have mentioned above, but with a big difference:
and the way we could have this nested function call:
is like:
As you see we get almost the same result using only numbers, without passing functions around.
the most inner function which as it is mentioned on the book is:
is a pure function and if you save its
And then if you call right to its upper function:
Now you can have access to this function like:
which can be called as a pure standalone function:
All the code above here is just to clarify one single point. The way closure
as it is called in my last code block, it doesn't need its parent's environment object. It only needs to get defined, then you could call it anywhere. Hope this could be helpful. |
@magopian I didn't read the above comment (tl;dr) but just wanted to point out that your example of a closure returning a pure function wasn't correct. Your example: function (x) {
return function (y) {
return function (z) {
return z
}
}
} According to @raganwald the definition of a closure is "a function containing one or more free variables". Since none of the functions above contain any free variables, they are all in fact pure functions, despite being nested. That said, I think you're right that "contain" should be "return" in the context of the original challenge. A closure can contain a pure function pretty simply. Consider this closure: (x) => {
let foo = (a) => a
return foo(y * x)
} I realize this is banal, but the closure above does "contain" the pure function I can also think of a somewhat contrived example of a closure returning a pure function: (x) =>
(y) => {
return z ? (a) => a : (a) => y * a
}
} In this case, the first function |
So, the conclusion is that it is possible that a closure both contains and returns a pure function? |
I'm wondering if the following question (from the "closures" part: https://leanpub.com/javascript-allonge/read#closures) should read "returns" instead of "contains":
If pure functions can contain closures, can a closure contain a pure function? Using only what we’ve learned so far, attempt to compose a closure that contains a pure function. If you can’t, give your reasoning for why it’s impossible.
My first idea was to come up with this:
This one contains a pure function, and could be seen as useful, and the answer could then be "yes".
However, if the sentence is reworded to use "returns", then I believe the answer would then be "no":
If pure functions can contain closures, can a closure contain a pure function? Using only what we’ve learned so far, attempt to compose a closure that RETURNS a pure function. If you can’t, give your reasoning for why it’s impossible.
In this case, here's an example:
In this case, the
function (z) { return z }
, even though it's pure, still is in a closure, and still needs its parent's environment (so, its closure).You could also argue that this construct is pretty useless, as neither x nor y are used.
On a side note, wouldn't it be beneficial to the reader to have this question reworded to something like "show that a closure can't return a pure function ..."
The text was updated successfully, but these errors were encountered: