Skip to content

Instantly share code, notes, and snippets.

@CrossEye
CrossEye / first.js
Last active June 16, 2016 08:48
An alternate form of functional composition.
var first = (function() {
var chainNames = ["then", "andThen", "next"];
var endChainNames = ["finally", "andFinally", "last"];
var chain = function(fn) {
var f1 = function(g) {
var func = function() {return g.call(this, fn.apply(this, arguments));};
chain(func);
return func;
};
var f2 = function(g) {
@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.
// clean and pure:
function cons(x, y) {
return function(pick) {
return pick(x, y);
}
}
// does more stuff:
function cons(x, y) {
var fn = function(pick) {
@CrossEye
CrossEye / fibonacci.js
Created May 19, 2013 04:04
Simple, tail-recursive version of Fibonacci. Should run in nearly O(n).
var fibonacci = (function() {
var fib = function(curr, next, n) {
return (n === 0) ? curr : fib(next, curr + next, n - 1);
};
return function(n) {
return fib(0, 1, n);
}
}());
@CrossEye
CrossEye / composePrototype.js
Last active June 16, 2016 08:48
Prototype composition
Function.prototype.compose = function(g) {
var fn = this;
return function() {
return fn.call(this, g.apply(this, arguments));
};
};
Function.prototype.pipe = function(g) {
var fn = this;
return function() {
@CrossEye
CrossEye / composeVariadic.js
Last active October 24, 2017 07:28
Variadic compose by multiple calls
var compose = function compose(f) {
var queue = f ? [f] : [];
var fn = function fn(g) {
if (arguments.length) {
queue.push(g);
return fn;
}
return function() {
var args = Array.prototype.slice.call(arguments);
queue.forEach(function(func) {
@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)) {
@CrossEye
CrossEye / tap.md
Last active December 19, 2015 17:39

Interesting and yet frightening technique

Run the following code against [Ramda][ramda].

// Simple predicate to test whether a number is even
var even = function(n) {return (n % 2) === 0;};

// Generates an infinite list of Fibonacci numbers

var fibsgen = generator(

@CrossEye
CrossEye / Functor.js
Last active December 23, 2015 07:58 — forked from buzzdecafe/Functor.js
First Functor Fantasy Follow-up
(function(global) {
// TODO: Make a tree structure from type objects' optional `parentKey` properties. Do a depth-first search of
// this tree instead of the simple linear search of `Object.keys`.
var types = {};
global.Functor = function(config) {
types[config.key] = config;
};
global.fmap = curry(function(f, obj) {