Coder Social home page Coder Social logo

wimjongeneel / ts-lazy-collections Goto Github PK

View Code? Open in Web Editor NEW
25.0 3.0 1.0 4 KB

Lazy collections with iterators for TypeScript

Home Page: https://itnext.io/fast-pipelines-with-generators-in-typescript-85d285ae6f51?source=friends_link&sk=e402495574e928a6864d614748df17d2

TypeScript 100.00%
typescript lazy-evaluation generators collections

ts-lazy-collections's Issues

Why not `for of`?

Would defining the functions like this instead impact performance?

function* map(it, fn) {
  for (const x of it) yield fn(x);
}
function* take(it, n) {
  for (let i = 0; i < n; i++) yield it.next().value;
}
function* skip(it, n) {
  for (let i = 0; i < n; i++) it.next();
  for (const x of it) yield x;
}
function first(it) {
  return it.next().value;
}
function* filter(it, p) {
  for (const x of it) if (p(x)) yield fn(x);
}
function* from_range(s, e = Infinity) {
  for (let i = s; i < e; i++) yield i;
}
function* from_fn(fn, s) {
  let i = 0;
  while (true) yield (s = fn(i++, s));
}
function reduce(it, fn, acc) {
  for (const x of it) acc = fn(acc, x);
  return acc;
}
function* chain(...its) {
  for (const it of its) for (const x of it) yield x;
}
const pipe = (...fns) => (x) => {
  for (const fn of fns) x = fn(x);
  return x;
};
const c = (fn) => (...args) => (x) => fn(x, ...args);

const line = pipe(
  c(from_fn)(),
  c(map)((x) => (x * 100) | 0),
  c(take)(3),
  c(chain)(["arrays", "are", "iterators"]),
  c(map)(String),
  c(reduce)((a, b) => a + b, ""),
  c(map)((x) => x + x + x + x + "\n") // strings are iterators
);
console.log(...line(Math.random));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.