feat: use iterable object directly #113
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
BackGround
In C#, iterable objects can be iterated over using LINQ in the following way:
Here, users is an object of type
IEnumerable<T>
, meaning it implements theIEnumerator
interface.IEnumerable | MSDN
In JS/Ts, a similar concept exists for iterables.
For example,
Map
is an iterable object that can be iterated byfor...of
because it implements theIterable<T>
interface (Ts), i.e., it contains a[Symbol.iterator]
method.Feature
This pull request enhances the from function by enabling it to accept
Iterable<T>
objects.This PR aims to automatically invoke the
[Symbol.iterator]
method on objects that possess it to obtain an iterator, and then use this iterator to construct anEnumerable
.Key Changes
New:
obj
has a[Symbol.iterator]
method. if ture, treat it as an iterable object.Why
More Direct Usage: This change will accept iterable objects directly. For instance:
The above code will throw an error in
4.0.2
version becausemap
is an iterable object, but linq treats it as an iterator and accesses its next function directly:In
PR#110
, an empty array is provided. The reason is thatPR#110
expects an iterator rather than an iterable object. The following code will return the expected results:This PR is more dangerous than last time. The last PR actually targeted a function signature that was not correctly aligned with linq.d.ts and corrected it to be fully compliant.
This PR adds a new feature, enabling the
Enumerable.from
function to accept parameters of typeIterable<T>
. This is a completely new feature and may introduce new problems.Thank you very much to the author for pointing out the problem of modifying irrelevant code. I will try to avoid this behavior in subsequent PRs.
This PR also reverts iterator.js:14-22 with changes from an unrelated commit.