npm install funcoffee
require('funcoffee').expose global
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 |
{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); |
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; |
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); |
{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! |