Reduce a sequence of items provided by a generator in JavaScript
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Say I have a sequence of items and I want to perform a reduce operation via myReducer
function (whatever it is). If my items are in an array (say myArray
), it's easy:
myArray.reduce(myReducer);
What if, however, my sequence is quite large and I don't want to allocate an array of all of it, only to immediately reduce it item after item? I can create a generator function for my sequence, that part is clear. Is there a straightforward way of how to then perform the reduction? I mean apart from writing the reduce functionality for a generator myself.
javascript generator reduce
add a comment |
Say I have a sequence of items and I want to perform a reduce operation via myReducer
function (whatever it is). If my items are in an array (say myArray
), it's easy:
myArray.reduce(myReducer);
What if, however, my sequence is quite large and I don't want to allocate an array of all of it, only to immediately reduce it item after item? I can create a generator function for my sequence, that part is clear. Is there a straightforward way of how to then perform the reduction? I mean apart from writing the reduce functionality for a generator myself.
javascript generator reduce
You need to create a lazy evaluation array object/class with a lazy evaluation reduce.
– Dominique Fortin
Nov 23 '18 at 16:26
add a comment |
Say I have a sequence of items and I want to perform a reduce operation via myReducer
function (whatever it is). If my items are in an array (say myArray
), it's easy:
myArray.reduce(myReducer);
What if, however, my sequence is quite large and I don't want to allocate an array of all of it, only to immediately reduce it item after item? I can create a generator function for my sequence, that part is clear. Is there a straightforward way of how to then perform the reduction? I mean apart from writing the reduce functionality for a generator myself.
javascript generator reduce
Say I have a sequence of items and I want to perform a reduce operation via myReducer
function (whatever it is). If my items are in an array (say myArray
), it's easy:
myArray.reduce(myReducer);
What if, however, my sequence is quite large and I don't want to allocate an array of all of it, only to immediately reduce it item after item? I can create a generator function for my sequence, that part is clear. Is there a straightforward way of how to then perform the reduction? I mean apart from writing the reduce functionality for a generator myself.
javascript generator reduce
javascript generator reduce
asked Nov 23 '18 at 16:19
OndraOndra
556
556
You need to create a lazy evaluation array object/class with a lazy evaluation reduce.
– Dominique Fortin
Nov 23 '18 at 16:26
add a comment |
You need to create a lazy evaluation array object/class with a lazy evaluation reduce.
– Dominique Fortin
Nov 23 '18 at 16:26
You need to create a lazy evaluation array object/class with a lazy evaluation reduce.
– Dominique Fortin
Nov 23 '18 at 16:26
You need to create a lazy evaluation array object/class with a lazy evaluation reduce.
– Dominique Fortin
Nov 23 '18 at 16:26
add a comment |
1 Answer
1
active
oldest
votes
For now, ECMA-Script standard provides functions like reduce
for arrays, so you're out of luck: you need to implement your own reduce
for iterables:
const reduce = (f, i, it) => {
let o = i
for (let x of it)
o = f (o, x)
return o
}
const xs = [1, 2, 3]
const xs_ = {
[Symbol.iterator]: function* () {
yield 1
yield 2
yield 3
}
}
const output1 = reduce ((o, x) => o + x, 10, xs)
const output2 = reduce ((o, x) => o + x, 10, xs_)
console.log ('output1:', output1)
console.log ('output2:', output2)
What's the problem with my answer? The answer is there's no current native way of reducing, mapping, filtering, ... iterables.
– Matías Fidemraizer
Nov 23 '18 at 16:50
it´s a nice solution! +1
– Luke
Nov 23 '18 at 16:56
@MatíasFidemraizer: Thanks, that's what I thought. I'm just not as up-to-date with the current state of EcmaScript, so I asked to be sure that I'm not reinventing the wheel.
– Ondra
Nov 23 '18 at 16:58
I tried myself the Symbol.iterator way, that is giving you what you wanted, unfortunatelly I was not able to transform it so it would work with the native reduce.
– Luke
Nov 23 '18 at 17:30
@MatíasFidemraizer I don't see any difference withxs.reduce((o, x) => (o + x), 10)
or withArray.prototype.call(xs, (o, x) => (o + x), 10)
if it is array like. That doesn't seem to answer the question.
– Dominique Fortin
Nov 23 '18 at 17:35
|
show 5 more comments
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%2f53450002%2freduce-a-sequence-of-items-provided-by-a-generator-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
For now, ECMA-Script standard provides functions like reduce
for arrays, so you're out of luck: you need to implement your own reduce
for iterables:
const reduce = (f, i, it) => {
let o = i
for (let x of it)
o = f (o, x)
return o
}
const xs = [1, 2, 3]
const xs_ = {
[Symbol.iterator]: function* () {
yield 1
yield 2
yield 3
}
}
const output1 = reduce ((o, x) => o + x, 10, xs)
const output2 = reduce ((o, x) => o + x, 10, xs_)
console.log ('output1:', output1)
console.log ('output2:', output2)
What's the problem with my answer? The answer is there's no current native way of reducing, mapping, filtering, ... iterables.
– Matías Fidemraizer
Nov 23 '18 at 16:50
it´s a nice solution! +1
– Luke
Nov 23 '18 at 16:56
@MatíasFidemraizer: Thanks, that's what I thought. I'm just not as up-to-date with the current state of EcmaScript, so I asked to be sure that I'm not reinventing the wheel.
– Ondra
Nov 23 '18 at 16:58
I tried myself the Symbol.iterator way, that is giving you what you wanted, unfortunatelly I was not able to transform it so it would work with the native reduce.
– Luke
Nov 23 '18 at 17:30
@MatíasFidemraizer I don't see any difference withxs.reduce((o, x) => (o + x), 10)
or withArray.prototype.call(xs, (o, x) => (o + x), 10)
if it is array like. That doesn't seem to answer the question.
– Dominique Fortin
Nov 23 '18 at 17:35
|
show 5 more comments
For now, ECMA-Script standard provides functions like reduce
for arrays, so you're out of luck: you need to implement your own reduce
for iterables:
const reduce = (f, i, it) => {
let o = i
for (let x of it)
o = f (o, x)
return o
}
const xs = [1, 2, 3]
const xs_ = {
[Symbol.iterator]: function* () {
yield 1
yield 2
yield 3
}
}
const output1 = reduce ((o, x) => o + x, 10, xs)
const output2 = reduce ((o, x) => o + x, 10, xs_)
console.log ('output1:', output1)
console.log ('output2:', output2)
What's the problem with my answer? The answer is there's no current native way of reducing, mapping, filtering, ... iterables.
– Matías Fidemraizer
Nov 23 '18 at 16:50
it´s a nice solution! +1
– Luke
Nov 23 '18 at 16:56
@MatíasFidemraizer: Thanks, that's what I thought. I'm just not as up-to-date with the current state of EcmaScript, so I asked to be sure that I'm not reinventing the wheel.
– Ondra
Nov 23 '18 at 16:58
I tried myself the Symbol.iterator way, that is giving you what you wanted, unfortunatelly I was not able to transform it so it would work with the native reduce.
– Luke
Nov 23 '18 at 17:30
@MatíasFidemraizer I don't see any difference withxs.reduce((o, x) => (o + x), 10)
or withArray.prototype.call(xs, (o, x) => (o + x), 10)
if it is array like. That doesn't seem to answer the question.
– Dominique Fortin
Nov 23 '18 at 17:35
|
show 5 more comments
For now, ECMA-Script standard provides functions like reduce
for arrays, so you're out of luck: you need to implement your own reduce
for iterables:
const reduce = (f, i, it) => {
let o = i
for (let x of it)
o = f (o, x)
return o
}
const xs = [1, 2, 3]
const xs_ = {
[Symbol.iterator]: function* () {
yield 1
yield 2
yield 3
}
}
const output1 = reduce ((o, x) => o + x, 10, xs)
const output2 = reduce ((o, x) => o + x, 10, xs_)
console.log ('output1:', output1)
console.log ('output2:', output2)
For now, ECMA-Script standard provides functions like reduce
for arrays, so you're out of luck: you need to implement your own reduce
for iterables:
const reduce = (f, i, it) => {
let o = i
for (let x of it)
o = f (o, x)
return o
}
const xs = [1, 2, 3]
const xs_ = {
[Symbol.iterator]: function* () {
yield 1
yield 2
yield 3
}
}
const output1 = reduce ((o, x) => o + x, 10, xs)
const output2 = reduce ((o, x) => o + x, 10, xs_)
console.log ('output1:', output1)
console.log ('output2:', output2)
const reduce = (f, i, it) => {
let o = i
for (let x of it)
o = f (o, x)
return o
}
const xs = [1, 2, 3]
const xs_ = {
[Symbol.iterator]: function* () {
yield 1
yield 2
yield 3
}
}
const output1 = reduce ((o, x) => o + x, 10, xs)
const output2 = reduce ((o, x) => o + x, 10, xs_)
console.log ('output1:', output1)
console.log ('output2:', output2)
const reduce = (f, i, it) => {
let o = i
for (let x of it)
o = f (o, x)
return o
}
const xs = [1, 2, 3]
const xs_ = {
[Symbol.iterator]: function* () {
yield 1
yield 2
yield 3
}
}
const output1 = reduce ((o, x) => o + x, 10, xs)
const output2 = reduce ((o, x) => o + x, 10, xs_)
console.log ('output1:', output1)
console.log ('output2:', output2)
edited Nov 23 '18 at 17:47
answered Nov 23 '18 at 16:29
Matías FidemraizerMatías Fidemraizer
49.8k1285149
49.8k1285149
What's the problem with my answer? The answer is there's no current native way of reducing, mapping, filtering, ... iterables.
– Matías Fidemraizer
Nov 23 '18 at 16:50
it´s a nice solution! +1
– Luke
Nov 23 '18 at 16:56
@MatíasFidemraizer: Thanks, that's what I thought. I'm just not as up-to-date with the current state of EcmaScript, so I asked to be sure that I'm not reinventing the wheel.
– Ondra
Nov 23 '18 at 16:58
I tried myself the Symbol.iterator way, that is giving you what you wanted, unfortunatelly I was not able to transform it so it would work with the native reduce.
– Luke
Nov 23 '18 at 17:30
@MatíasFidemraizer I don't see any difference withxs.reduce((o, x) => (o + x), 10)
or withArray.prototype.call(xs, (o, x) => (o + x), 10)
if it is array like. That doesn't seem to answer the question.
– Dominique Fortin
Nov 23 '18 at 17:35
|
show 5 more comments
What's the problem with my answer? The answer is there's no current native way of reducing, mapping, filtering, ... iterables.
– Matías Fidemraizer
Nov 23 '18 at 16:50
it´s a nice solution! +1
– Luke
Nov 23 '18 at 16:56
@MatíasFidemraizer: Thanks, that's what I thought. I'm just not as up-to-date with the current state of EcmaScript, so I asked to be sure that I'm not reinventing the wheel.
– Ondra
Nov 23 '18 at 16:58
I tried myself the Symbol.iterator way, that is giving you what you wanted, unfortunatelly I was not able to transform it so it would work with the native reduce.
– Luke
Nov 23 '18 at 17:30
@MatíasFidemraizer I don't see any difference withxs.reduce((o, x) => (o + x), 10)
or withArray.prototype.call(xs, (o, x) => (o + x), 10)
if it is array like. That doesn't seem to answer the question.
– Dominique Fortin
Nov 23 '18 at 17:35
What's the problem with my answer? The answer is there's no current native way of reducing, mapping, filtering, ... iterables.
– Matías Fidemraizer
Nov 23 '18 at 16:50
What's the problem with my answer? The answer is there's no current native way of reducing, mapping, filtering, ... iterables.
– Matías Fidemraizer
Nov 23 '18 at 16:50
it´s a nice solution! +1
– Luke
Nov 23 '18 at 16:56
it´s a nice solution! +1
– Luke
Nov 23 '18 at 16:56
@MatíasFidemraizer: Thanks, that's what I thought. I'm just not as up-to-date with the current state of EcmaScript, so I asked to be sure that I'm not reinventing the wheel.
– Ondra
Nov 23 '18 at 16:58
@MatíasFidemraizer: Thanks, that's what I thought. I'm just not as up-to-date with the current state of EcmaScript, so I asked to be sure that I'm not reinventing the wheel.
– Ondra
Nov 23 '18 at 16:58
I tried myself the Symbol.iterator way, that is giving you what you wanted, unfortunatelly I was not able to transform it so it would work with the native reduce.
– Luke
Nov 23 '18 at 17:30
I tried myself the Symbol.iterator way, that is giving you what you wanted, unfortunatelly I was not able to transform it so it would work with the native reduce.
– Luke
Nov 23 '18 at 17:30
@MatíasFidemraizer I don't see any difference with
xs.reduce((o, x) => (o + x), 10)
or with Array.prototype.call(xs, (o, x) => (o + x), 10)
if it is array like. That doesn't seem to answer the question.– Dominique Fortin
Nov 23 '18 at 17:35
@MatíasFidemraizer I don't see any difference with
xs.reduce((o, x) => (o + x), 10)
or with Array.prototype.call(xs, (o, x) => (o + x), 10)
if it is array like. That doesn't seem to answer the question.– Dominique Fortin
Nov 23 '18 at 17:35
|
show 5 more comments
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%2f53450002%2freduce-a-sequence-of-items-provided-by-a-generator-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
You need to create a lazy evaluation array object/class with a lazy evaluation reduce.
– Dominique Fortin
Nov 23 '18 at 16:26