Shelve In: Programming LanguageS/JavaScriPt What If If

Transcription

Sh elv e i n: Pro gramming Language s/ JavaScri ptWhat if ↜William Shakespeare were asked to generate theFibonacci series or Jane Austen had to write a factorial program?In If Hemingway Wrote JavaScript, author Angus Croll imaginesshort JavaScript programs as written by famous wordsmiths. Theresult is a peculiar and charming combination of prose, poetry,and programming.The best authors are those who obsess about languageâ ›—â ›and thesame goes for JavaScript developers. To master either craft, youmust experiment with language to develop your own style, yourown idioms, and your own expressions. To that end, If HemingwayifHemingwayWroteJavascriptWrote JavaScript playfully bridges the worlds of programmingand literature for the literary geek in all of us.The A u th o rAngus Croll is obsessed with JavaScript and literaturein equal measure. He works on Twitter’s UI framework team, where he co-authored theFlight framework. He writes the influential JavaScript, JavaScript blog and speaks atconferences worldwide.THE FINEST INGEEK ENTERTAINMENT www . nostarch . com 19.95( 20.95 CDN)No StarchPresswww.allitebooks.com

1011 101 01010100000111010101010111010 101 010 000 011 1010 101 010 111 010 101 0101010100000111010 101 0101 110 101 010 100 0000101 010 10111010101010000011101010101 000 111 010 101 0101 110 101 010 100 000 1000001110101010101 110 1010 101 000 001 110 1011011 101 01010100000111010101010111010 101 010 000 011 1010 101 010 111 010 101 1110101010100000111 010 1010 101 110 101 010 1001110 101 01010111010101010 00001110101 0101 011 101 010 101 000 0011 101 010 101 0101010 111010101010 000 011 101 010 101 011 101 00100 000 11101010101011101010101000001 110 101 010 101 1101 010 101 011 000 011 101010101011101010101000001110101010101110101010 100000111010101010111 010 1010 10 0000 111 010 1011110 101 01010000011101010101011101010 101 000 001 110 1010 101 011 101 010 101 000001110101010101110101010100000111010 101010111010101010000011101010 1010 111 010 101 010 000 0110101 010 11101010101000000111010101010 111 010 101 010 0000 111 010 101 010 111 010101010000011101010101011101 010101000001110101010101110101010100000 1110 101 010 101 1101 01 0100001 110 10101010111010101010000011101 010 101 011 101 0101 010 000 011 101 010 101011101010101000001 11010101010111010101010000011101010101011101010101 000 001 110 101 010 1011 11010 100 00011101010101011101010101010 101 110 101 010 1000 001 110 101 010 101 110101010100 010100000 111 010 101 010 111 0101 00000 011 10101010101110101010100000111 010 101 010 111 0101 010 100 000 111 010 1110101010101110101 010 100 0001 110 101 010 1011010 101 00000111010101010111010101010 000 011 101 010 1010 111 010 101 010 000 1010000011101010101 011 101 0101 010 000 011 1011010 111 01010101000001110101010101110 101 010 100 000 1110 101 010 101 110 101 1101010101000001110 101 010 1011 101 010 101 0001101 010 10101110101010100000111010101 010 111 0 10 101 010 0000 111 010 101 010 1010101110101010100 0001 11 0101 010 101 110 1011000 001 110101010101110101010100000 11 101 010 1010 111 010 101 010 000 0111 010 1110101010101110101 010 100 000 111 0101 010 100101 010 100000111010101010 111010101010 000 011 101 010 101 0111 010 101 010 1010100000111010101010 111 0101 010 100 000 111 011010 111 01010101000 0011101010101011101 010 101 0000 011 101 010 101 011 1010 1110101010101000011101 010 101 011 1010 101 010 01110 101 10101010100000111010101010111 010 101 010 000 0111 010 101 010 111 010 1110101010100000111 010 101 0101 110 101 010 1001110 101 01010111010101010000011101010 101 011 101 010 1010 000 011 101 010 101 1010101010111010101 010 0007 001 110 1 01 010 1001110 101 01010111010101010000011101010 101 011 101 010 1010 110 000 111 010 101 0101010101110101010 100 00 01 110 101 0101 011 100101 000 00111010101010111010101010000 011 101 010 101 0111 010 101 010 000 011 000011101010101 01110 101 010 1000 001 110 101 011110 101 01010100001110101010101110101 010 100 000 111 0101 010 101 110 101 010 101010 100000111010101 010 111 010 101 010 0000 11010 101 01110101010100000111010101010 111 010 101 010 0000 111 010 101 010 111 0111010101010111010 101 010 0000 111 010 101 0100101 010 10000011101010101011101010101 000 001 110 101 0101 011 101 010 101 000 1101010101000001110 101 010 1011 101 010 101 0001101 010 10101110101010100000111010101 010 111 010 101 0100 000 111 010 101 010 1010101110101010100 0001 110 101 010 101 110 1011000 001 11010101010111010101010000011 101 010 101 011 1010 101 010 000 011 101 01010101110101010100 0001110101010101110101010100000111010101010111010 1010 100 000 111 010 101 0100101 010 10 00001110101010101110101010 100 000 111 010 1010 101 110 101 010 100 000111010111 010101010 111 010 101 010 000 0111 00101 011 10101010100000111010101010111 010 101 010 000 0111 010 101 010 111 010 10 1010101010000011101 01 0101 011 101 010 101 001110 101 01010111010101010000011101010 101 011 101 010 1010 000 011 10 10 101 01010101011101010101000 001 110 101 010 101 110 100100 000 11101010101011101010101000001 110 101 010 101 1101 010 101 000 001 110 1110101010100000111 010 1010 101 110 101 010 1001110 101 01010111010101010000011101010101 011 101 010 1010 000 011 101 010 101 0101010111010101010 000 0111 010 101 010 111 010www.allitebooks.com

ifHemingwayWroteJavascriptIllustrations by Miran LipovačaNo Starch Presswww.allitebooks.com

I f H em i n g way W ro t e J ava S c r i p t . Copyright 2015 by Angus Croll.All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic ormechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior writtenpermission of the copyright owner and the publisher.Printed in CanadaFirst printing18 17 16 15 14123456789ISBN-10: 1-59327-585-4ISBN-13: 978-1-59327-585-3Publisher: William PollockProduction Editor: Alison LawCover and Interior Illustrations: Miran LipovačaCover Design: Beth MiddleworthInterior Design: Ryan Byarlay and Beth MiddleworthDevelopmental Editor: Seph KramerCopyeditor: Rachel MonaghanCompositor: Alison LawProofreader: Emelie BurnetteFor information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:No Starch Press, Inc.245 8th Street, San Francisco, CA 94103phone: 415.863.9900; info@nostarch.com;www.nostarch.comLibrary of Congress Cataloging-in-Publication DataCroll, Angus.If Hemingway Wrote JavaScript / by Angus Croll.pages cmISBN 978-1-59327-585-3 — ISBN 1-59327-585-4I. Title.PS3603.R64I38 2014811'.6—dc23 2014031873No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product andcompany names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbolwith every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of thetrademark owner, with no intention of infringement of the trademark.The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken inthe preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity withrespect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.This is a work of parody. It imitates various authors’ voices and styles for comic effect.www.allitebooks.com

To Lucy,George, andRosiewww.allitebooks.com

AcknowledgmentsThank you to Miran Lipovača for his amazingWallace’s prime number solution was inspiredbook, and to Jacob Thornton, who, two yearsPrabowo (“JavaScript Sieve Of Atkin.js,”artwork, which has added so much to thisago, invited me to write the original blog poston which this book is based.Thanks to Andrea Pitzer, author of The SecretHistory of Vladimir Nabokov (Pegasus Books,2013), for reviewing the Nabokov section;Chris Kubica, editor of Letters to J.D. Salinger,(University of Wisconsin Press, 2002), forreviewing the Salinger section; and Joelby a solution by Mohammad hanks to Bill Pollock at No Starch Press forbeing persuaded to take on this project againsthis better judgment, and to Alison Law, SephKramer, and everyone else at No Starch fortheir sterling work and for putting up with mystubbornness.Turnbull for reviewing the Joyce section.And a special thanks to the 28 authors, poets,Roberts for their contributions. David Fosterto Brendan Eich for inventing JavaScript.Thanks also to Lucy Kaminska and Graemeand playwrights who feature in this book, andwww.allitebooks.com

ContentsForeword6by Jacob ThorntonIn t ro duct io nFiBonAcci8121Ernest Hemingway. 142William Shakespeare. 203André Breton. 264Roberto Bolaño. 325Dan Brown. 38Poetic Interlude“The Variable”. 46inspired by Edgar Allan Poefact o r ia l486Jack Kerouac. 507Jane Austen. 568Samuel Johnson. 62PrimE Numbers9Sir Arthur Conan Doyle. 6816Jorge Luis Borges. 12210James Joyce. 7417Lewis Carroll. 12818Douglas Adams. 13419Charles Dickens. 14020David Foster Wallace. 146Poetic Interlude“Macbeth’s Lost Callback”. 82inspired by William ShakespeareHappy Numbers8411J.D. Salinger. 8612Tupac Shakur. 9213Virginia Woolf. 9814Geoffrey Chaucer. 10415Vladimir Nabokov. 112Poetic Interlude“The Refactor”. 118inspired by Dylan ThomasPoetic Interlude“O Captain, My Captain”. 154inspired by Walt Whitmans ay it15621Sylvia Plath. 15822Italo Calvino. 16423J.K. Rowling. 17024Arundhati Roy. 17625Franz Kafka. 184noteswww.allitebooks.com120190

ForewordAngus and I came together over a sharedSome of these conversations later informedbeing any art, literature, or other expressionDivya Manian’s excellent “YES P lZ LETSfascination with the outside—the outsidethat runs counter to Silicon Valley.Around 2 0 1 2 , this coalesced into ##ABC,an I RC book club that never actually readwritings on http://byfat.xxx—posts likeBURNNNN ” or my “rien ne tient en place.”But none was quite so well received as Angus’s“If Hemingway Wrote JavaScript.”anything. Instead, we were something like aAngus managed to perfectly articulate an issuework, what we were doing, and how we wereGood Parts” and the general rhetoric of “the bestsupport group, gathering to make sense of ourdoing it, within the world and through the lensof art and literature.central to many of us: our antipathy toward “Theway.” And he did so by celebrating JavaScript’svoice and variety, through exploration andexperimentation. He was making the languagewww.allitebooks.com

ours, and it was precisely this ownership thatreflection on creativity and language as craft.its voice making our work not only bearable, butI RC to the conference circuit, and now toserved to liberate its potential for expression—actually exciting.I wrote, not long after that, “Like an artistpainting a bowl of fruit, if I had to expresseach work the same way — with the onlyvariety being in the fruits themselves — I’dIt’s been fun to watch this idea evolve frombook form — the medium that inspired thiswhole line of thought.Jacob Thornton (@fat)August 2014surely have gone mad by now.” This insighton writing code, and my career at large, I owevery much to my dear friend Angus and hiswww.allitebooks.com

introductionDid Ernest Hemingway write JavaScript? WouldJane Austen have grappled with function hoisting?Was Franz Kafka driven to despair by prototypalinheritance? Brushing aside a few bothersome facts(such as JavaScript not being invented until 1995),it’s easy to see why this most literary of computerlanguages would have piqued the interest of theseand other authors.JavaScript has plenty in common with natural language.It is at its most expressive when combining simple idiomsin original ways; its syntax, which is limited yet flexible,promotes innovation without compromising readability.And, like natural language, it’s ready to write. Some ofJavaScript’s more baroque cousins must be edited withan IDE (integrated development environment—a sortof Rube Goldberg machine for coding). JavaScript needsnothing more than a text file and an open mind.Natural language has no dominant paradigm, andneither does JavaScript. Developers can select froma grab bag of approaches—procedural, functional, andobject-oriented—and blend them as appropriate. Mostideas can be expressed in multiple ways, and manyJavaScript programmers can be identified by theirdistinct coding style.Some of the solu

“O Captain, My Captain”. 154 inspired by Walt Whitman say ti 156 21 Sylvia Plath . 158 22 Italo Calvino . 164 23 J.K. Rowling . 170 24 Arundhati Roy . 176 25 Franz Kafka . 184 notes 190 contents www.allitebooks.com. Angus and I came together over a shared fascination with the outside—the outside being any art, literature, or other expression that runs counter to .