Skip to content

Instantly share code, notes, and snippets.

View AutoSponge's full-sized avatar

Paul Grenier AutoSponge

View GitHub Profile
@CrossEye
CrossEye / lens.js
Created June 5, 2014 15:36 — forked from andyhd/lens.js
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)); }
/**
* 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,
@dherman
dherman / closure.js
Created July 27, 2012 21:39
Analog to Function constructor for building closures
(function() {
var hasOwnProperty = Object.prototype.hasOwnProperty;
var Function = hasOwnProperty.constructor;
function isIdentifier(s) {
return /[a-zA-Z_$][a-zA-Z_$0-9]*/.test(s);
}
@dherman
dherman / functions.js
Created July 29, 2012 01:44
Function utilities
(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),
@buzzdecafe
buzzdecafe / generator.js
Last active December 16, 2015 13:39
infinite stream fibonacci generator
/*
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;
@hhauflaire
hhauflaire / cursor.svg
Last active December 27, 2015 17:19
WIdget Video iOS SVGs
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
/**
* 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') {
@CrossEye
CrossEye / omega.js
Last active May 14, 2018 08:01
Functional compostion using fake operator overloading
// 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.
@dherman
dherman / 1-recursive.js
Created February 7, 2012 20:10
turning recursion into iteration in JS
// 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)
};
@CrossEye
CrossEye / Functor.js
Last active July 26, 2020 19:59
First Functor Fantasy
(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)) {