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

Enhance operators with optional state #9

Open
andres-kovalev opened this issue Mar 24, 2020 · 0 comments
Open

Enhance operators with optional state #9

andres-kovalev opened this issue Mar 24, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@andres-kovalev
Copy link
Owner

It would be nice to hold some state between createFn calls. For instance, to be able to generate fibonacci sequence:

const next = last => {
    if (last.length < 2) {
      last.push(1);
      return 1;
    }

    const item = last.shift() + last[0];
    last.push(item);
    return item;
};

// before
const f = () => {
  const last = [];

  return () => next(last);
}

const fibonacciSequence = create(f(), 10); // ok
const fibonacciGenerator = create(f()); // not ok, will work only once

// after
const fibonacciGenerator = create((index, state) => {
  if (!state.last) state.last = [];

  return next(state.last);
})
@andres-kovalev andres-kovalev added the enhancement New feature or request label Mar 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant