Skip to content

Instantly share code, notes, and snippets.

@wvl
Last active August 29, 2015 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wvl/1e29b79e15d1fe1fc1e4 to your computer and use it in GitHub Desktop.
Save wvl/1e29b79e15d1fe1fc1e4 to your computer and use it in GitHub Desktop.
Sample of ramda as built by bldr
(function(root,factory) { if (typeof define === "function" && define.amd) { define([], factory); } else { root.ramda = factory(); }})(this, function() {
// src/mkArgStr.js
var ramda = {};
var mkArgStr = ramda.mkArgStr = function(n) {
var arr = [], idx = -1;
while(++idx < n) {
arr[idx] = "arg" + idx;
}
return arr.join(", ");
};
// src/slice.js
// `slice` implemented iteratively for performance
var slice = ramda.slice = function (args, from, to) {
var i, arr = [];
from = from || 0;
to = to || args.length;
for (i = from; i < to; i++) {
arr[arr.length] = args[i];
}
return arr;
};
// src/arity.js
// Wraps a function that may be nullary, or may take fewer than or more than `n` parameters, in a function that
// specifically takes exactly `n` parameters. Note, though, that all parameters supplied will in fact be
// passed along, in contrast with `nAry`, which only passes along the exact number specified.
var arity = ramda.arity = (function() {
var cache = {};
// For example:
// cache[3] = function(func) {
// return function(arg0, arg1, arg2) {
// return func.apply(this, arguments);
// }
// };
var makeN = function(n) {
var fnArgs = mkArgStr(n);
var body = [
" return function(" + fnArgs + ") {",
" return func.apply(this, arguments);",
" }"
].join("\n");
return new Function("func", body);
};
return function(n, fn) {
return (cache[n] || (cache[n] = makeN(n)))(fn);
};
}());
// src/nAry.js
// Wraps a function that may be nullary, or may take fewer than or more than `n` parameters, in a function that
// specifically takes exactly `n` parameters. Any extraneous parameters will not be passed on to the function
// supplied
var nAry = ramda.nAry = (function() {
var cache = {};
// For example:
// cache[3] = function(func) {
// return function(arg0, arg1, arg2) {
// return func.call(this, arg0, arg1, arg2);
// }
// };
var makeN = function(n) {
var fnArgs = mkArgStr(n);
var body = [
" return function(" + fnArgs + ") {",
" return func.call(this" + (fnArgs ? ", " + fnArgs : "") + ");",
" }"
].join("\n");
return new Function("func", body);
};
return function(n, fn) {
return (cache[n] || (cache[n] = makeN(n)))(fn);
};
}());
// src/core.js
// src/curry.js
// Returns a curried version of the supplied function. For example:
//
// var discriminant = function(a, b, c) {
// return b * b - 4 * a * c;
// };
// var f = curry(discriminant);
// var g = f(3), h = f(3, 7) i = g(7);
// i(4) ≅ h(4) == g(7, 4) == f(3, 7, 4) == 1
//
// Almost all exposed functions of more than one parameter already have curry applied to them.
var _ = function(fn) {
var fnArity = fn.length;
var f = function(args) {
return arity(Math.max(fnArity - (args && args.length || 0), 0), function () {
var newArgs = (args || []).concat(slice(arguments, 0));
if (newArgs.length >= fnArity) {
return fn.apply(this, newArgs);
}
else {return f(newArgs);}
});
};
return f([]);
};
ramda.curry = _;
// src/map.js
// (Internal use only) The basic implementation of map.
var internalMap = _(function(useIdx, fn, list) {
if (list && list.length === Infinity) {
return list.map(fn, list);
}
var idx = -1, len = list.length, result = new Array(len);
if (useIdx) {
while (++idx < len) {
result[idx] = fn(list[idx], idx, list);
}
} else {
while (++idx < len) {
result[idx] = fn(list[idx]);
}
}
return result;
});
// Returns a new list constructed by applying the function to every element of the list supplied.
var map = internalMap(false);
// Like `map`, but passes additional parameters to the predicate function. Parameters are
// `list item`, `index of item in list`, `entire list`.
//
// Example:
//
// var squareEnds = function(x, idx, list) {
// return (idx === 0 || idx === list.length - 1) ? x * x : x;
// };
//
// map(squareEnds, [8, 6, 7, 5, 3, 0, 9];
// //=> [64, 6, 7, 5, 3, 0, 81]
map.idx = internalMap(true);
ramda.map = map;
// src/all.js
// Returns `true` if all elements of the list match the predicate, `false` if there are any that don't.
var all = _(function (fn, list) {
var i = -1;
while (++i < list.length) {
if (!fn(list[i])) {
return false;
}
}
return true;
});
ramda.all = all;
// src/skip.js
//! tags: lists, math
// Returns a new list containing all **but** the first `n` elements of the given list.
var skip = _(function(n, list) {
if (list && list.length === Infinity) {
return list.skip(n);
}
return slice(list, n);
});
ramda.skip = skip;
// src/filter.js
// (Internal use only) The basic implementation of filter.
var internalFilter = _(function(useIdx, fn, list) {
if (list && list.length === Infinity) {
return list.filter(fn); // TODO: figure out useIdx
}
var idx = -1, len = list.length, result = [];
while (++idx < len) {
if (!useIdx && fn(list[idx]) || fn(list[idx], idx, list)) {
result.push(list[idx]);
}
}
return result;
});
// Returns a new list containing only those items that match a given predicate function.
var filter = internalFilter(false);
// Like `filter`, but passes additional parameters to the predicate function. Parameters are
// `list item`, `index of item in list`, `entire list`.
//
// Example:
//
// var lastTwo = function(val, idx, list) {
// return list.length - idx <= 2;
// };
// filter.idx(lastTwo, [8, 6, 7, 5, 3, 0 ,9]); //=> [0, 9]
filter.idx = internalFilter(true);
ramda.filter = filter;
return ramda;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment