-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoffeescript.html
179 lines (143 loc) · 8.1 KB
/
coffeescript.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<html lang="en">
<head>
<link href="/images/favicon.png" rel="icon">
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>trvrm.github.io</title>
<link rel="stylesheet" type="text/css" href="/theme/css/flatly.min.css" />
<link rel="stylesheet" type="text/css" href="/theme/css/style.css" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" />
<link href="/theme/css/pygments/tango.css" rel="stylesheet">
<meta name="tags" content="coffeescript" />
<meta name="tags" content="javascript" />
</head>
<body>
<section class="hero is-primary">
<!-- Hero header: will stick at the top -->
<div class="hero-head">
<nav class="navbar ">
<div class="navbar-menu is-active">
<div class="navbar-end">
<a class="navbar-item" href="https://twitter.com/trvrm">
<span class="icon"> <i class="fa fa-twitter"></i> </span>
twitter
</a>
<a class="navbar-item" href="https://github.com/trvrm">
<span class="icon"> <i class="fa fa-github"></i> </span>
github
</a>
</div>
</div>
</div>
</div>
<!-- Hero content: will be in the middle -->
<div class="hero-body">
<div class="container has-text-centered">
<p class="title is-1">trvrm.github.io</p>
</div>
</div>
<div class="hero-foot">
<nav class="navbar ">
<div class="navbar-brand is-active">
<a href="/" class="navbar-item" >
trvrm.github.io
</a>
</div>
<div class="navbar-menu is-active">
<div class="navbar-start">
<a class="navbar-item " href="/category/database.html">Database</a>
<a class="navbar-item is-active " href="/category/software.html">Software</a>
<a class="navbar-item " href="/category/systems.html">Systems</a>
</div>
</div>
</nav>
</div>
</section>
<section class="section">
<div class="container has-text-centered">
<p class="title is-3">
<a href="/coffeescript.html" rel="bookmark" title="Permalink to CoffeeScript">
CoffeeScript
</a>
</p>
<p class="subtitle is-5">
Thu 01 January 2015
</p>
</div>
<hr>
<div class="content ">
<p>Through a bizarre twist of history, the entire client-side web runs on a <a class="reference external" href="http://en.wikipedia.org/wiki/Brendan_Eich#Netscape_and_JavaScript">language</a>
that was thrown together in 10 days.</p>
<p>Despite huge investments in their own proprietary technology by the likes of Sun
Microsystems, Adobe and Microsoft, this weird little spinoff of <strong>self</strong> and <strong>scheme</strong>
is everywhere, while client-side Java, ActiveX and Flash fade into obscurity.</p>
<p>Unsurprisingly for a language developed so quickly, Javascript is pretty ugly.
I'm fond of saying that it's a horrible language, with a really nice language
inside trying to get out. It gets some things, like scoping rules, very, very
wrong. But it got other things, like anonymous functions, exactly right, long before
they were adopted in Java, C#, or C++. Even Python, my favourite language ever,
doesn't get them quite right.</p>
<p>Several people have attempted to build a nicer syntax on top of the javascript
virtual machine. In fact, the <a class="reference external" href="https://github.com/jashkenas/coffeescript/wiki/List-of-languages-that-compile-to-JS">list</a> of languages that compile to JS is startlingly
big.</p>
<p>For the last couple of years I've been using <a class="reference external" href="http://coffeescript.org/">CoffeeScript</a> as my standard javascript syntax.</p>
<p>From the project page:</p>
<blockquote>
"CoffeeScript is a little language that compiles into JavaScript.
Underneath that awkward Java-esque patina, JavaScript has always
had a gorgeous heart. CoffeeScript is an attempt to expose the
good parts of JavaScript in a simple way."</blockquote>
<p>and I think it achieves this admirably. It doesn't solve <em>all</em> of javascript's problems -
you can still get into trouble with the Infamous Loop Problem, but it does make the language
considerably more succinct, mostly by stealing ideas from Python and Haskell.</p>
<div class="section" id="examples">
<h2>Examples</h2>
<p>Function definitions go from</p>
<div class="highlight"><pre><span></span><span class="kd">function</span> <span class="nx">d</span><span class="p">(</span><span class="nx">x</span><span class="p">){</span>
<span class="k">return</span> <span class="mi">2</span><span class="o">*</span><span class="nx">x</span>
<span class="p">}</span>
</pre></div>
<p>to</p>
<div class="highlight"><pre><span></span><span class="nv">d = </span><span class="nf">(x) -></span> <span class="mi">2</span><span class="o">*</span><span class="nx">x</span>
</pre></div>
<p>This makes for very quick object construction:</p>
<div class="highlight"><pre><span></span><span class="nv">math =</span>
<span class="nv">root: </span> <span class="nb">Math</span><span class="p">.</span><span class="nx">sqrt</span>
<span class="nv">square: </span><span class="nx">square</span>
<span class="nv">cube: </span> <span class="nf">(x) -></span> <span class="nx">x</span> <span class="o">*</span> <span class="nx">square</span> <span class="nx">x</span>
</pre></div>
<p>It also borrows Python's list comprehension syntax:</p>
<div class="highlight"><pre><span></span><span class="nx">values</span><span class="o">=</span><span class="p">(</span><span class="nx">option</span><span class="p">.</span><span class="nx">value</span> <span class="k">for</span> <span class="nx">option</span> <span class="k">in</span> <span class="nx">question</span><span class="p">.</span><span class="nx">options</span><span class="p">)</span>
</pre></div>
<p>The near complete absense of curly brackets saves a lot of wasted lines in
my source code, and enables me to see what's going on a lot clearer than in raw
javascript. On the downside, I do find myself fairly regularly testing out code
snippets in the <a class="reference external" href="http://coffeescript.org/">CoffeeScript</a> online compiler to make sure that I've properly understood
how they will be interpreted.</p>
<p>Because CoffeeScript is a compiled language, to work with it effectively requires
integrating the compiler into your toolchain. For my larger projects I've hand-written
a tool using Python's <a class="reference external" href="https://pypi.python.org/pypi/watchdog">Watchdog</a> package to monitor my source code directories and
output compiled javascript everytime a file changes.</p>
<p>As a nice little extra, my tool jams in a warning message wrapped in an <code class="code">
alert</code>
call
if the compliation fails, so if I introduce a syntax error in my coffeescript, as soon
as I refresh the page that is using it I'll be presented with the source of the problem.</p>
</div>
</div>
</section>
<footer class="footer">
<div class="container">
<div class="content has-text-centered">
<p>
Powered by <a href="http://getpelican.com/">Pelican</a>, <a href="http://python.org">Python</a>,
and <a href="http://bulma.io/">Bulma</a>
</p>
<p class="subtitle is-6">Ubi Caritas et Amor, Deus Ibi Est</p>
</div>
</div>
</footer>
</body>
</html>