Last active
March 14, 2021 07:13
-
-
Save raganwald/702d4a24b1bdff9f9c1789c85b1f6979 to your computer and use it in GitHub Desktop.
Revisions
-
raganwald revised this gist
May 15, 2016 . No changes.There are no files selected for viewing
-
raganwald revised this gist
May 15, 2016 . No changes.There are no files selected for viewing
-
raganwald revised this gist
May 15, 2016 . No changes.There are no files selected for viewing
-
raganwald revised this gist
May 15, 2016 . 1 changed file with 46 additions and 40 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -100,13 +100,23 @@ function * filterWith (fn, iterable) { } } function * slices (length, iterable) { const iterator = iterable[Symbol.iterator](); let buffer = [...take(length, iterator)]; if (buffer.length < length) return; let nextElementIndex = 0; while (true) { yield buffer; const { value, done } = iterator.next(); if (done) return; buffer = buffer.slice(1); buffer.push(value); } } @@ -120,26 +130,43 @@ const squares = mapWith((x) => x*x, from(1)); const odds = filterWith((x) => x % 2 === 1, from(1)); function * primes (numbers = from(2)) { while (true) { const { first, rest } = split(numbers); yield first; numbers = filterWith((n) => n % first !== 0, rest); } } const greetings = mapWith( (x, y) => `${x} ${y}`, ['hello', 'bonjour', 'hej'], ['frank', 'pascal', 'rognvoldr'] ); const fibonacci = sequence2(0, 1, (x, y) => x + y); function * fibonacci2 () { yield 0; yield 1; yield * mapWith( (x, y) => x + y, fibonacci2(), rest(fibonacci2()) ); } function * fibonacci3 () { yield 0; yield 1; yield * mapWith( ([x, y]) => x + y, slices(2, fibonacci3()) ); }; function * phi () { yield * rest( mapWith( @@ -150,32 +177,11 @@ function * phi () { ); } function * phi2 () { yield * rest( mapWith( ([x, y]) => x / y, slices(2, fibonacci3()) ) ); }; -
raganwald revised this gist
May 15, 2016 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -101,12 +101,12 @@ function * filterWith (fn, iterable) { } function * primes (numbers = from(2)) { while (true) { const { first, rest } = split(numbers); yield first; numbers = filterWith((n) => n % first !== 0, rest); } } -
raganwald revised this gist
May 15, 2016 . 1 changed file with 7 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -101,10 +101,13 @@ function * filterWith (fn, iterable) { } function * primes (numbers = from(2)) { while (true) { const { first, rest } = split(numbers); yield first; numbers = filterWith((n) => n % first !== 0, numbers); } } // __Examples__ -
raganwald revised this gist
May 15, 2016 . 1 changed file with 35 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -68,7 +68,6 @@ function * sequence2 (first, second, nextFn = (x, y) => y) { yield second; [first, second] = [second, nextFn(first, second)] } } function * mapWith (fn, ...iterables) { @@ -118,6 +117,10 @@ const squares = mapWith((x) => x*x, from(1)); const odds = filterWith((x) => x % 2 === 1, from(1)); const reciprocals = mapWith((n) => 1/n, phi()); const fib = sequence2(0, 1, (x, y) => x + y); const greetings = mapWith( (x, y) => `${x} ${y}`, ['hello', 'bonjour', 'hej'], @@ -142,4 +145,34 @@ function * phi () { fibonacci() ) ); } ///////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// console.log([...take(10, skit)]) console.log([...take(10, from(42))]) console.log([...take(10, sequence(2, (x) => x * 2))]) console.log([...take(10, fibonacci())]) console.log([...take(10, squares)]) console.log([...take(10, odds)]) console.log([...take(20, primes())]) console.log([...greetings]) console.log([...take(10, phi())]) const squares2 = mapWith((x) => x*x, from(1)); console.log([...take(10, reciprocals)]) console.log([...take(10, fibonacci2)]) -
raganwald revised this gist
May 14, 2016 . 1 changed file with 20 additions and 37 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -43,18 +43,31 @@ function * take (numberToTake, iterable) { } function * infiniteNumberOf (something) { while (true) { yield something; } } function * from (number, increment = 1) { while (true) { yield number; number = number + increment; } } function * sequence (value, nextFn = (x) => x) { while (true) { yield value; value = nextFn(value); } } function * sequence2 (first, second, nextFn = (x, y) => y) { yield first; while (true) { yield second; [first, second] = [second, nextFn(first, second)] } yield * join(first, sequence2(second, nextFn(first, second), nextFn)); } @@ -105,7 +118,7 @@ const squares = mapWith((x) => x*x, from(1)); const odds = filterWith((x) => x % 2 === 1, from(1)); const greetings = mapWith( (x, y) => `${x} ${y}`, ['hello', 'bonjour', 'hej'], ['frank', 'pascal', 'rognvoldr'] @@ -129,34 +142,4 @@ function * phi () { fibonacci() ) ); } -
raganwald revised this gist
May 14, 2016 . 1 changed file with 55 additions and 28 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -58,44 +58,41 @@ function * sequence2 (first, second, nextFn = (x, y) => y) { yield * join(first, sequence2(second, nextFn(first, second), nextFn)); } function * mapWith (fn, ...iterables) { const iterators = iterables.map((iterable) => iterable[Symbol.iterator]()); while (true) { const pairs = iterators.map((iterator) => iterator.next()); const values = pairs.map((pair) => pair.value); const dones = pairs.map((pair) => pair.done); if (dones.find((done) => done)) { return; } else { yield fn(...values); } } } function * filterWith (fn, iterable) { const iterator = iterable[Symbol.iterator](); while (true) { const { value, done } = iterator.next(); if (done) { return; } else if (fn(value)) { yield value; } } } function * primes (numbers = from(2)) { const { first, rest } = split(numbers); yield first; yield * filterWith((n) => n % first !== 0, primes(rest)); } // __Examples__ @@ -117,7 +114,7 @@ const greetings = zipWith( function * fibonacci () { yield 0; yield 1; yield * mapWith( (x, y) => x + y, fibonacci(), rest(fibonacci()) @@ -126,10 +123,40 @@ function * fibonacci () { function * phi () { yield * rest( mapWith( (x, y) => x / y, rest(fibonacci()), fibonacci() ) ); } ///////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// console.log([...take(10, skit)]) console.log([...take(10, from(42))]) console.log([...take(10, sequence(2, (x) => x * 2))]) console.log([...take(10, fibonacci())]) console.log([...take(10, squares)]) console.log([...take(10, odds)]) console.log([...take(20, primes())]) console.log([...greetings]) console.log([...take(10, phi())]) const squares2 = zipWith((x) => x*x, from(1)); const reciprocals = zipWith((n) => 1/n, phi()); console.log([...take(10, reciprocals)]) -
raganwald revised this gist
May 9, 2016 . 1 changed file with 40 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -81,20 +81,55 @@ function * filterWith (fn, iterable) { } } function * zipWith (fn, ...iterables) { const asSplits = iterables.map(split); if (asSplits.every((asSplit) => asSplit.hasOwnProperty('first'))) { const firsts = asSplits.map((asSplit) => asSplit.first); const rests = asSplits.map((asSplit) => asSplit.rest); yield * join(fn(...firsts), zipWith(fn, ...rests)); } } function * primes (numbers = from(2)) { const { first, rest } = split(numbers); yield * join(first, filterWith((n) => n % first !== 0, primes(rest))); } // __Examples__ const skit = infiniteNumberOf('spam'); const powersOfTwo = sequence(2, (x) => x * 2); const squares = mapWith((x) => x*x, from(1)); const odds = filterWith((x) => x % 2 === 1, from(1)); const greetings = zipWith( (x, y) => `${x} ${y}`, ['hello', 'bonjour', 'hej'], ['frank', 'pascal', 'rognvoldr'] ); function * fibonacci () { yield 0; yield 1; yield * zipWith( (x, y) => x + y, fibonacci(), rest(fibonacci()) ); } function * phi () { yield * rest( zipWith( (x, y) => x / y, rest(fibonacci()), fibonacci() ) ); } -
raganwald revised this gist
May 9, 2016 . 1 changed file with 10 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,12 +27,8 @@ function split (iterable) { } }; function * join (first, rest) { yield first; yield * rest; }; @@ -47,22 +43,19 @@ function * take (numberToTake, iterable) { } function * infiniteNumberOf (something) { yield * join(something, infiniteNumberOf(something)); } function * from (first, increment = 1) { yield * join(first, from(first + increment, increment)); } function * sequence (first, nextFn = (x) => x) { yield * join(first, sequence(nextFn(first), nextFn)); } function * sequence2 (first, second, nextFn = (x, y) => y) { yield * join(first, sequence2(second, nextFn(first, second), nextFn)); } function * mapWith (fn, iterable) { @@ -71,10 +64,7 @@ function * mapWith (fn, iterable) { if (asSplit.hasOwnProperty('first')) { const { first, rest } = asSplit; yield * join(fn(first),mapWith(fn, rest)); } } @@ -93,6 +83,8 @@ function * filterWith (fn, iterable) { // __Examples__ const skit = infiniteNumberOf('spam'); const powersOfTwo = sequence(2, (x) => x * 2); const fibonacci = sequence2(0, 1, (x, y) => x + y); @@ -104,5 +96,5 @@ const odds = filterWith((x) => x % 2 === 1, from(1)); function * primes (numbers = from(2)) { const { first, rest } = split(numbers); yield * join(first, filterWith((n) => n % first !== 0, primes(rest))); } -
raganwald revised this gist
May 9, 2016 . 1 changed file with 38 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,15 +18,21 @@ function * rest (iterable) { function split (iterable) { const iterator = iterable[Symbol.iterator](); const { done, value: first } = iterator.next(); if (done) { return { rest: [] }; } else { return { first, rest: iterator }; } }; function * join (unjoined) { const {first, rest} = unjoined; if (unjoined.hasOwnProperty('first')) { yield first; } yield * rest; }; @@ -41,34 +47,48 @@ function * take (numberToTake, iterable) { } function * infiniteNumberOf (something) { yield * join({ first: something, rest: infiniteNumberOf(something) }); } function * from (first, increment = 1) { yield * join({first, rest: from(first + increment, increment)}); } function * sequence (first, nextFn = (x) => x) { yield * join({first, rest: sequence(nextFn(first), nextFn)}); } function * sequence2 (first, second, nextFn = (x, y) => y) { yield * join({first, rest: sequence2(second, nextFn(first, second), nextFn)}); } function * mapWith (fn, iterable) { const asSplit = split(iterable); if (asSplit.hasOwnProperty('first')) { const { first, rest } = asSplit; yield * join({ first: fn(first), rest: mapWith(fn, rest) }); } } function * filterWith (fn, iterable) { const asSplit = split(iterable); if (asSplit.hasOwnProperty('first')) { const { first, rest } = asSplit; if (fn(first)) { yield first; } yield * filterWith(fn, rest); } } // __Examples__ @@ -84,6 +104,5 @@ const odds = filterWith((x) => x % 2 === 1, from(1)); function * primes (numbers = from(2)) { const { first, rest } = split(numbers); yield * join({first, rest: filterWith((n) => n % first !== 0, rest)}); } -
raganwald revised this gist
May 9, 2016 . 1 changed file with 6 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -57,31 +57,18 @@ function * sequence2 (first, second, nextFn = (x, y) => y) { } function * mapWith (fn, iterable) { const { first, rest } = split(iterable); yield * join(fn(first), mapWith(fn, rest)); } function * filterWith (fn, iterable) { const { first, rest } = split(iterable); if (fn(first)) { yield first; } yield * filterWith(fn, rest); } // __Examples__ -
raganwald revised this gist
May 9, 2016 . 1 changed file with 16 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -84,4 +84,19 @@ function * filterWith (fn, iterable) { } } // __Examples__ const powersOfTwo = sequence(2, (x) => x * 2); const fibonacci = sequence2(0, 1, (x, y) => x + y); const squares = mapWith((x) => x*x, from(1)); const odds = filterWith((x) => x % 2 === 1, from(1)); function * primes (numbers = from(2)) { const { first, rest } = split(numbers); yield * join(first, filterWith((n) => n % first !== 0, rest)); } -
raganwald created this gist
May 9, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,87 @@ function * just (...values) { yield * values; }; function first (iterable) { const iterator = iterable[Symbol.iterator](); const { done, value } = iterator.next(); if (!done) return value; }; function * rest (iterable) { const iterator = iterable[Symbol.iterator](); iterator.next() yield * iterator; }; function split (iterable) { const iterator = iterable[Symbol.iterator](); const { done, value: first } = iterator.next(); if (!done) { return { first, rest: iterator }; } }; function * join (first, rest) { yield first; yield * rest; }; function * take (numberToTake, iterable) { const iterator = iterable[Symbol.iterator](); for (let i = 0; i < numberToTake; ++i) { const { done, value } = iterator.next(); if (done) return; else yield value; } } function * infiniteNumberOf (something) { yield * join(something, infiniteNumberOf(something)); } function * from (first, increment = 1) { yield * join(first, from(first + increment, increment)); } function * sequence (first, nextFn = (x) => x) { yield * join(first, sequence(nextFn(first), nextFn)); } function * sequence2 (first, second, nextFn = (x, y) => y) { yield * join(first, sequence2(second, nextFn(first, second), nextFn)); } function * mapWith (fn, iterable) { const iterator = iterable[Symbol.iterator](); while (true) { const { done, value } = iterator.next(); if (done) { return; } else yield fn(value); } } function * filterWith (fn, iterable) { const iterator = iterable[Symbol.iterator](); while (true) { const { done, value } = iterator.next(); if (done) { return; } else if (fn(value)) { yield value; } } } // __Examples__