functional.js
Advanced tools
Comparing version
@@ -30,3 +30,16 @@ var λ = (function () { | ||
λ.map = λ.curry(function (iterator, items) { | ||
var mapped = [], | ||
mapEach; | ||
if (!items || typeof (iterator) !== "function") { | ||
return; | ||
} | ||
mapEach = λ.each(function () { | ||
mapped.push(iterator.apply(null, arguments)); | ||
}); | ||
mapEach(items); | ||
return mapped; | ||
}); | ||
return λ; | ||
})(); |
{ | ||
"author": "Lee Crossley <leee@hotmail.co.uk> (http://ilee.co.uk)", | ||
"name": "functional.js", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"scripts": { | ||
@@ -6,0 +6,0 @@ "preinstall": "npm install -g grunt-cli; npm install -g jasmine-node", |
# functional.js (λ) | ||
functional.js is (go on, guess) a functional js library. It facilitates [currying](http://en.wikipedia.org/wiki/Currying) and [point-free / tacit](http://en.wikipedia.org/wiki/Tacit_programming) programming in JavaScript. | ||
[](https://travis-ci.org/leecrossley/functional-js) | ||
functional.js is (go on, guess) a functional js library. It facilitates [currying](http://en.wikipedia.org/wiki/Currying) and [point-free / tacit](http://en.wikipedia.org/wiki/Tacit_programming) programming in JavaScript. | ||
### Basic λ.curry example | ||
### λ.curry example | ||
```javascript | ||
@@ -35,2 +35,18 @@ var concatenate = λ.curry(function(word1, word2) { | ||
expect(result).toEqual(["f", "u", "n", "c"]); | ||
``` | ||
### Curried λ.map example | ||
```javascript | ||
var items = [1, 2, 3]; | ||
var doubleUp = function (number) { | ||
return number * 2; | ||
}; | ||
var doubleMap = λ.map(doubleUp); | ||
expect(typeof (doubleMap)).toEqual("function"); | ||
var result = doubleMap(items); | ||
expect(result).toEqual([2, 4, 6]); | ||
``` |
26
spec.js
@@ -80,2 +80,28 @@ describe("functional", function() { | ||
}); | ||
it("should be able to double numbers in an array using λ.map", function() { | ||
var items = [1, 2, 3]; | ||
var doubleUp = function (number) { | ||
return number * 2; | ||
}; | ||
var result = λ.map(doubleUp, items); | ||
expect(result).toEqual([2, 4, 6]); | ||
}); | ||
it("should be able to λ.curry λ.map", function() { | ||
var items = [1, 2, 3]; | ||
var doubleUp = function (number) { | ||
return number * 2; | ||
}; | ||
var doubleMap = λ.map(doubleUp); | ||
expect(typeof (doubleMap)).toEqual("function"); | ||
var result = doubleMap(items); | ||
expect(result).toEqual([2, 4, 6]); | ||
}); | ||
}); |
7206
23.45%140
27.27%51
45.71%