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

Pattern matching on sequences: Providing a lazy version of [head, ...tail] #141

Open
koraa opened this issue Nov 9, 2020 · 0 comments
Open
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed needs-api-design

Comments

@koraa
Copy link
Contributor

koraa commented Nov 9, 2020

Implement the standard pattern matching structure [head, ...tail] but using iterators…so with lazyness.

// Split the given sequence into head and tail
const pop = (seq) => {
  const i = iter(seq);
  const v = next(i);
  return [i, v];
};

const tryPop = ...

// Split the given sequence into an array and the rest of the iterator
const popn = (seq, headLen) => 
const tryPopn = (seq, headLen) => 

// Version of fold that will fail if the sequence has zero elements 
const foldl1 = (seq, fn) => {
  const [i, v] = pop(seq);
  return foldl(i, v, fn);
};
// Fold but without initial element; this will just use the default value in case the sequence is empty but always start folding with the first element from the sequence
const tryFoldl1 = (seq, default, fn) => {
  const None = Symbol();
  const [it, v] = tryPop(seq);
  return v === None ? default : foldl(it, v, fn);
};

const foldr1 = 
const tryFoldr1 = 
@koraa koraa added the enhancement New feature or request label Nov 9, 2020
@koraa koraa changed the title fold1, pop, popn, Pattern matching on sequences: Providing a lazy version of [head, ...tail] Apr 30, 2021
@koraa koraa added good first issue Good for newcomers help wanted Extra attention is needed needs-api-design labels Apr 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed needs-api-design
Projects
None yet
Development

No branches or pull requests

1 participant