This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function lens(get, set) { | |
var f = function (a) { return get(a); }; | |
f.set = set; | |
f.mod = function (f, a) { return set(a, f(get(a))); }; | |
return f; | |
} | |
var first = lens( | |
function (a) { return a[0]; }, | |
function (a, b) { return [b].concat(a.slice(1)); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Variant of Avraham Plotnitzky's String.prototype method mixed with the "fast" version | |
* see: https://sites.google.com/site/abapexamples/javascript/luhn-validation | |
* @author ShirtlessKirk. Copyright (c) 2012. | |
* Licensed under WTFPL (http://www.wtfpl.net/txt/copying) | |
*/ | |
function luhnChk(luhn) { | |
var len = luhn.length, | |
mul = 0, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var hasOwnProperty = Object.prototype.hasOwnProperty; | |
var Function = hasOwnProperty.constructor; | |
function isIdentifier(s) { | |
return /[a-zA-Z_$][a-zA-Z_$0-9]*/.test(s); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(Fp, Ap) { | |
var applyMethod = Fp.apply, | |
bindMethod = Fp.bind, | |
callMethod = Fp.call; | |
var sliceMethod = Ap.slice, | |
concatMethod = Ap.concat; | |
var apply = callMethod.bind(applyMethod), | |
bind = callMethod.bind(bindMethod), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Fibonacci infinite stream generator. Not hugely exciting yet | |
*/ | |
var fgen = (function() { | |
var fn1 = 0, fn2 = 1; | |
f = function f() { | |
var curr = fn2; | |
fn2 = fn1; | |
fn1 = fn1 + curr; | |
return fn1; |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Create an array with `len` elements. | |
* | |
* @param [initFunc] Optional: a function that returns | |
* the elements with which to fill the array. | |
* If the function is omitted, all elements are `undefined`. | |
* `initFunc` receives a single parameter: the index of an element. | |
*/ | |
function initArray(len, initFunc) { | |
if (typeof initFunc !== 'function') { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on a discussion with Michael Haufe: | |
// https://groups.google.com/group/jsmentors/browse_thread/thread/d028fb0041f93a27 | |
// Not really recommended for anything but the fun of knowing it can be done! | |
var omega = function() { | |
var queue = []; | |
var valueOf = Function.prototype.valueOf; | |
Function.prototype.valueOf = function() { | |
queue.push(this); | |
return 1; // not needed now, but could be used later to distinguish operators. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Version 1. Simple recursive function. Blows the stack for large nodes. | |
function replace(node, from, to) { | |
switch (node.type) { | |
case IF: | |
return { | |
type: IF, | |
test: replace(node.test, from, to), | |
then: replace(node.then, from, to), | |
else: replace(node.else, from, to) | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(global) { | |
var types = function(obj) { | |
throw new TypeError("fmap called on unregistered type: " + obj); | |
}; | |
// inefficient as hell, but as long as there aren't too many types.... | |
global.Functor = function(type, defs) { | |
var oldTypes = types; | |
types = function(obj) { | |
if (type.prototype.isPrototypeOf(obj)) { |
OlderNewer