New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

lazychain

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lazychain

**[Document](http://falsandtru.github.io/lazychain/)** | **[js](https://github.com/falsandtru/lazychain/releases)** | **[d.ts](src/ts/.d/lazychain.d.ts)**


Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created

LazyChain

Document | js | d.ts

Build Status Coverage Status Dependency Status

FRP(Functional reactive programming) supporting DSL. Lazy stream, monad, pattern match, guard implements in JavaScript.

Data-Oriented Design by Pipeline

mainstream
[input]
  |     substream(async)
  | ----> |
  |       :     procstream
  |       | ----> |
  |       |       |
  |       | <---- |
  |       |
  |       |     ajax(async)
  |       | ----> |
  |       |       :
  |       | <---- |
  |       |
  |       |     worker(async)
  |       | ----> |
  |       |       :
  |       | <---- |
  |       |
  |       |     process
  |       | ----> |
  |       |       |
  |       | <---- |
  |       |
  | <---- |
  |
[output]
var streams = build({
  main: LazyChain(),
  sub: LazyChain(),
  proc: LazyChain()
});

streams.main.notify('');

function build(streams) {
  streams.proc
    .map(encodeURI);

  streams.sub
    .lazy()
    .stream(streams.proc)
    .lazy(req)
    .filter(isXHR)
    .map(conv);

  streams.main
    .stream(streams.sub)
    .stream(alert);

  return streams;
}

function req(v) {
  return $.ajax(v);
}

function isXHR(v) {
  return v instanceof Object;
}

function conv(xhr) {
  return xhr.responseText;
}
// Maybe
var Just = Number,
    Nothing = Error;
 
LazyChain<number>([-1, 0, 1, NaN])
  .pattern([
    Number, n => n > 0,
    n => Just(n)
  ], [
    Number,
    _ => Nothing()
  ], [
    void 0,
    _ => Nothing()
  ])
  .pattern([
    Just,
    n => Just(n * 100)
  ])
  .pattern([
    Just,
    n => console.log(Just, n) // 1
  ], [
    Nothing,
    e => console.log(Nothing, e)
  ]);

// Either
type Either = [number, number|typeof Number];
type EitherValue = [number, number];
var LEFT: Either =
      [1, Number],
    Left =
      (data: number) => <EitherValue>[LEFT[0], data],
    RIGHT: Either =
      [0, Number],
    Right =
      (data: number) => <EitherValue>[RIGHT[0], data],
    Either = {
      return: Right,
      bind: (m: EitherValue, f: typeof Right|typeof Left) => <EitherValue>f.apply(undefined, [m[1]]),
      fail: _ => Left(NaN)
    };
var monad =
  LazyChain<number>()
    .monad<Either>(Either, false)
    .monadic([
      RIGHT,
      data => Right(data + 1)
    ], [
      LEFT,
      data => Left(data)
    ]);
var stream =
  LazyChain<number>();
stream
  .monad<Either>(Either)
  .stream(monad)
  .monadic([
    RIGHT,
    function (data) {
      console.log(data); // 1
      return Right(data);
    }
  ]);
stream.notify(0);
// Asynchronous method chain
LazyChain([1, 2])
  .lazy(function (v, i, a, e, d) { setTimeout(function () { d.resolve(v * 2) }, 50) }, true)
  .map(function (v) { return v * 3 })
  .lazy(function (v, i, a, e, d) { setTimeout(function () { d.resolve(v * 10) }, 50) }, true)
  .reduce(function (r, v, i, a) { return r + v }, 0)
  .forEach(function (v) {
    console.log(v); // 180 = (1 * 2 * 3 * 10) + (2 * 2 * 3 * 10)
  });

LazyChain($.ajax(''))
  .filter(function (v) { return 'object' === typeof v })
  .forEach(function (xhr) {
    console.log(xhr); // XHR object
  });
LazyChain(true);
['./']
  .lazy(function (v) { return $.ajax(v) })
  .filter(function (v) { return 'object' === typeof v })
  .forEach(function (xhr) {
    console.log(xhr); // XHR object
  });
LazyChain(false);

// Stream generate and control
var a = LazyChain(),
    b = LazyChain();

LazyChain(a)
  .stream('number', b)
  .stream(function (v, i, a, e) {
    console.log(v); // 0
  });

a.notify(0, '');
b
  .stream(function (v, i, a, e) {
    console.log(typeof v); // string
  });

Extend

Underscore.js / Lo-Dash

LazyChain([1, 2, 3], _)
  .stream(function (v) {
    console.log(v); // 1, 2, 3
  })
  .difference([1, 3])
  .stream(function (v) {
    console.log(v); // 2
  });

Installation

npm i lazychain

Documentation

API

Spec

Sorry, japanese documents only. I welcome translation.

Browser

  • IE9+
  • Chrome
  • Firefox
  • Safari
  • Android
  • iOS

License

MIT License

FAQs

Package last updated on 21 Jul 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts