seangenabe / starry

[DEPRECATED. Please direct efforts to the awesome `ix` library.] Modular functions for iterable objects
MIT License
4 stars 0 forks source link

any basic generators? #23

Open jmagaram opened 6 years ago

jmagaram commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

No. Unfold is like the inverse of fold. Take an initial state and from that generate a sequence, each time generating a new item to be emitted and the next state. Probably can do that with custom generators. Some good inspiration here too.

https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/collections.seq-module-%5Bfsharp%5D


From: Sean Genabe notifications@github.com Sent: Tuesday, May 29, 2018 4:35:07 AM To: seangenabe/starry Cc: Justin Magaram; Author Subject: Re: [seangenabe/starry] any basic generators? (#23)

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.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/seangenabe/starry/issues/23#issuecomment-392745111, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ANecS-fbB6GVLWfqiWfpfa-c8tZlA4jWks5t3TJrgaJpZM4T-Xvm.