Created
November 30, 2011 05:41
-
-
Save NigelThorne/1408193 to your computer and use it in GitHub Desktop.
monadic stack attempt in coffee script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GOAL - immutable objects | |
# to make these composable they need to both have the same signature... | |
compose = (f, g) -> (x) -> g(f(x)) | |
c = (f, args...) -> | |
if(f) | |
compose(f, c.apply(this,args)) | |
else | |
nil | |
nil = (x) -> x | |
unit = (stack)-> | |
val: null | |
stack: stack | |
bind = (operation) -> (monadicState) -> operation(monadicState.stack) | |
push = (val) -> (stack) -> | |
val: null | |
stack:[val].concat(stack) | |
pop = ()-> (stack)-> | |
val:stack[0] | |
stack: stack.slice(1) | |
b = (operation)-> (state)-> operation(state.stack) | |
call = (operation, stack) -> operation(unit(stack)).stack | |
output = (monadicFunction)->(state) -> | |
result = monadicFunction(state) | |
console.log("popped value was :" + result.val) | |
result | |
r = (monadicFunction, observer) -> (state) -> | |
result = monadicFunction(state) | |
observer(result.val) | |
result | |
v1=0 | |
v2=0 | |
pushnpop = c( | |
b(push(4)), | |
b(push(5)), | |
r(b(pop()), (v) -> v1 = v) | |
r(b(pop()), (v) -> v2 = v) | |
); | |
console.log(call(pushnpop, [])); | |
console.log(v1 + ","+ v2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment