1.0.1 • Published 6 years ago

piecewise-defined v1.0.1

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

piecewise-defined function generator

Small module to easily define piecewise functions

const { Piecewise, PiecewiseGeneric } = require("piecewise-defined");

const pf = Piecewise()
  .add(x => x > 3 && x < 6, x => 2 * x)
  .add(x => x >= 6, x => x)
  .compile();

console.log(pf(4), pf(7), pf(3));

// prints out "8, 7, undefined"


const pfg = PiecewiseGeneric()
  .add(x => x.length < 3, x => x.reduce((a, b) => a+b, 0))
  .add(x => x.length >= 3, x => 0)
  .compile();

console.log(pfg([1,2]), pfg([1,2,3]));

// prints out "3 0"