版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、如果让莎士比亚、海明威编写JavaScript代码 最近,我做了一个梦,我请海明威和其他四位文学名人替我编写 JavaScript 代码,一个函数返回一个给定长度的斐波纳契数列。有趣地是,他们每个人都以不同的方式完成了这一题,并且都做的很好只是我想说,每个解决方案工作起来都好比广告(即使是 Andre Breton 的),后来我又请包括海明威在内的 7 名文豪用 JavaScript 来编写阶乘,似乎一切都那么神奇,但他们都成功地完成了,大家不妨来看看结果吧。一、实现斐波纳契数列1. 欧内斯特海明威(Ernest Hemingway美国作家)代表作:老人与海太阳照样升起永别了,武器丧钟为谁而鸣
2、function fibonacci (size) var first = 0, second = 1, next, count = 2, 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;简洁明了的代码,没有任何多余的字母和变量,没有精心设计的逻辑和聪明的变量命名,非
3、常平淡地表明了它是做什么的,仅此而已,这就是海明威的魅力之处。2. 威廉莎士比亚(William Shakespeare 英国诗人、戏剧家)代表作: 哈姆雷特奥赛罗李尔王罗密欧与朱丽叶等function theSeriesOfFIBONACCI (theSize) /a CALCKULATION in two acts. /employng the humourous logick of JAVA-SCRIPTE /Dramatis Personae var theResult;/an ARRAY to contain THE NUMBERS var theCounter;/a NUMBER,
4、 servnt to the FOR LOOP /ACT I: in which a ZERO is added for INITIATION /ENTER: theResult /Upon the noble list bestow a zero var theResult = 0; /ACT II: a LOOP in which the final TWO NUMBERS are QUEREED and SUMMD /ENTER: theCounter /Commence at one and venture oer the numbers for (theCounter = 1; th
5、eCounter theSize; theCounter+) /By divination set adjoining members theResulttheCounter = (theResulttheCounter-1|1) + theResultMath.max (0, theCounter-2); /Tis done, and heres the answer. return theResult; /Exuent 大家可以看看莎士比亚是如何使用抑扬格五音步编写注释的。熟悉他戏剧的朋友应该对抑扬格五音步非常熟悉吧。3. 安德烈布勒东(Andre Breton 法国诗人)代表作:超现实主
6、义宣言function Colette (umbrella) var staircase = 0, galleons = 0, brigantines = 1, armada = galleons, brigantines, bassoon; Atotype.embrace = .push; while(2 + staircase+ umbrella) bassoon = galleons + brigantines; armada.embrace (brigantines = (galleons = brigantines, bassoon); return armada;整
7、个解决方案的逻辑非常清晰、优雅,在 galleons、brigantines、bassoons 这三个之间,使用逗号操作符对它们进行同时转移。4. 罗贝托波拉尼奥(Roberto Bolano 智利小说家、诗人)代表作:荒野侦探2666function LeonardoPisanoBigollo (l) if(l 0) return Id prefer not to respond. (Although several replies occur to me) /*/ /Everything is getting complicated. for (var i=2,r=0,1.slice (0
8、,l);il;r.push (ri-1+ri-2),i+) /*/ /Here are some other mathematicians. Mostly its 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, Brahmagu
9、pta, Bhaskara II, Nilakantha Somayaji, Omar Khayym, Muhammad ibn Ms al-Khwrizm, 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 Gdel, Jose
10、ph-Louis Lagrange, Georg Cantor, William Rowan Hamilton, Carl Jacobi, variste Galois, Nikolay Lobachevsky, Rene Descartes, Joseph Fourier, Pierre-Simon Laplace, Alonzo Church, Nikolay Bogolyubov /*/ /I didnt understand any of this, but here it is anyway. return r /*/ /Nothing happens here and if it
11、does Id rather not talk about it. 长短不齐的段落,缺少分号,使用隐式全局变量对每一个变量都做进一步深入的说明。5. 查尔斯狄更斯(Charles Dickens 英国小说家)代表作:匹克威克外传雾都孤儿 双城记 等function mrFibbowicksNumbers (enormity) var assortment = 0,1,1, tally = 3, artfulRatio = 1.61803; while(tally+ 1)/*mad to descend*/n-,/*madto multiply*/r = r * n/*and at the en
12、d, you see the blue center-lightpop, and everybody goes 1.4050061177528801e+51.*/r上面代码只能返回 43 的阶乘,如果想要实现另一个数的阶乘,你还需重写代码。不过,最值得注意地是,注释和代码几乎没有区别,无法分辨,从侧面反映了 Kerouac 的风格,很长、狂野和狂想式的流露。2. 塞缪尔约翰逊(Samuel Johnson 英国作家、诗人)代表作:英语大辞典伦敦人类欲望的虚幻等# In which various NUMBERS are summond by# means of ELECTRONICK CONJ
13、URYfactorial = (n) - # All argument is against it; yet all belief is for it return 1 unless n # Ingenious sophistry to prove the palpbly 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 return n * factorial n - 1整段代码比较稀疏,
14、中间也掺杂着 Johnson 的精言妙语:其中,他也对 factorial (0) 应该为 1 进行了怀疑,他应该用一个完整的句子来表达 factorial (1) 就是1,从其字典里进行推测,完成了一个具有讽刺定义的递归解除。Johnson 的整个解决方案充满着艺术和模仿的结合带有整洁表达式地温和的自我嘲讽和真正美丽的混合。3. 詹姆斯乔伊斯(James Joyce 爱尔兰作家、诗人)代表作:尤利西斯function hacktorial (integette) var nonthings = undefined, null, false, 0, , NaN; var resultution
15、 = 1; if (integette = 0) /behold the strangerous zeroine! resultution = 1; else while (integette 1) /caligulate by multicapables resultution = resultution * integette-; with(resultution) var duodismal = Function (return this) .call (toString (12); var disemvowel = Function (n,return n?parseInt (n,12
16、):0) return disemvowel (duodismal.slice (0,-1), shillings and, disemvowel (duodismalduodismal.length-1), pence .join ( ); /klikkaklakkaklaskaklopatzklatschabattacreppycrottygraddahappluddyappladdypkonpkot! Joyce 不仅仅是解决了阶乘问题,前半部分代码已经解决了这个问题,但 Joyce 坚决把结果转换为当时的货币:先令和便士。结果:hacktorial (3)/0 shillings an
17、d 6 pence hacktorial (4)/2 shillings and 0 pence hacktorial (7)/420 shillings and 0 pence hacktorial (21)/4257578514309120000 shillings and 0 pence4. 理查德费曼(Richard Feynman 美国物理学家)代表作:费曼物理学讲义物理之美/using Ramanujans approximation function fractorail (n) with(Math) var r = sqrt (PI)*pow (n/E,n); r *= pow
18、 (8*pow (n, 3) + 4*(n*n) + n + 1/30, 1/6); return r; 费曼的工作的特点是具有大胆的创意和非常古怪的才华,从它所编写的这段代码中也能看出。结果,不要担心一些“舍入误差”,如果你非常熟悉 JavaScript,你就应该知道:fractorail (3);/6.00005 fractorail (1.1);/1.04671 fractorail (5.2);/169.406285. 阿瑟柯南道尔(Arthur Conan Doyle 世界著名小说家)代表作:福尔摩斯探案集失落的世界use strict;/In solving a problem o
19、f 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(caseHistoryevidence)
20、 /elementary! return caseHistoryevidence; /third, eliminate the impossible! if(evidence = 0 | evidence % 24 != 0) return charlatans!; /fourth, deduction! var theDeduction, enumarator = evidence, denominator = 1; while(enumarator % denominator = 0) enumarator = enumarator/denominator+; if (enumarator
21、 = denominator) theDeduction = enumarator; theDeduction = theDeduction | impostors; /What one man can invent another can discover! caseHistoryevidence = theDeduction; /What remains, however improbable, must be the truth! return theDeduction;正如大家想象的那样, 柯南道尔的设计过程是精确和认真有序的,此外,大家可能注意到,他让其应用程序在 strict 模式
22、运行容不得半点马虎。6. 简奥斯汀(Jane Austen 英国小说家)代表作:理智与情感 傲慢与偏见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 /c
23、an only be adjudged truly agreeable by means of = if (quantity = 0) return 1; /Mr Crockford teaches that we be wary of inherited property. if (ledger.hasOwnProperty (quantity) return ledgerquantity; /Pray persist until an answer is furnished return ledgerquantity = quantity * reckoning (quantity - 1
24、); ;)();Jane Austen 解决方案的亮点之一是代码工艺和结构的完整性,主要体现在代码块的整洁上,她调用模块模式,隐藏历史数据(或 ledger)在上层结构的文件夹里;第二个则是体现了 Jane Austen 的俏皮,甚至是颠覆性的,对 power 的讽刺和荒谬的约定。7. 欧内斯特海明威(Ernest Hemingway 美国小说家)代表作:老人与海太阳照样升起等/Economy. function factorial (n) return n 2 ? 1: factorial (n-1)*n;代码足以说明一切。我们从中所学到的这些著名的小说家、诗人、戏剧家的伟大之处在于他们勇于
25、开拓新方式,尝试新的技术并且打破我们一直所遵循的规律。最优秀的英语作家具有着对规则的蔑视。Steven PinkerJavaScript 作为最流行的编程语言之一,它的进步取决于开发者们地大胆创新、研究以及发现新模式,这样才能惠及更多的人。allied mechanical maintenance group, lawn maintenance, plant protection group. Establishment of conservation of garden plants in long sections, professional engineers, professional
26、 technicians and other related jobs. 1, the maintenance lead responsibilities (1) familiar with the Groups foundations of political thought, technology, culture, work attitudes, feelings and emotions, habits and other personal problems, easy to work. (2) before work, quality of work, requirements en
27、gineering, safety, mechanical equipment and some of the technical issues seriously disclosure to workers, and keep the information in writing. (3) when assigning production tasks to the workers, to implement safety measures, complete the task to do self-examination record. (4) section chiefs be vanguard of work, must step forward are familiar with the relevant business, work in the field, hard, dirty work, heavy work need to be at the head of work to do before charge in and retreat. (5) pay attention to every team, every process of close cooperation and
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电商行业社交电商与直播带货方案
- 质量管理体系建设指导书模板
- 采购供应商信息评估与分析工具
- 采购成本控制流程与操作指南模板
- 员工离职信息安全管理团队预案
- 产品质量担保期限延长承诺书(6篇)
- 活动赞助资金支配承诺书(9篇)
- 湖北省南漳县2026年初三第一次诊断考试英语试题文试题含解析
- 隐秘信息严格保护承诺书(8篇)
- 2026九年级语文下册第五单元阅读重点突破作业课件新人教版
- 中学语文课程标准与教材研究 第2版 课件全套 第1-6章 语文课程-语文课程资源
- 《生物信息学课件》课件
- T-CCTAS 34-2022 带肋钢筋轴向冷挤压连接技术规程
- 村文书考试题及答案甘肃
- 十二星座知识及性格课件
- 2025年浙江工贸职业技术学院单招职业倾向性考试题库及答案1套
- 高职应用语文教程(第二版)教案 上篇 文学鉴赏
- 《特种设备重大事故隐患判定准则图解》
- 乡村振兴 高素质农民培养规范 (DB3205T 1138-2024)
- 管道设备钢结构拆除施工方案
- 腮腺炎防治知识培训讲座
评论
0/150
提交评论