Last active
August 29, 2015 14:08
-
-
Save redice/f25a57871300dcafc842 to your computer and use it in GitHub Desktop.
Revisions
-
redice revised this gist
Nov 3, 2014 . 1 changed file with 9 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -20,14 +20,17 @@ compare2Promise = (title, pA, pB) -> The first law M(mResult(x), mf) = mf(x) ### promiseMonad = (mv, mf) -> Promise::then.call mv, mf x = "monad example" # mResult is a function as Promise.resolve promiseMonad.mResult = Promise.resolve.bind(Promise) mf = (value) -> new Promise (resolved, rejected) -> resolved value + " monadF" result1 = promiseMonad promiseMonad.mResult(x), mf result2 = mf x compare2Promise "First Law:", result1, result2 @@ -36,7 +39,7 @@ The second law M(mv, mResult) = mv ### mv = Promise.resolve [4] result1 = promiseMonad mv, promiseMonad.mResult result2 = mv compare2Promise "Second Law:", result1, result2 @@ -48,8 +51,8 @@ mg = (value) -> new Promise (resolved, rejected) -> resolved value + " monadG" result1 = promiseMonad promiseMonad(mv, mf), mg result2 = Promise::then.call mv, (x) -> Promise::then.call (mf x), mg compare2Promise "Third Law:", result1, result2 -
redice revised this gist
Nov 3, 2014 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,10 @@ # Promise = require 'promise' ## compare2Promise = (title, pA, pB) -> valueA = null valueB = null pA.then (value) -> valueA = value pB.then (value) -> @@ -19,14 +21,13 @@ The first law M(mResult(x), mf) = mf(x) ### x = "monad example" # mResult is a function as Promise.resolve mResult = Promise.resolve.bind(Promise) mf = (value) -> new Promise (resolved, rejected) -> resolved value + " monadF" result1 = Promise.prototype.then.call mResult(x), mf result2 = mf x compare2Promise "First Law:", result1, result2 @@ -35,7 +36,6 @@ The second law M(mv, mResult) = mv ### mv = Promise.resolve [4] result1 = Promise.prototype.then.call mv, mResult result2 = mv compare2Promise "Second Law:", result1, result2 -
redice created this gist
Nov 2, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ compare2Promise = (title, pA, pB) -> valueA = null valueB = null pA.then (value) -> valueA = value pB.then (value) -> valueB = value Promise.all [pA, pB] .then -> console.log "==> #{title}" console.log "value of A: #{valueA}" console.log "value of B: #{valueB}" console.log "equals is #{if valueA is valueB then 'true' else 'false'}" ### The first law M(mResult(x), mf) = mf(x) ### x = "monad example" # mResult is a object mResult = Promise.resolve x mf = (value) -> new Promise (resolved, rejected) -> resolved value + " monadF" result1 = Promise.prototype.then.call mResult, mf result2 = mf x compare2Promise "First Law:", result1, result2 ### The second law M(mv, mResult) = mv ### mv = Promise.resolve [4] result1 = Promise.prototype.then.call mv, mResult result2 = mv compare2Promise "Second Law:", result1, result2 ### The third law M(M(mv, mf), mg)) = M(mv, function(x){return M(mf(x), mg)}) ### mg = (value) -> new Promise (resolved, rejected) -> resolved value + " monadG" result1 = Promise.prototype.then.call Promise.prototype.then.call(mv, mf), mg result2 = Promise.prototype.then.call mv, (x) -> Promise.prototype.then.call (mf x), mg compare2Promise "Third Law:", result1, result2