Code Monkey home page Code Monkey logo

ts-lazy-collections's Introduction

Lazy collections with iterators for TypeScript

Read the article here for more context

Interface

Constructors

fromArray

Convers an array to a lazy collection.

fromFunction

Creates a sequence based of a function. The function will get the index and the previous value as a param and should return the next value. The last value is undefiend on the first call.

fromRange

Creates a sequence of numbers based on a min and max.

Collection methods

toArray

converts the collection to an array. Causes a full iteration of the complete data pipeline. Don't use this unless needed.

filter

Filter a collection based on a predicate. This is a lazy operation, no iteration will be caused.

map

Convert all items in the collection by applying a function. This is a lazy operation, no iteration will be caused.

first

Get the first item from the collection. This will only cause the pipeline to proccess the first item. No full iteration will occure. Collections are a stream, so you can only recieve every item once.

skip

Skips a given amount of items of the collection. This will only cause the pipeline to proccess the skipped items. No full iteration will occure.

take

Limit the size of the collection to a given number. This is a lazy operation, no iteration will be caused.

ts-lazy-collections's People

Contributors

wimjongeneel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

georgepu

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.