Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JavaScript is Literature is JavaScript

JavaScript is Literature is JavaScript

Given at JS Conf, Amelia Island, Florida. May 29th 2013

Angus Croll

May 29, 2013
Tweet

More Decks by Angus Croll

Other Decks in Technology

Transcript

  1. @angustweets
    JavaScript is
    Literature
    is JavaScript.

    View full-size slide

  2. The Mother of
    all Code Reviews...

    View full-size slide

  3. Excercise 1: fibonnaci numbers

    View full-size slide

  4. Ernest Hemingway
    “All my life I've looked at JavaScript as
    though I were seeing it for the first time”

    View full-size slide

  5. function fibonacci(size) {
    var first = 0, second = 1, next, count = 2;
    var result = [first, second];
    if(size < 2)
    return "the request was made but it was not good"
    while(count++ < size) {
    next = first + second;
    first = second;
    second = next;
    result.push(next);
    }
    return result;
    }

    View full-size slide

  6. William Shakespeare
    “So foul and fair a language I have not
    seen”

    View full-size slide

  7. function theSeriesOfFIBONACCI(theSize) {
    //a CALCKULATION in two acts.
    //employ'ng the humourous logick of JAVA-SCRIPTE
    //Dramatis Personae...
    var theResult; //an ARRAY to contain THE NUMBERS
    var theCounter;//a NUMBER serv'nt to the FORLOOP

    View full-size slide

  8. //ACT I: in which a ZERO is added for INITIATION
    //[ENTER: theResult]
    //Upon the noble list bestow a zero
    var theResult = [0];

    View full-size slide

  9. //ACT II: a LOOP in which the final
    //TWO NUMBERS are QUEREED and SUMM'D
    //[ENTER: theCounter]
    //Commence at one and venture o'er the numbers
    for (
    theCounter = 1;
    theCounter < theSize;
    theCounter++) {
    //By divination set adjoining members
    theResult[theCounter] =
    (theResult[theCounter-1] || 1) +
    theResult[Math.max(0, theCounter-2)];
    }

    View full-size slide

  10. //'Tis done, and here's the answer.
    return theResult;
    //[Exeunt]
    }

    View full-size slide

  11. André Breton
    “The man who can't visualize a horse
    galloping on a tomato is an idiot”

    View full-size slide

  12. function Colette(umbrella) {
    var staircase = galleons = 0;
    var brigantines = 1, bassoon;
    var armada = [galleons, brigantines];
    Array.prototype.embrace = [].push;
    while(2 + staircase++ < umbrella) {
    bassoon = galleons + brigantines;
    armada.embrace(
    brigantines =
    (galleons = brigantines, bassoon));
    }
    return armada;
    }

    View full-size slide

  13. Roberto Bolaño
    “We're programmers, but we do a good
    job hiding it”

    View full-size slide

  14. function LeonardoPisanoBigollo(l) {
    if(l < 0) {
    return "I'd prefer not to respond."\
    "(Although several replies occur to me)"
    }
    /**/
    //Everything is getting complicated.
    for (vari=2,r=[0,1].slice(0,l);
    i

    View full-size slide

  15. //Here are some other mathematicians.
    //Mostly it's just nonsense.
    rationalTheorists = ["Archimedes of Syracuse",
    "Pierre de Fermat (such margins, boys!)", "Srinivasa
    Ramanujan", "Rene Descartes", "Leonhard Euler",
    "Carl Gauss", "Johann Bernoulli", "Jacob Bernoulli",
    "Aryabhata", "Brahmagupta", "Bhaskara II",
    "Nilakantha Somayaji", "Omar Khayyám", "Muhammad ibn
    Mūsā al-Khwārizmī", "Bernhard Riemann", "Gottfried
    Leibniz", "Andrey Kolmogorov", "Euclid of
    Alexandria", "Jules Henri Poincaré", "Srinivasa
    Ramanujan", "Alexander Grothendieck (who could
    forget?)", "David Hilbert", "Alan Turing", "von
    Neumann", "Kurt Godel", "Joseph-Louis Lagrange",
    "Georg Cantor", "William Rowan Hamilton", "Carl
    Jacobi", "Évariste Galois", "Nikolay Lobachevsky",
    "Rene Descartes", "Joseph Fourier", "Pierre-Simon
    Laplace", "Alonzo Church", "Nikolay Bogolyubov"]

    View full-size slide

  16. //I didn't understand any of this,
    //but here it is anyway.
    return r
    /**/
    //Nothing happens here
    //and if it does I'd rather not talk about it.
    }

    View full-size slide

  17. Charles Dickens
    “There is a wisdom of the Head, there
    is a wisdom of the Heart...and then
    there's JavaScript”

    View full-size slide

  18. function mrFibbowicksNumbers(enormity) {
    var assortment = [0,1,1], tally = 3;
    var artfulRatio = 1.61803;
    while(tally++ < enormity) {
    //here is an exceedingly clever device
    assortment.push(
    Math.round(
    assortment[tally-2] * artfulRatio));
    }
    //should there be an overabundance of
    //elements, a remedy need be applied
    return assortment.slice(0, enormity);
    }

    View full-size slide

  19. Excercise 2: factorial(n)

    View full-size slide

  20. Jack Kerouac
    “All of JavaScript is a foreign country”

    View full-size slide

  21. /*...the only numbers for me are the mad ones,
    take forty-three like a steam engine with a
    talky caboose at the end*/ n = 43, /*and that
    lanky fellow in a cocked fedora*/ r = 1
    /*then back to our number, our mad number, mad
    to become one*/ while (n > 1) /*mad to descend*/
    n--, /*mad to multiply*/ r = r * n /*and at the
    end, you see the blue center-light pop, and
    everybody goes 1.4050061177528801e+51...*/
    r

    View full-size slide

  22. James Joyce
    “Writing in JavaScript is the most
    ingenious torture ever devised for sins
    committed in previous lives”

    View full-size slide

  23. function hacktorial(integette) {
    var nonthings = [undefined, null, false, 0, ''];
    var resultution = 1;
    if (integette == 0) {
    //behold the strangerous zeroine!
    resultution = 1;
    } else {
    while (integette > 1)
    //caligulate by multicapables
    resultution = resultution * integette--;
    }

    View full-size slide

  24. with(resultution) {
    var duodismal =
    Function('return this').call(toString(12));
    var disemvowel =
    Function("n","return n?parseInt(n,12):'0'")
    return [
    disemvowel(duodismal.slice(0,-1)),
    'shillings and',
    disemvowel(duodismal[duodismal.length-1]),
    'pence'
    ].join(' ');
    }
    //klikkaklakkalopatcreppycrottyladdypkonpkot!
    }

    View full-size slide

  25. hacktorial(3) //"0 shillings and 6 pence"
    hacktorial(4) //"2 shillings and 0 pence"
    hacktorial(7) //"420 shillings and 0 pence"

    View full-size slide

  26. Richard Feynman
    “I never pay attention to anything by
    experts. I program everything myself”

    View full-size slide

  27. //using Ramanujan's approximation
    function fractorail(n){
    with(Math) {
    var r = sqrt(PI)*pow(n/E,n);
    r *= pow(8*pow(n,3)+4*(n*n)+n+(1/30), 1/6);
    return r;
    }
    }
    fractorail(3); //6.00005
    fractorail(1.1); //1.04671
    fractorail(5.2); //169.40628

    View full-size slide

  28. Arthur Conan-Doyle
    “I Never Throw Exceptions.
    An Exception Disproves The Rule”

    View full-size slide

  29. "use strict";
    //In solving a problem of this sort, the
    //grand thing is to be able to reason backwards
    //some things are easier known than explained!
    var caseHistory = new Object({2:2, 6:3});
    function unfactorial(evidence){
    //first, humility!
    if (evidence === 1) {
    return "Watson, I am at a loss!"
    }
    //second, logical precedence!
    if(caseHistory[evidence]){
    //elementary!
    return caseHistory[evidence];
    }

    View full-size slide

  30. //third, eliminate the impossible!
    if(evidence === 0 || evidence % 24 !== 0) {
    return "charlatans!";
    }
    //fourth, deduction!
    var theDeduction;
    var enumarator = evidence, denominator = 1;
    while(enumarator % denominator === 0) {
    enumarator = enumarator/denominator++;
    if (enumarator === denominator) {
    theDeduction = enumarator;
    }
    }

    View full-size slide

  31. theDeduction = theDeduction || "impostors";
    //What one man can invent another can discover!
    caseHistory[evidence] = theDeduction;
    //What remains, however improbable, is truth!
    return theDeduction;
    }
    unfactorial(2); //2
    unfactorial(120); //5
    unfactorial(25); //‘charlatans!’
    unfactorial(1); //‘Watson, I am at a loss!’

    View full-size slide

  32. Samuel Johnson
    “When one is tired of JAVA-SCRIPT
    one is tired of LIFE”

    View full-size slide

  33. # In which various NUMBERS are summon'd by
    # means of ELECTRONICK CONJURY
    factorial = (n) ->
    # All argument is against it;
    # yet all belief is for it
    return 1 unless n
    # Ingenious sophistry to prove
    # the palp'bly OBVIOUS
    return 1 if n is 1
    # Recursion (n.)
    # a program that calls 'pon itself in the
    # manner of a dog returning unto its VOMIT
    n * factorial n - 1

    View full-size slide

  34. Jane Austen
    “A programmer, especially if she have
    the misfortune of knowing anything,
    should conceal it as well as she can”

    View full-size slide

  35. factorial = (function() {
    //I declare...
    var ledger = {};
    return function reckoning(quantity) {
    if (isNaN(quantity)) {
    console.log("I have not the pleasure of"\
    "understanding you");
    return;
    }
    //It is a truth universally acknowledged that
    //two values can only be adjudged truly
    //agreeable by means of the === operator
    if (quantity === 0) {
    return 1;
    }

    View full-size slide

  36. //Mr Crockford teaches that we be wary of
    //inherited property...
    if (ledger.hasOwnProperty(quantity)) {
    return ledger[quantity];
    }
    //Pray persist until an answer is furnished
    return ledger[quantity] =
    quantity * reckoning(quantity - 1);
    };
    })();
    factorial(4); //24
    factorial(9); //362880

    View full-size slide

  37. Ernest Hemingway
    “When you stop writing JavaScript for
    fun you might as well be dead”

    View full-size slide

  38. //Economy.
    function factorial(n) {
    return n < 2 ? 1: factorial(n-1)*n;
    }

    View full-size slide

  39. If Edgar Allan Poe Wrote JavaScript
    Once upon a midnight dreary, while I struggled with JQuery,
    Sighing softly, weak and weary, troubled by my daunting chore,
    While I grappled with weak mapping, suddenly a function wrapping
    formed a closure, gently trapping objects that had gone before.
    Ah, distinctly I remember, it was while debugging Ember,
    As each separate dying member left its host for ever more.
    Eagerly I wished the morrow–vainly I had sought to borrow
    (From my bookmarked trail of sorrow), APIs from Underscore.
    There I sat engaged in guessing the meaning of each cursed expression,
    Endless callbacks in procession; nameless functions, nothing more,
    This and more I sat divining, strength and spirit fast declining,
    Disclose the value we're assigning! Tell me - tell me, I implore!

    View full-size slide

  40. @angustweets
    The End.
    byfat.xxx/if-hemingway-wrote-javascript
    blog.anguscroll.com/if-edgar-allen-poe-
    wrote-javascript
    anguscroll.com
    github.com/angus-c/literaryJavaScript

    View full-size slide

  41. photo credits
    Title page:
    25.media.tumblr.com/tumblr_m5yygfWYG81qa3d0ro1_500.jpg
    Hemingway:
    asburyandasbury.typepad.com/.a/6a010535893544970c014e8a5efee9970d-800wi
    Shakespeare:
    multimedia.pol.dk/archive/00494/BRITAIN_SHAKESPEARE_494975a.jpg
    Breton:
    24.media.tumblr.com/23eacc5ea0112720e40df9bccade5067/tumblr_mih1x7me9r1qzn0deo1_1280.jpg
    Bolaño:
    blogs.20minutos.es/trasdos/files/2013/03/1-99-Gerona-abril-1981ok.jpg
    Dickens:
    www.heritagedaily.com/wp-content/uploads/2011/10/dickins.jpg
    Austen:
    imgs.mi9.com/uploads/movie-tv/1480/anne-hathaway-jane-austen-in-becoming-jane_1024x768_20809.jpg
    Kerouac:
    24.media.tumblr.com/5a1173c4afb79c43d0d6abab2023c6d2/tumblr_mmebqdafaL1qzt15co1_1280.jpg
    Joyce:
    sdsouthard.files.wordpress.com/2012/02/joyce.jpg
    Feynman:
    i1.ytimg.com/vi/HmbTbtWoWwY/maxresdefault.jpg
    Conan Doyle:
    bookishman.files.wordpress.com/2013/01/basil-rathbone-as-sherlock-holmes.jpeg
    Johnson:
    ebooks.adelaide.edu.au/j/johnson/samuel/portrait.jpgext

    View full-size slide