Giter Site home page Giter Site logo

partials-for-js's Introduction

Build Status

partials-for-js

Implements support for partial function application in JavaScript.

What are partial functions?

Many programming languages are only able to call a function with the same amount of parameters as the function has been defined with. Partial function application makes it possible to call a function with fewer parameters than it has defined. This call results in a partially applied function. The partially applied function accepts the remaining parameters. When no parameters remain, the original function implementation is called.

The example below demonstrates this concept:

# let f be a function that accepts three parameters and returns their cummulative value
f = (a, b, c) -> a + b + c

# let f1 be a function that accepts partial application for function f
f1 = partial f

# let ff1 be f1 to which one value has been applied
ff1 = f1 5

# ff1 is now a function of the form f(b, c), the two remaining parameters
ff2 = ff1 10

# ff2 is now a function of the form f(c)
result = ff2 20

# all parameters are collected and the original function f is called. 
# result now holds value 35.

In this example all parameters are applied one-by-one. It is however possible to apply more parameters at once.

Partials-for-js implements a function wrapper that collects all parameters of the original function. When all parameters are collected, the original function is called.

Examples

in CoffeeScript

partial = require 'partials-for-js'

add = partial (x, y) -> x + y
addFive = add 5

result = addFive 10
# result now holds value 15
result = addFive 8
# result now holds value 13

in plain JavaScript

var partial = require('partials-for-js');

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

var addFive = add(5);

var result = addFive(10);
// result now holds value 15
var result = addFive(8);
// result now holds value 13

When to use partial functions

TBD

How to install

TBD

partials-for-js's People

Contributors

jeroenpeeters avatar

Watchers

 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.