Skip to content

Instantly share code, notes, and snippets.

@elclanrs
elclanrs / _.js
Created June 4, 2014 02:08
Dynamic object key interpolation
var formatObj = function(data, obj) {
return Object.keys(obj).reduce(function(acc, k) {
acc[data[k.match(/#\{(\w+)\}/)[1]]] = obj[k]
return acc
},{})
}
formatObj({a:'foo', b:'baz'}, {'#{a}': 'this is foo'
,'#{b}': 'this is baz'})
//^
require('funcoffee').expose global
#- Monadic
unit = curry (x, a) -> new x.constructor a
mbind = curry (f, ma) -> ma.bind f
mjoin = (ma) -> ma.bind id
fmap = curry (f, ma) -> ma.bind (a) -> unit ma, f a
ap = curry (mf, ma) -> mf.bind (f) -> fmap f, ma

Install

npm install funcoffee

Require

require('funcoffee').expose global
@elclanrs
elclanrs / dom.ls
Last active August 29, 2015 13:57
Minimal library for functional DOM manipulation
{map, unique, flatten} = require \prelude-ls
# Helpers
#
joinF = (f, g) -->
->
(f ...) ++ g ...
maybe = (pred, f, x) -->
function curry$(f, bound){
var context,
_curry = function(args) {
return f.length > 1 ? function(){
var params = args ? args.concat() : [];
context = bound ? context || this : this;
return params.push.apply(params, arguments) <
f.length && arguments.length ?
_curry.call(context, params) : f.apply(context, params);
} : f;
var __slice = [].slice;
var __noop = function(){};
function typeOf(x) {
return {}.toString.call(x).slice(8,-1);
}
function overload(fs) {
return function() {
var types = __slice.call(arguments).map(typeOf);
@elclanrs
elclanrs / money.js
Created January 10, 2014 17:25
Working with money in JS using integers instead of floating point
var Money = (function(){
function Money(qty) {
this.whole = 0;
this.cents = 0;
this.add(qty);
}
Money.prototype.calc = function() {
while (this.cents > 100) {
this.cents -= 100;
this.whole += 1;
@elclanrs
elclanrs / _helpers.js
Last active June 23, 2021 15:12
Monads in JavaScript
var extend = function(a, b) {
for (var i in b)
a[i] = b[i];
return a;
};
var fluent = function(f) {
return function() {
var clone = extend(Object.create(null), this);
f.apply(clone, arguments);
@elclanrs
elclanrs / _readme.md
Last active January 30, 2017 00:21
Simple monads in JavaScript with `do` syntax
@elclanrs
elclanrs / building.ls
Created November 18, 2013 10:39
Building.js
{each, filter} = require \prelude-ls
@Building =
overload: (fns) -> (...args) ->
types = args.map -> typeof! it
i = 0
while true
fn = fns[types * ', ']
break if fn or i > args.length
types.pop!