Git Product home page Git Product logo

fn-util's Introduction

fn-util

Yes, Yet another functional library toolkit. This is just a collection of functional programming tools that have been useful for me.

fn-util build status

bind

Wraps a given function within a bound function that applies a given this context and any optional left and right arguments. Like Function.bind, it provides context and argument binding to a function. It also provides right argument binding and has a more readable binding API.

Examples:

1.) this context binding:

GLOBAL.name="global scope";
GLOBAL.description="the default this context, harmless, but mostly unintented.";
// a simple robot object
var robot = {
    name : 'Robot',
    description : 'a killing machine',
    describeSelf: function(){
        console.log("Hello, my name is ", this.name, ", and I am ", this.description);
    }
};

// the robot describes itself
robot.describeSelf();

// the same thing
fn.bind(robot.describeSelf, robot)();

// the same thing
fn.bind(robot.describeSelf).ctx(robot)();

// binds to global scope by default
fn.bind(robot.describeSelf)();

/** Output:

Hello, my name is  Robot , and I am  a killing machine
Hello, my name is  Robot , and I am  a killing machine
Hello, my name is  Robot , and I am  a killing machine
Hello, my name is  global scope , and I am  the default this context, harmless, but mostly unintented.

**/

2.) Left argument binding

function command(who, what, act, upon){
    console.log(who + " " + what + ": " + act + " " + upon);
}

var simonSays = fn.bind(command).lbind("Simon", "says");
simonSays("Jump", "on one leg");
simonSays("Spin", "around");


/** Output:

Simon says: Jump on one leg
Simon says: Spin around

**/

3.) Right argument binding

function command(who, what, act, upon){
    console.log(who + " " + what + ": " + act + " " + upon);
}

var commandToJump = fn.bind(command).rbind("says", "Jump", "on one leg");
commandToJump("Simon");
commandToJump("Peter");
commandToJump("Martha");

/** Output:

Simon says: Jump on one leg
Peter says: Jump on one leg
Martha says: Jump on one leg

**/

curry

Makes a given function curriable. A curriable function of a given arity that collects their given arguments and return another curriable function, if the number of arguments is less than its arity, otherwise it runs its code.

Example:

function add(x, y){
    return x + y;
}

var plus = fn.curry(add, 2);

console.log( plus(5)(2) );
console.log([1,2,3,4,5].map(plus(5)));

/** Output:

7
[ 6, 7, 8, 9, 10 ]

**/

fn-util's People

Contributors

g-i-o- avatar

Watchers

James Cloos avatar  avatar

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.