Skip to content

Instantly share code, notes, and snippets.

View algesten's full-sized avatar
😀
barcelona!

Martin Algesten algesten

😀
barcelona!
View GitHub Profile
@algesten
algesten / promiseApply.coffee
Last active August 29, 2015 14:26 — forked from scott-christopher/promiseApply.js
Promise applicative example
# Goals
#
# 1. Mix promise and non-promise arguments
# 2. Behaves like a normal function if all arguments are non-promise
# 3. Returns a promise if any non-promise argument
# 4. Use no promise library, rely on the fact that:
# promise.then(-> a1).then((a2) -> a1 === a2)
# promise.then(-> Promise(a1)).then((a2) -> a1 === a2)
#
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split("")
base = alphabet.length
exports.encode = (i) ->
return alphabet[0] if i is 0
s = ""
while i > 0
s += alphabet[i % base]
i = parseInt(i / base, 10)