What is “function*” in JavaScript?
In this page I found a new JavaScript function type:
// NOTE: "function*" is not supported yet in Firefox.
// Remove the asterisk in order for this code to work in Firefox 13
function* fibonacci() { // !!! this is the interesting line !!!
let [prev, curr] = [0, 1];
for (;;) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
I already know what yield
, let
and [?,?]=[?,?]
do, but have no idea what the function*
is meant to be. What is it?
P.S. don't bother trying Google, it's impossible to search for expressions with asterisks (they're used as placeholders).
javascript function ecmascript-6
|
show 1 more comment
In this page I found a new JavaScript function type:
// NOTE: "function*" is not supported yet in Firefox.
// Remove the asterisk in order for this code to work in Firefox 13
function* fibonacci() { // !!! this is the interesting line !!!
let [prev, curr] = [0, 1];
for (;;) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
I already know what yield
, let
and [?,?]=[?,?]
do, but have no idea what the function*
is meant to be. What is it?
P.S. don't bother trying Google, it's impossible to search for expressions with asterisks (they're used as placeholders).
javascript function ecmascript-6
4
The comment in the example is quite old now,function*
syntax is supported in Firefox since v26: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…* . Older versions used a different syntax.
– Nickolay
Sep 9 '14 at 22:16
33
Regarding Google, just search for "function star" or "function asterisk". That's how I found this question ;).
– trysis
Feb 2 '15 at 22:05
2
Looks like the*
was stripped from the link from @Nickolay. Here's a link directly tofunction*
at MDN. Sure enough, "basic" support since v26.
– ruffin
Apr 29 '15 at 17:16
Another MDN link (which, by the way, I found on the MDN page linked by OP): developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– BlueRaja - Danny Pflughoeft
Dec 1 '15 at 17:20
Another useful MDN link: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…*
– Logan
Jun 23 '17 at 5:46
|
show 1 more comment
In this page I found a new JavaScript function type:
// NOTE: "function*" is not supported yet in Firefox.
// Remove the asterisk in order for this code to work in Firefox 13
function* fibonacci() { // !!! this is the interesting line !!!
let [prev, curr] = [0, 1];
for (;;) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
I already know what yield
, let
and [?,?]=[?,?]
do, but have no idea what the function*
is meant to be. What is it?
P.S. don't bother trying Google, it's impossible to search for expressions with asterisks (they're used as placeholders).
javascript function ecmascript-6
In this page I found a new JavaScript function type:
// NOTE: "function*" is not supported yet in Firefox.
// Remove the asterisk in order for this code to work in Firefox 13
function* fibonacci() { // !!! this is the interesting line !!!
let [prev, curr] = [0, 1];
for (;;) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
I already know what yield
, let
and [?,?]=[?,?]
do, but have no idea what the function*
is meant to be. What is it?
P.S. don't bother trying Google, it's impossible to search for expressions with asterisks (they're used as placeholders).
javascript function ecmascript-6
javascript function ecmascript-6
edited Apr 25 '14 at 6:00
thefourtheye
166k27300378
166k27300378
asked Mar 8 '12 at 15:53
string QNAstring QNA
1,36531112
1,36531112
4
The comment in the example is quite old now,function*
syntax is supported in Firefox since v26: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…* . Older versions used a different syntax.
– Nickolay
Sep 9 '14 at 22:16
33
Regarding Google, just search for "function star" or "function asterisk". That's how I found this question ;).
– trysis
Feb 2 '15 at 22:05
2
Looks like the*
was stripped from the link from @Nickolay. Here's a link directly tofunction*
at MDN. Sure enough, "basic" support since v26.
– ruffin
Apr 29 '15 at 17:16
Another MDN link (which, by the way, I found on the MDN page linked by OP): developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– BlueRaja - Danny Pflughoeft
Dec 1 '15 at 17:20
Another useful MDN link: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…*
– Logan
Jun 23 '17 at 5:46
|
show 1 more comment
4
The comment in the example is quite old now,function*
syntax is supported in Firefox since v26: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…* . Older versions used a different syntax.
– Nickolay
Sep 9 '14 at 22:16
33
Regarding Google, just search for "function star" or "function asterisk". That's how I found this question ;).
– trysis
Feb 2 '15 at 22:05
2
Looks like the*
was stripped from the link from @Nickolay. Here's a link directly tofunction*
at MDN. Sure enough, "basic" support since v26.
– ruffin
Apr 29 '15 at 17:16
Another MDN link (which, by the way, I found on the MDN page linked by OP): developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– BlueRaja - Danny Pflughoeft
Dec 1 '15 at 17:20
Another useful MDN link: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…*
– Logan
Jun 23 '17 at 5:46
4
4
The comment in the example is quite old now,
function*
syntax is supported in Firefox since v26: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…* . Older versions used a different syntax.– Nickolay
Sep 9 '14 at 22:16
The comment in the example is quite old now,
function*
syntax is supported in Firefox since v26: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…* . Older versions used a different syntax.– Nickolay
Sep 9 '14 at 22:16
33
33
Regarding Google, just search for "function star" or "function asterisk". That's how I found this question ;).
– trysis
Feb 2 '15 at 22:05
Regarding Google, just search for "function star" or "function asterisk". That's how I found this question ;).
– trysis
Feb 2 '15 at 22:05
2
2
Looks like the
*
was stripped from the link from @Nickolay. Here's a link directly to function*
at MDN. Sure enough, "basic" support since v26.– ruffin
Apr 29 '15 at 17:16
Looks like the
*
was stripped from the link from @Nickolay. Here's a link directly to function*
at MDN. Sure enough, "basic" support since v26.– ruffin
Apr 29 '15 at 17:16
Another MDN link (which, by the way, I found on the MDN page linked by OP): developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– BlueRaja - Danny Pflughoeft
Dec 1 '15 at 17:20
Another MDN link (which, by the way, I found on the MDN page linked by OP): developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– BlueRaja - Danny Pflughoeft
Dec 1 '15 at 17:20
Another useful MDN link: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…*
– Logan
Jun 23 '17 at 5:46
Another useful MDN link: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…*
– Logan
Jun 23 '17 at 5:46
|
show 1 more comment
3 Answers
3
active
oldest
votes
It's a Generator function.
Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.
Calling a generator function does not execute its body immediately; an iterator object for the function is returned instead. When the iterator's
next()
method is called, the generator function's body is executed until the firstyield
expression, which specifies the value to be returned from the iterator or, withyield*
, delegates to another generator function.
Historical note:
It's a proposed syntax for EcmaScript.next
.
Dave Herman of Mozilla gave a talk about EcmaScript.next. At 30:15 he talks about generators.
Earlier, he explains how Mozilla is experimentally implementing proposed language changes to help steer the committee. Dave works closely with Brendan Eich, Mozilla's CTO (I think), and the original JavaScript designer.
You can find more detail on the EcmaScript working group wiki: http://wiki.ecmascript.org/doku.php?id=harmony:generators
The working group (TC-39) has general agreement that EcmaScript.next should have some kind of generator iterator proposal, but this is not final.
You shouldn't rely on this showing up without changes in the next version of the language, and even if it doesn't change, it probably won't show up widely in other browsers for a while.
Overview
First-class coroutines, represented as objects encapsulating suspended execution contexts (i.e., function activations). Prior art: Python, Icon, Lua, Scheme, Smalltalk.
Examples
The “infinite” sequence of Fibonacci numbers (notwithstanding behavior around 253):
function* fibonacci() {
let [prev, curr] = [0, 1];
for (;;) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
Generators can be iterated over in loops:
for (n of fibonacci()) {
// truncate the sequence at 1000
if (n > 1000)
break;
print(n);
}
Generators are iterators:
let seq = fibonacci();
print(seq.next()); // 1
print(seq.next()); // 2
print(seq.next()); // 3
print(seq.next()); // 5
print(seq.next()); // 8
7
Follow up: what does a for loop with no parameters (for(;;)
) do? Why use it in this context?
– Fergie
Aug 28 '13 at 7:22
13
@Fergie,for(;;)
is the same aswhile (true)
. It's used in this context since the Fibonacci sequence is an unbounded sequence.
– Mike Samuel
Aug 28 '13 at 7:37
5
Prior art: C# yield?
– Dave Van den Eynde
Dec 19 '14 at 13:09
3
@DaveVandenEynde, prior prior art: Python yield. Prior prior prior art: CLU and Icon.
– Mike Samuel
Dec 19 '14 at 17:06
add a comment |
It's a generator function - and it said so in the page you cite, in the comment you replaced with "this is the interesting line"...
Basically it's a way to specify sequences programmatically so that they can be passed around and elements accessed by index without having to compute the entire sequence (possibly infinite in size) beforehand.
9
"accessed by index without having to compute the entire sequence" is possibly the most helpful bit of explanation about generators I've come across so far. I could see using this in an app, vs previously just understanding it theoretically.
– wes
Aug 6 '15 at 16:17
add a comment |
The function*
type looks like it acts as a generator function for processes that can be iterated.
C# has a feature like this using "yield return" see 1 and see 2
Essentially this returns each value one by one to whatever is iterating this function, which is why their use case shows it in a foreach style loop.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9620586%2fwhat-is-function-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's a Generator function.
Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.
Calling a generator function does not execute its body immediately; an iterator object for the function is returned instead. When the iterator's
next()
method is called, the generator function's body is executed until the firstyield
expression, which specifies the value to be returned from the iterator or, withyield*
, delegates to another generator function.
Historical note:
It's a proposed syntax for EcmaScript.next
.
Dave Herman of Mozilla gave a talk about EcmaScript.next. At 30:15 he talks about generators.
Earlier, he explains how Mozilla is experimentally implementing proposed language changes to help steer the committee. Dave works closely with Brendan Eich, Mozilla's CTO (I think), and the original JavaScript designer.
You can find more detail on the EcmaScript working group wiki: http://wiki.ecmascript.org/doku.php?id=harmony:generators
The working group (TC-39) has general agreement that EcmaScript.next should have some kind of generator iterator proposal, but this is not final.
You shouldn't rely on this showing up without changes in the next version of the language, and even if it doesn't change, it probably won't show up widely in other browsers for a while.
Overview
First-class coroutines, represented as objects encapsulating suspended execution contexts (i.e., function activations). Prior art: Python, Icon, Lua, Scheme, Smalltalk.
Examples
The “infinite” sequence of Fibonacci numbers (notwithstanding behavior around 253):
function* fibonacci() {
let [prev, curr] = [0, 1];
for (;;) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
Generators can be iterated over in loops:
for (n of fibonacci()) {
// truncate the sequence at 1000
if (n > 1000)
break;
print(n);
}
Generators are iterators:
let seq = fibonacci();
print(seq.next()); // 1
print(seq.next()); // 2
print(seq.next()); // 3
print(seq.next()); // 5
print(seq.next()); // 8
7
Follow up: what does a for loop with no parameters (for(;;)
) do? Why use it in this context?
– Fergie
Aug 28 '13 at 7:22
13
@Fergie,for(;;)
is the same aswhile (true)
. It's used in this context since the Fibonacci sequence is an unbounded sequence.
– Mike Samuel
Aug 28 '13 at 7:37
5
Prior art: C# yield?
– Dave Van den Eynde
Dec 19 '14 at 13:09
3
@DaveVandenEynde, prior prior art: Python yield. Prior prior prior art: CLU and Icon.
– Mike Samuel
Dec 19 '14 at 17:06
add a comment |
It's a Generator function.
Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.
Calling a generator function does not execute its body immediately; an iterator object for the function is returned instead. When the iterator's
next()
method is called, the generator function's body is executed until the firstyield
expression, which specifies the value to be returned from the iterator or, withyield*
, delegates to another generator function.
Historical note:
It's a proposed syntax for EcmaScript.next
.
Dave Herman of Mozilla gave a talk about EcmaScript.next. At 30:15 he talks about generators.
Earlier, he explains how Mozilla is experimentally implementing proposed language changes to help steer the committee. Dave works closely with Brendan Eich, Mozilla's CTO (I think), and the original JavaScript designer.
You can find more detail on the EcmaScript working group wiki: http://wiki.ecmascript.org/doku.php?id=harmony:generators
The working group (TC-39) has general agreement that EcmaScript.next should have some kind of generator iterator proposal, but this is not final.
You shouldn't rely on this showing up without changes in the next version of the language, and even if it doesn't change, it probably won't show up widely in other browsers for a while.
Overview
First-class coroutines, represented as objects encapsulating suspended execution contexts (i.e., function activations). Prior art: Python, Icon, Lua, Scheme, Smalltalk.
Examples
The “infinite” sequence of Fibonacci numbers (notwithstanding behavior around 253):
function* fibonacci() {
let [prev, curr] = [0, 1];
for (;;) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
Generators can be iterated over in loops:
for (n of fibonacci()) {
// truncate the sequence at 1000
if (n > 1000)
break;
print(n);
}
Generators are iterators:
let seq = fibonacci();
print(seq.next()); // 1
print(seq.next()); // 2
print(seq.next()); // 3
print(seq.next()); // 5
print(seq.next()); // 8
7
Follow up: what does a for loop with no parameters (for(;;)
) do? Why use it in this context?
– Fergie
Aug 28 '13 at 7:22
13
@Fergie,for(;;)
is the same aswhile (true)
. It's used in this context since the Fibonacci sequence is an unbounded sequence.
– Mike Samuel
Aug 28 '13 at 7:37
5
Prior art: C# yield?
– Dave Van den Eynde
Dec 19 '14 at 13:09
3
@DaveVandenEynde, prior prior art: Python yield. Prior prior prior art: CLU and Icon.
– Mike Samuel
Dec 19 '14 at 17:06
add a comment |
It's a Generator function.
Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.
Calling a generator function does not execute its body immediately; an iterator object for the function is returned instead. When the iterator's
next()
method is called, the generator function's body is executed until the firstyield
expression, which specifies the value to be returned from the iterator or, withyield*
, delegates to another generator function.
Historical note:
It's a proposed syntax for EcmaScript.next
.
Dave Herman of Mozilla gave a talk about EcmaScript.next. At 30:15 he talks about generators.
Earlier, he explains how Mozilla is experimentally implementing proposed language changes to help steer the committee. Dave works closely with Brendan Eich, Mozilla's CTO (I think), and the original JavaScript designer.
You can find more detail on the EcmaScript working group wiki: http://wiki.ecmascript.org/doku.php?id=harmony:generators
The working group (TC-39) has general agreement that EcmaScript.next should have some kind of generator iterator proposal, but this is not final.
You shouldn't rely on this showing up without changes in the next version of the language, and even if it doesn't change, it probably won't show up widely in other browsers for a while.
Overview
First-class coroutines, represented as objects encapsulating suspended execution contexts (i.e., function activations). Prior art: Python, Icon, Lua, Scheme, Smalltalk.
Examples
The “infinite” sequence of Fibonacci numbers (notwithstanding behavior around 253):
function* fibonacci() {
let [prev, curr] = [0, 1];
for (;;) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
Generators can be iterated over in loops:
for (n of fibonacci()) {
// truncate the sequence at 1000
if (n > 1000)
break;
print(n);
}
Generators are iterators:
let seq = fibonacci();
print(seq.next()); // 1
print(seq.next()); // 2
print(seq.next()); // 3
print(seq.next()); // 5
print(seq.next()); // 8
It's a Generator function.
Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.
Calling a generator function does not execute its body immediately; an iterator object for the function is returned instead. When the iterator's
next()
method is called, the generator function's body is executed until the firstyield
expression, which specifies the value to be returned from the iterator or, withyield*
, delegates to another generator function.
Historical note:
It's a proposed syntax for EcmaScript.next
.
Dave Herman of Mozilla gave a talk about EcmaScript.next. At 30:15 he talks about generators.
Earlier, he explains how Mozilla is experimentally implementing proposed language changes to help steer the committee. Dave works closely with Brendan Eich, Mozilla's CTO (I think), and the original JavaScript designer.
You can find more detail on the EcmaScript working group wiki: http://wiki.ecmascript.org/doku.php?id=harmony:generators
The working group (TC-39) has general agreement that EcmaScript.next should have some kind of generator iterator proposal, but this is not final.
You shouldn't rely on this showing up without changes in the next version of the language, and even if it doesn't change, it probably won't show up widely in other browsers for a while.
Overview
First-class coroutines, represented as objects encapsulating suspended execution contexts (i.e., function activations). Prior art: Python, Icon, Lua, Scheme, Smalltalk.
Examples
The “infinite” sequence of Fibonacci numbers (notwithstanding behavior around 253):
function* fibonacci() {
let [prev, curr] = [0, 1];
for (;;) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
Generators can be iterated over in loops:
for (n of fibonacci()) {
// truncate the sequence at 1000
if (n > 1000)
break;
print(n);
}
Generators are iterators:
let seq = fibonacci();
print(seq.next()); // 1
print(seq.next()); // 2
print(seq.next()); // 3
print(seq.next()); // 5
print(seq.next()); // 8
edited Jul 11 '18 at 13:36
answered Mar 8 '12 at 16:02
Mike SamuelMike Samuel
94.1k23174215
94.1k23174215
7
Follow up: what does a for loop with no parameters (for(;;)
) do? Why use it in this context?
– Fergie
Aug 28 '13 at 7:22
13
@Fergie,for(;;)
is the same aswhile (true)
. It's used in this context since the Fibonacci sequence is an unbounded sequence.
– Mike Samuel
Aug 28 '13 at 7:37
5
Prior art: C# yield?
– Dave Van den Eynde
Dec 19 '14 at 13:09
3
@DaveVandenEynde, prior prior art: Python yield. Prior prior prior art: CLU and Icon.
– Mike Samuel
Dec 19 '14 at 17:06
add a comment |
7
Follow up: what does a for loop with no parameters (for(;;)
) do? Why use it in this context?
– Fergie
Aug 28 '13 at 7:22
13
@Fergie,for(;;)
is the same aswhile (true)
. It's used in this context since the Fibonacci sequence is an unbounded sequence.
– Mike Samuel
Aug 28 '13 at 7:37
5
Prior art: C# yield?
– Dave Van den Eynde
Dec 19 '14 at 13:09
3
@DaveVandenEynde, prior prior art: Python yield. Prior prior prior art: CLU and Icon.
– Mike Samuel
Dec 19 '14 at 17:06
7
7
Follow up: what does a for loop with no parameters (
for(;;)
) do? Why use it in this context?– Fergie
Aug 28 '13 at 7:22
Follow up: what does a for loop with no parameters (
for(;;)
) do? Why use it in this context?– Fergie
Aug 28 '13 at 7:22
13
13
@Fergie,
for(;;)
is the same as while (true)
. It's used in this context since the Fibonacci sequence is an unbounded sequence.– Mike Samuel
Aug 28 '13 at 7:37
@Fergie,
for(;;)
is the same as while (true)
. It's used in this context since the Fibonacci sequence is an unbounded sequence.– Mike Samuel
Aug 28 '13 at 7:37
5
5
Prior art: C# yield?
– Dave Van den Eynde
Dec 19 '14 at 13:09
Prior art: C# yield?
– Dave Van den Eynde
Dec 19 '14 at 13:09
3
3
@DaveVandenEynde, prior prior art: Python yield. Prior prior prior art: CLU and Icon.
– Mike Samuel
Dec 19 '14 at 17:06
@DaveVandenEynde, prior prior art: Python yield. Prior prior prior art: CLU and Icon.
– Mike Samuel
Dec 19 '14 at 17:06
add a comment |
It's a generator function - and it said so in the page you cite, in the comment you replaced with "this is the interesting line"...
Basically it's a way to specify sequences programmatically so that they can be passed around and elements accessed by index without having to compute the entire sequence (possibly infinite in size) beforehand.
9
"accessed by index without having to compute the entire sequence" is possibly the most helpful bit of explanation about generators I've come across so far. I could see using this in an app, vs previously just understanding it theoretically.
– wes
Aug 6 '15 at 16:17
add a comment |
It's a generator function - and it said so in the page you cite, in the comment you replaced with "this is the interesting line"...
Basically it's a way to specify sequences programmatically so that they can be passed around and elements accessed by index without having to compute the entire sequence (possibly infinite in size) beforehand.
9
"accessed by index without having to compute the entire sequence" is possibly the most helpful bit of explanation about generators I've come across so far. I could see using this in an app, vs previously just understanding it theoretically.
– wes
Aug 6 '15 at 16:17
add a comment |
It's a generator function - and it said so in the page you cite, in the comment you replaced with "this is the interesting line"...
Basically it's a way to specify sequences programmatically so that they can be passed around and elements accessed by index without having to compute the entire sequence (possibly infinite in size) beforehand.
It's a generator function - and it said so in the page you cite, in the comment you replaced with "this is the interesting line"...
Basically it's a way to specify sequences programmatically so that they can be passed around and elements accessed by index without having to compute the entire sequence (possibly infinite in size) beforehand.
answered Mar 8 '12 at 16:08
Michael BorgwardtMichael Borgwardt
297k64429668
297k64429668
9
"accessed by index without having to compute the entire sequence" is possibly the most helpful bit of explanation about generators I've come across so far. I could see using this in an app, vs previously just understanding it theoretically.
– wes
Aug 6 '15 at 16:17
add a comment |
9
"accessed by index without having to compute the entire sequence" is possibly the most helpful bit of explanation about generators I've come across so far. I could see using this in an app, vs previously just understanding it theoretically.
– wes
Aug 6 '15 at 16:17
9
9
"accessed by index without having to compute the entire sequence" is possibly the most helpful bit of explanation about generators I've come across so far. I could see using this in an app, vs previously just understanding it theoretically.
– wes
Aug 6 '15 at 16:17
"accessed by index without having to compute the entire sequence" is possibly the most helpful bit of explanation about generators I've come across so far. I could see using this in an app, vs previously just understanding it theoretically.
– wes
Aug 6 '15 at 16:17
add a comment |
The function*
type looks like it acts as a generator function for processes that can be iterated.
C# has a feature like this using "yield return" see 1 and see 2
Essentially this returns each value one by one to whatever is iterating this function, which is why their use case shows it in a foreach style loop.
add a comment |
The function*
type looks like it acts as a generator function for processes that can be iterated.
C# has a feature like this using "yield return" see 1 and see 2
Essentially this returns each value one by one to whatever is iterating this function, which is why their use case shows it in a foreach style loop.
add a comment |
The function*
type looks like it acts as a generator function for processes that can be iterated.
C# has a feature like this using "yield return" see 1 and see 2
Essentially this returns each value one by one to whatever is iterating this function, which is why their use case shows it in a foreach style loop.
The function*
type looks like it acts as a generator function for processes that can be iterated.
C# has a feature like this using "yield return" see 1 and see 2
Essentially this returns each value one by one to whatever is iterating this function, which is why their use case shows it in a foreach style loop.
edited May 23 '17 at 11:47
Community♦
11
11
answered Mar 8 '12 at 16:04
invalidsyntaxinvalidsyntax
6261513
6261513
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9620586%2fwhat-is-function-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
The comment in the example is quite old now,
function*
syntax is supported in Firefox since v26: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…* . Older versions used a different syntax.– Nickolay
Sep 9 '14 at 22:16
33
Regarding Google, just search for "function star" or "function asterisk". That's how I found this question ;).
– trysis
Feb 2 '15 at 22:05
2
Looks like the
*
was stripped from the link from @Nickolay. Here's a link directly tofunction*
at MDN. Sure enough, "basic" support since v26.– ruffin
Apr 29 '15 at 17:16
Another MDN link (which, by the way, I found on the MDN page linked by OP): developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– BlueRaja - Danny Pflughoeft
Dec 1 '15 at 17:20
Another useful MDN link: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…*
– Logan
Jun 23 '17 at 5:46