functional-lite
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -5,3 +5,3 @@ | ||
/* | ||
* @version 0.0.5 | ||
* @version 0.1.0 | ||
* @author Lauri Rooden - https://github.com/litejs/fn-lite | ||
@@ -16,2 +16,6 @@ * @license MIT License - http://lauri.rooden.ee/mit-license.txt | ||
function This() { return this } | ||
function Init() { | ||
var self = this | ||
return self.init && self.init.apply(self, arguments) || self | ||
} | ||
@@ -232,3 +236,7 @@ !function(root) { | ||
F.fn = This | ||
S.fn = function() { | ||
return Fn(this) | ||
} | ||
@@ -241,3 +249,3 @@ /* | ||
function Fn(expr) { | ||
if (fns[expr]) return fns[expr] | ||
if (Fn[expr]) return Fn[expr] | ||
var args = "_" | ||
@@ -252,18 +260,7 @@ , body = expr | ||
} | ||
return fns[expr] = new Function(args, "return(" + body + ")") | ||
return Fn[expr] = new Function(args, "return(" + body + ")") | ||
} | ||
root.Fn = Fn | ||
Fn.Init = function() { | ||
var self = this | ||
return self.init && self.init.apply(self, arguments) || self | ||
} | ||
F.fn = This | ||
S.fn = function() { | ||
return Fn(this) | ||
} | ||
}(this) | ||
@@ -273,238 +270,1 @@ | ||
/** Tests for Array | ||
!function(){ | ||
var test = new TestCase("Array extensions") | ||
, arr = [1,2,3,4,2,5] | ||
, res | ||
test.compare( | ||
[1,2,3,2,1].remove(2).join(), "1,3,1" | ||
, ['1',2,3,2,1].remove(2,1).join(), "1,3" | ||
, "Array.remove()"); | ||
var sort = function(a,b){return a-b}; | ||
test.compare( | ||
Array.indexFor([1,3,5], 2), 3 | ||
, Array.indexFor([1,3,5], 0, sort), 0 | ||
, Array.indexFor([1,3,5], 1, sort), 1 | ||
, Array.indexFor([1,3,5], 2, sort), 1 | ||
, Array.indexFor([1,3,5], 3, sort), 2 | ||
, Array.indexFor([1,3,5], 4, sort), 2 | ||
, Array.indexFor([1,3,5], 5, sort), 3 | ||
, Array.indexFor([1,3,5], 6, sort), 3 | ||
, "Array.indexFor()"); | ||
test.compare( | ||
Array.isArray([1]) | ||
, true | ||
, Array.isArray(1) | ||
, false | ||
, Array.isArray(arguments) | ||
, false | ||
, Array.isArray({a:1}) | ||
, false | ||
, "Array.isArray"); | ||
test.done(); | ||
}() | ||
//*/ | ||
/** Tests for Function | ||
!function(){ | ||
function Fn1(){ | ||
var t = this; | ||
t.a = 1; | ||
"init" in t && t.init.apply(t,arguments); | ||
return t; | ||
} | ||
Fn1.prototype = { | ||
init: function(){ | ||
this.b = 1; | ||
}, | ||
c:1 | ||
} | ||
var test = new TestCase("Function") | ||
, Fn2 = Fn1.extend({d:1}) | ||
, Fn3 = Fn2.extend({init:function(){},e:1}) | ||
, Fn4 = Fn3.extend({init:function(){Fn2.prototype.init.call(this);},f:1}) | ||
, f1 = new Fn1() | ||
, f2 = new Fn2() | ||
, f3 = new Fn3() | ||
, f4 = new Fn4() | ||
, run = 0 | ||
, actual = 0 | ||
, fn = function(i) { | ||
actual++; | ||
return i*i; | ||
}.cache() | ||
, fn2 = fn.origin.cache(true); | ||
test.compare( | ||
"a" in f1, true | ||
, "b" in f1, true | ||
, "c" in f1, true | ||
, "d" in f1, false | ||
, "e" in f1, false | ||
, "f" in f1, false | ||
, "a" in f2, true | ||
, "b" in f2, true | ||
, "c" in f2, true | ||
, "d" in f2, true | ||
, "e" in f2, false | ||
, "f" in f2, false | ||
, "a" in f3, true | ||
, "b" in f3, false | ||
, "c" in f3, true | ||
, "d" in f3, true | ||
, "e" in f3, true | ||
, "f" in f3, false | ||
, "a" in f4, true | ||
, "b" in f4, true | ||
, "c" in f4, true | ||
, "d" in f4, true | ||
, "e" in f4, true | ||
, "f" in f4, true | ||
, f1 instanceof Fn1, true | ||
, f1 instanceof Fn2, false | ||
, f1 instanceof Fn3, false | ||
, f1 instanceof Fn4, false | ||
, f2 instanceof Fn1, true | ||
, f2 instanceof Fn2, true | ||
, f2 instanceof Fn3, false | ||
, f2 instanceof Fn4, false | ||
, f3 instanceof Fn1, true | ||
, f3 instanceof Fn2, true | ||
, f3 instanceof Fn3, true | ||
, f3 instanceof Fn4, false | ||
, f4 instanceof Fn1, true | ||
, f4 instanceof Fn2, true | ||
, f4 instanceof Fn3, true | ||
, f4 instanceof Fn4, true | ||
, "Function.extend()"); | ||
test.compare( | ||
fn(2), 4, ++run, actual | ||
, fn(2), 4, run, actual | ||
, fn(3), 9, ++run, actual | ||
, fn(3), 9, run, actual | ||
, fn(3,1), 9, ++run, actual | ||
, fn(3,1), 9, run, actual | ||
, "Function.cache() function results"); | ||
test.compare( | ||
new fn(), new fn(), ++run, actual | ||
, new fn(1, 2), new fn(1, 2), ++run, actual | ||
, "Function.cache() instances"); | ||
test.compare( | ||
fn2(), new fn2(), ++run, actual | ||
, new fn2(1, 2), fn2(1, 2), ++run, actual | ||
, "Function.cache() and create instance"); | ||
test.done(); | ||
}() | ||
//*/ | ||
/** Tests for String extensions | ||
!function(){ | ||
var test = new TestCase("String extensions"); | ||
test.compare( | ||
(4294967295).int2ip() | ||
, "255.255.255.255" | ||
, (0).int2ip() | ||
, "0.0.0.0" | ||
, "4294967295".int2ip() | ||
, "255.255.255.255" | ||
, "0".int2ip() | ||
, "0.0.0.0" | ||
, "String.int2ip()"); | ||
test.compare( | ||
"255.255.255.255".ip2int() | ||
, 4294967295 | ||
, "0.0.0.0".ip2int() | ||
, 0 | ||
, "String.ip2int()"); | ||
test.done(); | ||
}() | ||
//*/ | ||
/** Tests for lambda | ||
!function(){ | ||
var test = new TestCase("Object extensions") | ||
test.compare( | ||
JSON.stringify(Object.zip(["a","b"], [1, 2])) | ||
, '{"a":1,"b":2}' | ||
) | ||
test.done() | ||
}() | ||
//*/ | ||
/** Tests for lambda | ||
!function(){ | ||
var test = new TestCase("lambda") | ||
test.compare( | ||
'->1'.fn()(), 1, | ||
'x -> x + 1'.fn()(1), 2, | ||
'x y -> x + 2*y'.fn()(1, 2), 5, | ||
'x, y -> x + 2*y'.fn()(1, 2), 5, | ||
'_ + 1'.fn()(1), 2, | ||
'x -> y -> x + 2*y'.fn()(1)(2), 5, | ||
'x -> y -> z -> x + 2*y+3*z'.fn()(1)(2)(3), 14, | ||
'1+_'.map([1,2,3]).join(), [2, 3, 4].join(), | ||
'x y -> 2*x+y'.fold([1,0,1,0], 0), 10, | ||
'_%2'.filter([1,2,3,4]).join(), [1, 3].join(), | ||
'_>2'.some([1,2,3]), true, | ||
'_>10'.some([1,2,3]), false | ||
); | ||
//map('"im"+root', ["probable", "possible"]), ["improbable", "impossible"] | ||
//["improbable", "impossible"], map('"im"+root', ["probable", "possible"]) , | ||
test.done(); | ||
}() | ||
//*/ | ||
/** Tests for functional | ||
!function(){ | ||
var test = new TestCase("functional") | ||
test.compare( | ||
'1+_'.fn().compose('2*_'.fn())(3), 7, | ||
'1+_'.fn().chain('2*_'.fn())(3), 8) | ||
test.done(); | ||
}() | ||
//*/ | ||
14
min.js
@@ -1,7 +0,7 @@ | ||
function Nop(){}function True(){return!0}function False(){return!1}function This(){return this} | ||
!function(p){function j(a){if(k[a])return k[a];for(var b="_",d=a,c=a.split("->");1<c.length;)d=c.pop(),b=c.pop().match(/\w+/g)||"",c.length&&c.push("(function("+b+"){return("+d+")})");return k[a]=new Function(b,"return("+d+")")}var k={},f=Array.prototype,e=Function.prototype,m=String.prototype,l=Object,g=e.call.bind(f.slice),n=[];e.construct=function(a){var b=a.length;return b?(n[b]||(n[b]=j("t a->new t(a["+Object.keys(g(a)).join("],a[")+"])")))(this,a):new this};e.partial=function(){var a=this,b= | ||
g(arguments);return function(){return a.apply(this,f.concat.apply(b,arguments))}};e.byWords=function(a){var b=this;a|=0;return function(){var d=this,c=d,h=arguments;(h[a]||"").replace(/[-\w]+/g,function(f){h[a]=f;c=b.apply(d,h)});return c}};e.byKeyVal=function(){var a=this;return function(b){var d,c=g(arguments);if("object"==typeof b)for(d in b)c[0]=d,c[1]=b[d],d=a.apply(this,c);else d=a.apply(this,c);return d}};e.cache=function(a,b,d){var c=this,h=d||{},e=function(){var d=arguments,j=!!a||this instanceof | ||
e,g=b?b(d,c):j+":"+d.length+":"+f.join.call(d);return g in h?h[g]:h[g]=j?c.construct(d):c.apply(this,d)};e.origin=c;e.cached=h;e.extend=function(){return c.extend.apply(c,arguments).cache(a,b,h)};e.prototype=c.prototype;return e};e.extend=function(){var a,b=this,d=0,c=function(){return b.apply(this,arguments)};c.prototype=Object.create(b.prototype);for(c.prototype.constructor=c;a=arguments[d++];)Object.merge(c.prototype,a);return c};e.ttl=function(a,b){var d=this,c=setTimeout(function(){a=0;b&&b()}, | ||
a);return function(){clearTimeout(c);a&&d.apply(null,arguments)}};e.once=function(a){var b,d,c=this;return function(){clearTimeout(b);d=arguments;b=setTimeout(function(){c.apply(null,d)},a)}};e.rate=function(a,b){var d,c,e=this,f=0;return function(){var g=+new Date;clearTimeout(d);g>f?(f=g+a,e.apply(null,arguments)):b&&(c=arguments,d=setTimeout(function(){e.apply(null,c)},f-g))}};l.each=function(a,b,d,c){if(a)for(c in a)a.hasOwnProperty(c)&&b.call(d,a[c],c,a)};l.merge=function(a){for(var b,d,c=1;d= | ||
arguments[c++];)for(b in d)d.hasOwnProperty(b)&&(a[b]=d[b]);return a};l.zip=function(a,b){return a.fold(function(a,c,e){a[c]=b[e];return a},{})};f.remove=function(){for(var a=this.length,b=g(arguments);a--;)~b.indexOf(this[a])&&this.splice(a,1);return this};f.each=f.forEach;f.fold=f.reduce;f.foldr=f.reduceRight;f.unique=f.filter.partial(function(a,b,d){return b==d.lastIndexOf(a)});!function(a){e[a]=m[a]=function(){var b=arguments,d=b[0];b[0]=this.fn();return f[a].apply(d,b)}}.byWords()("every filter each map fold foldr some"); | ||
p.Fn=j;j.Init=function(){return this.init&&this.init.apply(this,arguments)||this};e.fn=This;m.fn=function(){return j(this)}}(this); | ||
function Nop(){}function True(){return!0}function False(){return!1}function This(){return this}function Init(){return this.init&&this.init.apply(this,arguments)||this} | ||
!function(n){function g(a){if(g[a])return g[a];for(var b="_",d=a,c=a.split("->");1<c.length;)d=c.pop(),b=c.pop().match(/\w+/g)||"",c.length&&c.push("(function("+b+"){return("+d+")})");return g[a]=new Function(b,"return("+d+")")}var f=Array.prototype,e=Function.prototype,l=String.prototype,k=Object,j=e.call.bind(f.slice),m=[];e.construct=function(a){var b=a.length;return b?(m[b]||(m[b]=g("t a->new t(a["+Object.keys(j(a)).join("],a[")+"])")))(this,a):new this};e.partial=function(){var a=this,b=j(arguments); | ||
return function(){return a.apply(this,f.concat.apply(b,arguments))}};e.byWords=function(a){var b=this;a|=0;return function(){var d=this,c=d,h=arguments;(h[a]||"").replace(/[-\w]+/g,function(f){h[a]=f;c=b.apply(d,h)});return c}};e.byKeyVal=function(){var a=this;return function(b){var d,c=j(arguments);if("object"==typeof b)for(d in b)c[0]=d,c[1]=b[d],d=a.apply(this,c);else d=a.apply(this,c);return d}};e.cache=function(a,b,d){var c=this,h=d||{},e=function(){var d=arguments,g=!!a||this instanceof e,j= | ||
b?b(d,c):g+":"+d.length+":"+f.join.call(d);return j in h?h[j]:h[j]=g?c.construct(d):c.apply(this,d)};e.origin=c;e.cached=h;e.extend=function(){return c.extend.apply(c,arguments).cache(a,b,h)};e.prototype=c.prototype;return e};e.extend=function(){var a,b=this,d=0,c=function(){return b.apply(this,arguments)};c.prototype=Object.create(b.prototype);for(c.prototype.constructor=c;a=arguments[d++];)Object.merge(c.prototype,a);return c};e.ttl=function(a,b){var d=this,c=setTimeout(function(){a=0;b&&b()},a); | ||
return function(){clearTimeout(c);a&&d.apply(null,arguments)}};e.once=function(a){var b,d,c=this;return function(){clearTimeout(b);d=arguments;b=setTimeout(function(){c.apply(null,d)},a)}};e.rate=function(a,b){var d,c,e=this,f=0;return function(){var g=+new Date;clearTimeout(d);g>f?(f=g+a,e.apply(null,arguments)):b&&(c=arguments,d=setTimeout(function(){e.apply(null,c)},f-g))}};k.each=function(a,b,d,c){if(a)for(c in a)a.hasOwnProperty(c)&&b.call(d,a[c],c,a)};k.merge=function(a){for(var b,d,c=1;d=arguments[c++];)for(b in d)d.hasOwnProperty(b)&& | ||
(a[b]=d[b]);return a};k.zip=function(a,b){return a.fold(function(a,c,e){a[c]=b[e];return a},{})};f.remove=function(){for(var a=this.length,b=j(arguments);a--;)~b.indexOf(this[a])&&this.splice(a,1);return this};f.each=f.forEach;f.fold=f.reduce;f.foldr=f.reduceRight;f.unique=f.filter.partial(function(a,b,d){return b==d.lastIndexOf(a)});!function(a){e[a]=l[a]=function(){var b=arguments,d=b[0];b[0]=this.fn();return f[a].apply(d,b)}}.byWords()("every filter each map fold foldr some");e.fn=This;l.fn=function(){return g(this)}; | ||
n.Fn=g}(this); |
{ | ||
"name": "functional-lite", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Functional Javascript", |
@@ -6,8 +6,8 @@ | ||
Fn | ||
== | ||
Functional | ||
========== | ||
Experimental Functional stuff. | ||
Download [compressed][1] | ||
(2784 bytes, 1199 bytes gzipped) | ||
(2777 bytes, 1187 bytes gzipped) | ||
or [uncompressed][2] source. | ||
@@ -18,2 +18,19 @@ | ||
Examples | ||
-------- | ||
Extends String and Function with "every filter each map fold foldr some" | ||
``` | ||
// _ is default first argument name when no arguments defined | ||
"_ + 1".map([1, 2, 3]) | ||
// is equal to | ||
"_ -> _ + 1".map([1, 2, 3]) | ||
// is equal to | ||
[1, 2, 3].map(Fn("_ + 1")) | ||
// [2, 3, 4] | ||
``` | ||
### Licence | ||
@@ -20,0 +37,0 @@ |
121
test/run.js
@@ -21,9 +21,125 @@ | ||
function Fn1(){ | ||
var t = this; | ||
t.a = 1; | ||
"init" in t && t.init.apply(t,arguments); | ||
return t; | ||
} | ||
Fn1.prototype = { | ||
init: function(){ | ||
this.b = 1; | ||
}, | ||
c:1 | ||
} | ||
var Fn2 = Fn1.extend({d:1}) | ||
, Fn3 = Fn2.extend({init:function(){},e:1}) | ||
, Fn4 = Fn3.extend({init:function(){Fn2.prototype.init.call(this);},f:1}) | ||
, f1 = new Fn1() | ||
, f2 = new Fn2() | ||
, f3 = new Fn3() | ||
, f4 = new Fn4() | ||
, run = 0 | ||
, actual = 0 | ||
, fn = function(i) { | ||
actual++; | ||
return i*i; | ||
}.cache() | ||
, fn2 = fn.origin.cache(true); | ||
var found = 0 | ||
, arr = [1,2,3,4,2,5] | ||
, res | ||
, failed = [] | ||
, out = | ||
[ | ||
] | ||
[ JSON.stringify(Object.zip(["a","b"], [1, 2])), | ||
'{"a":1,"b":2}', | ||
'->1'.fn()(), 1, | ||
'x -> x + 1'.fn()(1), 2, | ||
'x y -> x + 2*y'.fn()(1, 2), 5, | ||
'x, y -> x + 2*y'.fn()(1, 2), 5, | ||
'_ + 1'.fn()(1), 2, | ||
'x -> y -> x + 2*y'.fn()(1)(2), 5, | ||
'x -> y -> z -> x + 2*y+3*z'.fn()(1)(2)(3), 14, | ||
'1+_'.map([1,2,3]).join(), [2, 3, 4].join(), | ||
'x y -> 2*x+y'.fold([1,0,1,0], 0), 10, | ||
'_%2'.filter([1,2,3,4]).join(), [1, 3].join(), | ||
'_>2'.some([1,2,3]), true, | ||
'_>10'.some([1,2,3]), false, | ||
[1,2,3,2,1].remove(2).join(), "1,3,1", | ||
['1',2,3,2,1].remove(2,1).join(), "1,3", | ||
"a" in f1, true, | ||
"b" in f1, true, | ||
"c" in f1, true, | ||
"d" in f1, false, | ||
"e" in f1, false, | ||
"f" in f1, false, | ||
"a" in f2, true, | ||
"b" in f2, true, | ||
"c" in f2, true, | ||
"d" in f2, true, | ||
"e" in f2, false, | ||
"f" in f2, false, | ||
"a" in f3, true, | ||
"b" in f3, false, | ||
"c" in f3, true, | ||
"d" in f3, true, | ||
"e" in f3, true, | ||
"f" in f3, false, | ||
"a" in f4, true, | ||
"b" in f4, true, | ||
"c" in f4, true, | ||
"d" in f4, true, | ||
"e" in f4, true, | ||
"f" in f4, true, | ||
f1 instanceof Fn1, true, | ||
f1 instanceof Fn2, false, | ||
f1 instanceof Fn3, false, | ||
f1 instanceof Fn4, false, | ||
f2 instanceof Fn1, true, | ||
f2 instanceof Fn2, true, | ||
f2 instanceof Fn3, false, | ||
f2 instanceof Fn4, false, | ||
f3 instanceof Fn1, true, | ||
f3 instanceof Fn2, true, | ||
f3 instanceof Fn3, true, | ||
f3 instanceof Fn4, false, | ||
f4 instanceof Fn1, true, | ||
f4 instanceof Fn2, true, | ||
f4 instanceof Fn3, true, | ||
f4 instanceof Fn4, true, | ||
fn(2), 4, ++run, actual, | ||
fn(2), 4, run, actual, | ||
fn(3), 9, ++run, actual, | ||
fn(3), 9, run, actual, | ||
fn(3,1), 9, ++run, actual, | ||
fn(3,1), 9, run, actual, | ||
new fn(), new fn(), ++run, actual, | ||
new fn(1, 2), new fn(1, 2), ++run, actual, | ||
fn2(), new fn2(), ++run, actual, | ||
new fn2(1, 2), fn2(1, 2), ++run, actual, | ||
"", "" ] | ||
var tests = new async(tests_done) | ||
@@ -39,2 +155,3 @@ | ||
test_static( tests.wait() ) | ||
@@ -41,0 +158,0 @@ |
40
19445
478