function1

0.1.2 • Public • Published

function1.js Build Status NPM version

Function composition a la Scala's Function1.

Depending Upon

The project is available on the Node Packaged Modules registry. Add the dependency to your package.json file:

"dependencies": {
    "function1": "0.1.x"
}

Usage (CoffeeScript)

Create some functions:

add = (a, b) -> a + b
divide = (a, b) -> a / b
divideByFour = (a) -> a / 4
divideByTwo = (a) -> a / 2
multiply = (a, b) -> a * b
subtract = (a, b) -> a - b
square = (a) -> a * a
variadicAddTwo = -> y + 2 for y in arguments
variadicDivideByTwo = -> y / 2 for y in arguments

Compose some functions:

(square.andThen divideByTwo.andThen divideByFour)(10) # 12.5 
(square.compose divideByTwo.compose divideByFour)(10) # 1.5625 
 
(add.andThen square)(105) # 225 
(square.compose add)(105) # 225 
 
(divide.andThen square)(10010) # 100 
(square.compose divide)(10010) # 100 
 
(multiply.andThen square)(55) # 625 
(square.compose multiply)(55) #625 
 
(subtract.andThen square)(05) # 25 
(square.compose subtract)(05) # 25 
 
(variadicAddTwo.andThen variadicDivideByTwo)(106) # [6, 4] 
(variadicAddTwo.compose variadicDivideByTwo)(106) # [7, 5] 

License

The MIT License (MIT)

Copyright (c) 2013 Rocky Madden (http://rockymadden.com/)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Readme

Keywords

none

Package Sidebar

Install

npm i function1

Weekly Downloads

1

Version

0.1.2

License

none

Last publish

Collaborators

  • rockymadden