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

ES6 Iterable #65

Closed
xmedeko opened this issue Sep 9, 2019 · 6 comments
Closed

ES6 Iterable #65

xmedeko opened this issue Sep 9, 2019 · 6 comments

Comments

@xmedeko
Copy link

xmedeko commented Sep 9, 2019

As a former C# developer I like your library. Just want to know, any plans to use ES6 Iterable instead of custom Enumerable? If you want to support old browsers, would it be possible to have Enumerables to use Iterables under the hood for modern browsers? Also relates to #51

Thanks

@mihaifm
Copy link
Owner

mihaifm commented Sep 9, 2019

Iterable interface for the Enumerable object has been added in this pull request #62, in the sense that you can do for..of on an Enumerable object.

But I suppose you're looking for a way to initialize Enumerable from an Iterable object, as described in #51? I think it can be done. I'll see if I can add the feature, that issue kinda slipped below the radar.

@xmedeko
Copy link
Author

xmedeko commented Sep 9, 2019

Yes, one solution would be both Iterable and Enumerable to be easily interchangeable. I.e. implement #51 like Enumerable.from(someIterable()).where(...).

But I think, better solution would be to Enumerable be Iterable directly? I.e. change getEnumerator to [Symbol.iterator], moveNext() to next()? It would be more JavaScriptish and maybe more efficient. Also, this way the library would be more easy to adopt for JS guys without C# background. The old methods may stay for BC. The problem may be compatibility with the old browsers, but IMO it would need just define to some Symbol.iterator.

@mihaifm
Copy link
Owner

mihaifm commented Sep 11, 2019

This is now resolved: 540699b. Can you help verifying the fix?

You can now initialize the Enumerable from an iterable object, so the scenario from #51 should now work:

function* words() {
    yield "abc";
    yield "def";
}

console.log(Enumerable.from(words()).toArray()); // ["abc", "def"]

Symbol.iterator was available before, so Enumerable is also an Iterable object where the code requires. Now it works both ways (initialize from Iterable and become an Iterable):

for (var a of Enumerable.from(words())) {
    console.log(a);
}

The good part is that it doesn't use an array internally, so it works with infinte iterables...which is pretty sweet.

function* countToInfinity() {
    var index = 0;
    while (true)
        yield index++;
}

Enumerable.from(countToInfinity()).where(v => v%2 == 0).forEach(x => console.log(x));

This will display all even numbers to infinity.

@xmedeko
Copy link
Author

xmedeko commented Sep 12, 2019

Yeah, it works perfect, thanks.

Just you can use Utils.hasNativeIteratorSupport() in Enumerable.from:

            // iterable object
            if (Utils.hasNativeIteratorSupport) {
            // orig code: if (typeof Symbol !== 'undefined' && typeof obj[Symbol.iterator] !== 'undefined') {

@mihaifm
Copy link
Owner

mihaifm commented Sep 12, 2019

Cool. Not quite the same, hasNativeIteratorSupport checks the global Symbol object, basically a feature check for the browser/node. The new code checks the object we're initializing from, making sure it's iterable.

I'll publish to npm later today, thanks for helping out with this.

@mihaifm mihaifm closed this as completed Sep 12, 2019
@xmedeko
Copy link
Author

xmedeko commented Sep 12, 2019

Ah, I see, thanks for explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants