Git Product home page Git Product logo

Comments (6)

mihaifm avatar mihaifm commented on September 26, 2024

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.

from linq.

xmedeko avatar xmedeko commented on September 26, 2024

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.

from linq.

mihaifm avatar mihaifm commented on September 26, 2024

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.

from linq.

xmedeko avatar xmedeko commented on September 26, 2024

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') {

from linq.

mihaifm avatar mihaifm commented on September 26, 2024

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.

from linq.

xmedeko avatar xmedeko commented on September 26, 2024

Ah, I see, thanks for explanation.

from linq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.