Skip to content

Instantly share code, notes, and snippets.

View micmarsh's full-sized avatar

Michael Marsh micmarsh

View GitHub Profile
@micmarsh
micmarsh / dopromise.coffee
Last active August 29, 2015 14:01
Haskell Do Notation for JavaScript Promises
COMPILED_COFFEE = ' < -'
PLAIN_JS = '<-'
thenify = (lines) ->
[line, rest...] = lines
if rest.length is 0
line
else if line.search(PLAIN_JS) > 0 or line.search(COMPILED_COFFEE) > 0
[value, promise] = line.split if line.search(PLAIN_JS) > 0 then PLAIN_JS else COMPILED_COFFEE
noSemiColon = promise.slice(0, -1)
@micmarsh
micmarsh / fixscoping.coffee
Last active August 29, 2015 13:59
Fix Scoping in CoffeeScript
# While coffee script offers a "class" keyword, methods still aren't that closely
# associated with class instances. Include fixScoping(this) at the beginning
# of all of your constructors to make that class' methods well behaved by javascript standards
fixScoping = (instance) ->
for property, value of instance
do (property, value) -> #b/c javascript
if typeof value is "function"
instance[property] = (args...) -> value.apply(instance, args)
@micmarsh
micmarsh / compose.coffee
Last active December 31, 2015 01:49
Compose Promises and Synchronous Functions
# use lodash/underscore, this could probably actually be a mixin for those
compose = (fns) ->
_.reduceRight fns, (composed, currentFn) ->
_.compose (valueOrPromise) ->
vop = valueOrPromise
if Boolean vop and vop.then
vop.then (value) ->
currentFn value
else
currentFn vop
@micmarsh
micmarsh / queue.clj
Last active December 30, 2015 22:39
Persistent Queue In Clojure
; Clojure actually has a queue type, but it's not easily accesible. Here's a function to help with that
(defn queue
([] clojure.lang.PersistentQueue/EMPTY)
([& elements]
(into (queue) elements)))