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

any basic generators? #23

Open
jmagaram opened this issue May 14, 2018 · 3 comments
Open

any basic generators? #23

jmagaram opened this issue May 14, 2018 · 3 comments

Comments

@jmagaram
Copy link

Does the library have any basic generators, like "the numbers from 1 to 1000"? This would be a nice addition. Check out F# Seq module. Some basic generators like...

unfold
init
initInfinite

...would go a long way. Here is an unfold implementation...

export function unfold<T, TState>(args: { seed: TState, generator: (state: TState) => ([T, TState] | undefined) }) {
    function* items() {
        let state: TState | undefined = args.seed;
        do {
            const next = args.generator(state);
            if (next !== undefined) {
                yield next[0];
                state = next[1];
            }
            else {
                state = undefined;
            }
        } while (state !== undefined)
    }
    return {
        [Symbol.iterator]: items
    }
}
@jmagaram
Copy link
Author

Actually I’m realizing now how easy it is to create an iterable using the function* syntax. So maybe there is not much need for unfold, range, init, repeat, and other ways of creating an iterable in your library.

@seangenabe
Copy link
Owner

Wouldn't unfold just be equivalent to a map function?

I think init/initInfinite would be covered with a function* generator function, yes.

Right now I'm just adding modules as I need them, but I'm open to ideas. I'm taking the .NET Framework and lodash as inspirations.

@jmagaram
Copy link
Author

jmagaram commented May 29, 2018 via email

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

No branches or pull requests

2 participants