Coder Social home page Coder Social logo

es-iter's People

Contributors

abozhilov avatar asenmelon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

funcodingpanda

es-iter's Issues

More compact version of range()?

How about the following changes?

    // Current
    static range (start, end, step) {
        return new Iter(function* () {
            let s = toInteger(start),
                e = toInteger(end);

            if (typeof end == 'undefined') {
                e = s;
                s = 0;
            }

            let k = toInteger(step) || (s < e ? 1 : -1)

            if (k > 0) {
                while (s < e) {
                    yield s;
                    s += k;
                }
            }
            else {
                while (s > e) {
                    yield s;
                    s += k;
                }        
            }
        });
    }

    // Proposed
    static * range(start, end, step=undefined) {
        // step = undefined signals that `step` is optional
        if (start === undefined || end === undefined) {
            throw new Error('Need at least two arguments');
        }
        start = toInteger(start);
        end = toInteger(end);
        step = toInteger(step) || (start < end ? 1 : -1);
        if (step >= 0) {
            while (start < end) {
                yield start;
                start += step;
            }
        }
        else {
            while (start > end) {
                yield start;
                start += step;
            }
        }
    }

Provide collect()

Provide polymorphic collect() instead of bunch of the fns (toArray, toString, toObject etc).

Api (can easily be described in ts or flow):

  new Iter(...).collect(Array);
  new Iter(...).collect(String);
  new Iter(...).collect(Map);
  new Iter(...).collect(Set);

Implementation:

collect(Class) {
    const iter = Iter.getIterator(this);

    // Array and subclasses.
    if (Class === Array || Class.prototype instanceof Array) {
        return Class.from(iter);
    }

    if (Class === Object) {
        const result = {};

        for (const [key, value] of iter) {
            result[key] = value;
        }

        return result;
    }

    if (Class === String) {
        return this.collect(Array).join('');
    }

    // Map, Set, WeakMap, WeakSet and custom types.
    return new Class(iter);
}

Use TypeScript/Flow notation

For documenting, I prefer:

foo(start, end, step?)

Instead of:

foo(start, end[, step])

This is what I’d do myself, but, incidentally, it is also what TypeScript and Flow use. I generally find their notation easy to understand.

Merge range() and count()?

Have you considered giving range() the following signature?

Iter.range(start=0, end=Infinity, step=1)

That way, you wouldn’t need count(), right?

I also find it more intuitive if Iter.range(10) starts at 10, instead of it being the upper boundary (similarly to how Array.prototype.slice() etc. work).

NodeJS compatibility

I saw your lib published on npm, and was going to use it but sadly it requires transpilation because of its usage of modules. You should really publish a node-compatible version on npm. My advice: transpile it with rollup in a pre-publish hook, change the main entry and set esnext:main to src/Iter.js in the package.json (doc).

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.