X Tutup
The Wayback Machine - https://web.archive.org/web/20220321210445/https://github.com/nodejs/node/pull/41669
Skip to content
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

stream: add reduce #41669

Merged
Merged

Conversation

benjamingr
Copy link
Member

@benjamingr benjamingr commented Jan 23, 2022

This continues the work in #41630 so that one has to land before this one and only look at the last commit :) (same as last time, I want feedback :))

I have a few questions here about what the behaviour should be and I think my implementation may be simplified (I changed it as I added tests).

cc @ronag @mcollina @aduh95 @vweevers

@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented Jan 23, 2022

Review requested:

doc/api/stream.md Outdated Show resolved Hide resolved
doc/api/stream.md Outdated Show resolved Hide resolved
test/parallel/test-stream-reduce.js Outdated Show resolved Hide resolved
lib/internal/streams/operators.js Show resolved Hide resolved
doc/api/stream.md Outdated Show resolved Hide resolved
@@ -2118,6 +2118,45 @@ import { Readable } from 'stream';
await Readable.from([1, 2, 3, 4]).take(2).toArray(); // [1, 2]
```

### `readable.reduce(fn[, initial[, options])`
Copy link
Contributor

@vweevers vweevers Jan 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### `readable.reduce(fn[, initial[, options])`
### `readable.reduce(fn[, initial[, options]])`

To align with what the code currently supports. What if you want to reduce() with options but without an initial value?

Copy link
Member Author

@benjamingr benjamingr Jan 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would we differentiate an initial value not existing? An object containing a signal can be a valid initialValue for reduce

Copy link
Contributor

@vweevers vweevers Jan 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An ugly solution would be reduce(fn, undefined, options) and changing the spec from "initialValue is not present" to "initialValue is not undefined".

Copy link
Member Author

@benjamingr benjamingr Jan 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would also be a hard sell because it diverges from how Array.prototype.reduce behaves:

[1,2,3].reduce((p, c) => p + c); // 6
[1,2,3].reduce((p, c) => p + c, undefined); // NaN

Copy link
Member Author

@benjamingr benjamingr Jan 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devsnek thoughts?

Copy link
Member

@targos targos Jan 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever is decided, there is something to fix here because one of the opening brackets isn't closed.

Copy link
Contributor

@aduh95 aduh95 Jan 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@benjamingr benjamingr Jan 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, didn't realize this was blocking the review, fixed.

doc/api/stream.md Outdated Show resolved Hide resolved
doc/api/stream.md Outdated Show resolved Hide resolved
doc/api/stream.md Show resolved Hide resolved
@benjamingr benjamingr force-pushed the stream-iterator-helper-reduce branch 2 times, most recently from fb751fa to aafca2a Compare Jan 23, 2022
lib/internal/streams/operators.js Outdated Show resolved Hide resolved
@benjamingr benjamingr marked this pull request as ready for review Jan 24, 2022
@benjamingr
Copy link
Member Author

@benjamingr benjamingr commented Jan 24, 2022

Promoted from draft cc @ronag @mcollina PTAL

@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented Jan 25, 2022

@benjamingr benjamingr force-pushed the stream-iterator-helper-reduce branch 2 times, most recently from cb6672d to 9976118 Compare Jan 27, 2022
@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented Jan 27, 2022

@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented Jan 29, 2022

lib/internal/streams/operators.js Outdated Show resolved Hide resolved
benjamingr and others added 2 commits Jan 30, 2022
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
Copy link
Member

@mcollina mcollina left a comment

lgtm

@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented Jan 30, 2022

@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented Jan 30, 2022

@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented Jan 30, 2022

@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented Jan 30, 2022

Commit Queue failed
- Loading data for nodejs/node/pull/41669
✔  Done loading data for nodejs/node/pull/41669
----------------------------------- PR info ------------------------------------
Title      stream: add reduce (#41669)
Author     Benjamin Gruenbaum  (@benjamingr)
Branch     benjamingr:stream-iterator-helper-reduce -> nodejs:master
Labels     stream, needs-ci, commit-queue-squash
Commits    3
 - stream: add reduce
 - fixup! bad markup
 - fixup! Update lib/internal/streams/operators.js
Committers 2
 - Benjamin Gruenbaum 
 - GitHub 
PR-URL: https://github.com/nodejs/node/pull/41669
Reviewed-By: Matteo Collina 
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/41669
Reviewed-By: Matteo Collina 
--------------------------------------------------------------------------------
   ℹ  This PR was created on Sun, 23 Jan 2022 13:57:13 GMT
   ✔  Approvals: 1
   ✔  - Matteo Collina (@mcollina) (TSC): https://github.com/nodejs/node/pull/41669#pullrequestreview-867069404
   ✖  This PR needs to wait 2 more hours to land (or 0 hours if there is one more approval)
   ✖  GitHub CI is still running
   ℹ  Last Full PR CI on 2022-01-30T10:55:05Z: https://ci.nodejs.org/job/node-test-pull-request/42234/
- Querying data for job/node-test-pull-request/42234/
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu
https://github.com/nodejs/node/actions/runs/1768552118

@benjamingr
Copy link
Member Author

@benjamingr benjamingr commented Jan 30, 2022

ok let's wait for the GitHub ci

@nodejs-github-bot nodejs-github-bot merged commit d2ac192 into nodejs:master Jan 30, 2022
59 checks passed
@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented Jan 30, 2022

Landed in d2ac192

@ruyadorno
Copy link
Member

@ruyadorno ruyadorno commented Feb 7, 2022

this looks like a semver-minor so I'm adding the label for it to be picked up by the changelog, feel free to remove it in case that's not true.

Also should it be a notable-change @benjamingr ?

ruyadorno added a commit that referenced this issue Feb 8, 2022
PR-URL: #41669
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

10 participants
X Tutup