Skip to content

Instantly share code, notes, and snippets.

@datchley
datchley / useWith.js
Last active August 29, 2015 14:26
A decorator for modifying a function to lift functions to work with different types of arguments
//
// Some common utility combinators and helpers
//
function flip(fn) {
return function() {
var args = [].slice.call(arguments);
return fn.apply(this, args.reverse());
};
}
@datchley
datchley / app.js
Last active September 20, 2022 01:22
"Getting Functional with Javascript" Blog post source files
/**
* Primary application logic for our Functional Programming blog example
* See related blog series at: http://www.datchley.name/tag/functional-programming/
* Version: 2.0
*/
// A simple, resuable comparison for '>='
function greaterThanOrEqual(a, b) {
return a >= b
}
@datchley
datchley / exercise.js
Last active August 29, 2015 14:27
Simple Functional/Pure Programming Exercise for UI Meetup (Bullhorn)
//
// This is the actual implementation using our minimal functional library
//
var validate = {
'values': function(o) { return !isNull(o) && !isUndefined(o) && (isBoolean(o) || isNumber(o) || isString(o)); },
'arrays': function(o) { return !isNull(o) && !isUndefined(o) && isArray(o); }
}
var build = {
'values': function(prop, val) { return [prop,"=",qs(val.toString())].join(''); },
@datchley
datchley / combine.js
Last active August 29, 2015 14:27
Idea for a combine(list, list) function for javascript in functional style
['Function','Array'].forEach(function(type) {
var checkFn = 'is'+type;
window[checkFn] = function(o){ return Object.prototype.toString.call(o) == '[object ' + type + ']'; };
});
// Return new list as combination of the two lists passed
// The second list can be a function which will be passed each item
// from the first list and should return an array to permute that item
// with. If either argument is not a list, it will be treated as a list.
//
@datchley
datchley / es6-eventemitter.js
Last active June 7, 2021 03:40
A straight forward EventEmitter implemented in Javascript using ES6
let isFunction = function(obj) {
return typeof obj == 'function' || false;
};
class EventEmitter {
constructor() {
this.listeners = new Map();
}
addListener(label, callback) {
this.listeners.has(label) || this.listeners.set(label, []);
@datchley
datchley / tco.js
Created November 24, 2015 21:59
Tail Call Optimization on the fly for tail recursive functions in Javascript (naive implementation)
function range(s, e, res) {
if (!res) { res = []; }
res.push(s);
if (s == e ) {
return res;
}
else {
if (s<e) { s++; }
else { s--; }
return range(s, e, res);
@datchley
datchley / romans.js
Created December 8, 2015 05:20
Conversions for Integers <-> Roman Numerals
function int2roman(n) {
var map = [
{ factor: 1000, letter: 'M' },
{ factor: 900, letter: 'CM' },
{ factor: 500, letter: 'D' },
{ factor: 400, letter: 'CD' },
{ factor: 100, letter: 'C' },
{ factor: 90, letter: 'XC' },
{ factor: 50, letter: 'L' },
{ factor: 40, letter: 'XL' },
@datchley
datchley / bash-snippets.bash
Created January 22, 2016 17:16
Various bash snippets I've used in scripts
#!/bin/bash
# disable builtin echo so we can use '-en'
enable -n echo
SCRIPT=$(basename "$0")
SCRIPT_DIR=$(dirname "$0")
echo "$SCRIPT running from $SCRIPT_DIR"
# Determine if we're interactive or not
@datchley
datchley / async-generator-runner.js
Created February 24, 2016 15:09
A runner for creating async generators for various types of sources
// Sources...
let arr = [1,2,3,4];
// thunk returning function that takes a callback
// and applies the arg to it after ms milliseconds timeout
function delay(arg, ms=1000) {
return function(callback) {
setTimeout(_=>callback(arg), ms);
};
}
@datchley
datchley / QEvBwg.markdown
Last active July 5, 2016 16:41 — forked from anonymous/QEvBwg.markdown
Responsive: Higher Order Component to pass width/height to Components as props