Skip to content

Instantly share code, notes, and snippets.

@spamwax
Last active August 29, 2015 14:02

Revisions

  1. spamwax revised this gist Jun 10, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion eloquentjavascript_solutions_chapter_8.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // Solution to eloquentjavascript exercise second 2nd edition
    // Chapter 6
    // Chapter 8
    // http://eloquentjavascript.net/2nd_edition/preview/08_error.html
    // Retry
    function MultiplicatorUnitFailure() {}
  2. spamwax revised this gist Jun 4, 2014. 1 changed file with 37 additions and 1 deletion.
    38 changes: 37 additions & 1 deletion eloquentjavascript_solutions_chapter_8.js
    Original file line number Diff line number Diff line change
    @@ -39,4 +39,40 @@ function reliableMultiply2(a, b) {
    console.log(reliableMultiply1(8, 8));
    console.log(reliableMultiply1(6, 6));
    // ---------------------------------------
    //
    // The locked box
    function withBoxUnlocked(body) {
    box.unlock();
    try {
    body();
    }
    catch (e) {
    // do nothing
    } finally {
    box.lock();
    }
    }

    // Bonus
    function withBoxUnlocked(body) {
    var state = box.locked;
    box.unlock();
    try {
    body();
    } catch (e) {
    // do nothing
    } finally {
    if (state) {
    box.lock();
    }
    }
    }

    withBoxUnlocked(function() {
    box.content.push("gold piece");
    });

    withBoxUnlocked(function() {
    throw new Error("Pirates on the horizon! Abort!");
    });
    console.log(box.locked);
    // → true
  3. spamwax revised this gist Jun 4, 2014. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion eloquentjavascript_solutions_chapter_8.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    // Solution to eloquentjavascript exercise second 2nd edition
    // Chapter 6
    // http://eloquentjavascript.net/2nd_edition/preview/08_error.html
    // Retry
    function MultiplicatorUnitFailure() {}

    function primitiveMultiply(a, b) {
    @@ -33,4 +37,6 @@ function reliableMultiply2(a, b) {
    }

    console.log(reliableMultiply1(8, 8));
    console.log(reliableMultiply1(6, 6));
    console.log(reliableMultiply1(6, 6));
    // ---------------------------------------
    //
  4. spamwax created this gist Jun 4, 2014.
    36 changes: 36 additions & 0 deletions eloquentjavascript_solutions_chapter_8.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    function MultiplicatorUnitFailure() {}

    function primitiveMultiply(a, b) {
    if (Math.random() < 0.5)
    return a * b;
    else
    throw new MultiplicatorUnitFailure();
    }

    function reliableMultiply1(a, b) {
    try {
    return primitiveMultiply(a, b);
    } catch (e) {
    if (e instanceof MultiplicatorUnitFailure) {
    return reliableMultiply(a, b);
    } else {
    throw e;
    }
    }
    }

    function reliableMultiply2(a, b) {
    for (;;) {
    try {
    return primitiveMultiply(a, b);
    } catch (e) {
    if (e instanceof MultiplicatorUnitFailure) {
    continue;
    } else
    throw e;
    }
    }
    }

    console.log(reliableMultiply1(8, 8));
    console.log(reliableMultiply1(6, 6));