Skip to content

Instantly share code, notes, and snippets.

@redice
Last active August 29, 2015 14:08
Show Gist options
  • Save redice/f25a57871300dcafc842 to your computer and use it in GitHub Desktop.
Save redice/f25a57871300dcafc842 to your computer and use it in GitHub Desktop.

Revisions

  1. redice revised this gist Nov 3, 2014. 1 changed file with 9 additions and 6 deletions.
    15 changes: 9 additions & 6 deletions promise.proof.coffee
    Original 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
    mResult = Promise.resolve.bind(Promise)
    promiseMonad.mResult = Promise.resolve.bind(Promise)
    mf = (value) ->
    new Promise (resolved, rejected) ->
    resolved value + " monadF"

    result1 = Promise.prototype.then.call mResult(x), mf
    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 = Promise.prototype.then.call mv, mResult
    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 = 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
    result1 = promiseMonad promiseMonad(mv, mf), mg
    result2 = Promise::then.call mv, (x) ->
    Promise::then.call (mf x), mg
    compare2Promise "Third Law:", result1, result2

  2. redice revised this gist Nov 3, 2014. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions promise.proof.coffee
    Original 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 object
    mResult = Promise.resolve x

    # 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, mf
    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
  3. redice created this gist Nov 2, 2014.
    55 changes: 55 additions & 0 deletions promise.proof.coffee
    Original 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