Skip to content

Instantly share code, notes, and snippets.

@2bbb
Last active December 15, 2015 11:58
Show Gist options
  • Save 2bbb/5256814 to your computer and use it in GitHub Desktop.
Save 2bbb/5256814 to your computer and use it in GitHub Desktop.
JavaScript Math Extention
/*
JavaScript Array Extention
v0.1
2013 [c] ISHII 2bit
Author : ISHII 2bit
E-Mail : ishii [ at ] buffer-renaiss.com
Web : http://2bit.jp/
Update :
2013/03/28
v.0.1
*/
(function(A, L, R, S, U) {
function swap(f) {
return function() {
return f.apply(this, A[R].apply(arguments));
}
}
A.map = A.map || function(f, res, n) {
for(res = [], n = 0; n++ < this[L]; res[n] = f(this[n]));
return res;
};
A.foldl = function(f, i, n) {
for(n = 0, i = (i === U) && this[n++]; n < this[L]; i = f(i,this[n++]));
return i;
};
A.foldr = function(f, i) { return this[S](0)[R]().foldl(swap(f), i); };
A.each = A.each || function(f, n) {
for(n = 0; n < this[L]; n++) f(this[n]);
};
})(Array.prototype, 'length', 'reverse', 'slice');
/*
JavaScript Function Extention
v0.1
2013 [c] ISHII 2bit
Author : ISHII 2bit
E-Mail : ishii [ at ] buffer-renaiss.com
Web : http://2bit.jp/
Update :
2013/03/28
v.0.1
*/
(function(F, A, C, U) {
function map(f, l) {
var r = [];
for(var i = 0; l[i] !== U; i++) {
r[i] = f(l[i]);
console.log(i, l[i], f(l[i]));
}
return r;
}
var P = F.prototype;
F.swap = function(f) {
return function() {
return f[A](this, [].reverse[A](arguments));
}
}
P.swap = function() { return F.swap(this); };
F.compose = function(f, g) {
return function() {
return f(g.apply(g, arguments));
}
};
P.compose = function(f) { return F.compose(this, f); };
F.wrap = function(f, g) { return F.compose(g, f); };
P.wrap = function(f) { return F.wrap(this, f); };
P.curry = function() {
var f = this, arg = [].slice[C](arguments);
return function() {
return f[A](f, [].concat[A](arg, arguments));
};
};
F.curry = function(f) {
return f.curry.apply(f, [].slice[C](arguments, 1));
}
})(Function, 'apply', 'call');
/*
JavaScript Math Extention
v0.1.1.1
2013 [c] ISHII 2bit
Author : ISHII 2bit
E-Mail : ishii [ at ] buffer-renaiss.com
Web : http://2bit.jp/
Update :
2013/03/28
v.0.1 - v.0.1.1.1
*/
(function(M, L, S) {
function map(f, l) {
var r = [];
for(var i = 0; i < l[L]; i++) r[i] = f(l[i]);
return r;
}
function listApply(f) {
return function() {
var a = arguments;
return f.apply(f, a[0][L] ? a[0] : a);
};
}
M.random = (function(random) {
return listApply(function(a, b, n) {
n = arguments[L];
a = n < 2 ? ((b = n ? a : 1) - b) : a;
return random() * (b - a) + a;
});
})(M.random);
M.log = (function(log) {
return listApply(function(a, b) {
if(arguments[L] < 2) {
b = a;
a = 10;
}
return log(b) / log(a);
});
})(M.log);
function L_(y) {
return function(x) {
return M.log(y, x);
};
}
M.log2 = L_(2);
M.ln = L_(M.E);
M.pow = (function(pow) {
return listApply(function(a, b) {
if(arguments[L] < 2) {
b = a;
a = M.E;
}
return pow(a, b);
});
})(M.pow);
function E_(y) {
return function(x) {
return M.pow(y, x);
};
}
M.exp2 = E_(2);
M.exp10 = E_(10);
map(
function(key) {
M[key] = (function mod(k, tmp) {
tmp = M[k];
return listApply(tmp);
})(key);
},
"min,max"[S](",")
);
map(
function(key) {
M[key] = (function(k, tmp) {
tmp = M[k];
return function() {
var a = arguments;
return a[0][L] ? map(tmp, a[0]) : tmp(a[0]);
};
})(key);
},
"ceil,floor,round,abs,sin,cos,tan,asin,acos,atan,atan2,sqrt,log,log2,ln,pow,exp,exp2,exp10,random"[S](",")
);
})(Math, 'length', 'split');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment