1.0.5 • Published 8 years ago

function-lift v1.0.5

Weekly downloads
8
License
Apache-2.0
Repository
github
Last release
8 years ago

function-lift

Lifts functions into the given context: promise, list, maybe, etc.

Motivation

This tool is heavily inspired by Haskell and the functional programming concept. The main idea of lifting is to abstract away the application control flow in some well-known patterns: .then() in case of promises, for example, or if (x !== undefined)). This ability makes code clean, pure and far more reusable.

Features

  • Allows functions to operate on monadic values
  • Supports any function arity

Installation

npm install function-lift

Usage

var lift = require('function-lift');

function promiseUnit(x) {
    ...
}

function bindToPromise(func) {
    ...
}

var promisify = lift(promiseUnit, bindToPromise);

var multiply = promisify(function (number, multiplier) {
    return number * multiplier;
});

var log = promisify(console.log);

// Now we can write asynchronous code in synchronous manner
log(multiply(Promise.resolve(400), Promise.resolve(5)));

// Or mix synchronous and asynchronous values without using any callbacks or .then()
log(multiply(100, 3));
log(multiply(Promise.resolve(200), 4));
log(multiply(300, Promise.resolve(10)));

// I always wanted to do this:
var myNumber = $.get('http://my.end.point/data');
var result = multiply(myNumber, 5);

log(result)
1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago