forked from jashkenas/coffeescript
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
3185 lines (2819 loc) · 163 KB
/
index.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>CoffeeScript</title>
<link rel="canonical" href="http://coffeescript.org" />
<link rel="stylesheet" type="text/css" href="documentation/css/docs.css" />
<link rel="stylesheet" type="text/css" href="documentation/css/tomorrow.css" />
<link rel="shortcut icon" href="documentation/images/favicon.ico" />
</head>
<body>
<div id="fadeout"></div>
<div id="flybar">
<a id="logo" href="#top"><img src="documentation/images/logo.png" width="225" height="39" alt="CoffeeScript" /></a>
<div class="navigation toc">
<div class="button">
Table of Contents
</div>
<div class="contents menu">
<a href="#overview">Overview</a>
<a href="#installation">Installation</a>
<a href="#usage">Usage</a>
<a href="#literate">Literate CoffeeScript</a>
<a href="#language">Language Reference</a>
<a href="#literals">Literals: Functions, Objects and Arrays</a>
<a href="#lexical-scope">Lexical Scoping and Variable Safety</a>
<a href="#conditionals">If, Else, Unless, and Conditional Assignment</a>
<a href="#splats">Splats...</a>
<a href="#loops">Loops and Comprehensions</a>
<a href="#slices">Array Slicing and Splicing</a>
<a href="#expressions">Everything is an Expression</a>
<a href="#operators">Operators and Aliases</a>
<a href="#classes">Classes, Inheritance, and Super</a>
<a href="#destructuring">Destructuring Assignment</a>
<a href="#fat-arrow">Bound and Generator Functions</a>
<a href="#embedded">Embedded JavaScript</a>
<a href="#switch">Switch and Try/Catch</a>
<a href="#comparisons">Chained Comparisons</a>
<a href="#strings">String Interpolation, Block Strings, and Block Comments</a>
<a href="#regexes">Block Regular Expressions</a>
<a href="#cake">Cake, and Cakefiles</a>
<a href="#source-maps">Source Maps</a>
<a href="#scripts">"text/coffeescript" Script Tags</a>
<a href="#resources">Books, Screencasts, Examples and Resources</a>
<a href="#changelog">Change Log</a>
</div>
</div>
<div class="navigation try">
<div class="button">
Try CopheeScript
<div class="repl_bridge"></div>
</div>
<div class="contents repl_wrapper">
<div class="code">
<div class="screenshadow tl"></div>
<div class="screenshadow tr"></div>
<div class="screenshadow bl"></div>
<div class="screenshadow br"></div>
<div id="repl_source_wrap">
<textarea id="repl_source" rows="100" spellcheck="false">echo "Hello CoffeeScript!"</textarea>
</div>
<div id="repl_results_wrap"><pre id="repl_results"></pre></div>
<div class="minibutton dark run" title="Ctrl-Enter">Run</div>
<a class="minibutton permalink" id="repl_permalink">Link</a>
<br class="clear" />
</div>
</div>
</div>
<div class="navigation annotated">
<div class="button">
Annotated Source
</div>
<div class="contents menu">
<a href="documentation/docs/grammar.html">Grammar Rules — src/grammar</a>
<a href="documentation/docs/lexer.html">Lexing Tokens — src/lexer</a>
<a href="documentation/docs/rewriter.html">The Rewriter — src/rewriter</a>
<a href="documentation/docs/nodes.html">The Syntax Tree — src/nodes</a>
<a href="documentation/docs/scope.html">Lexical Scope — src/scope</a>
<a href="documentation/docs/helpers.html">Helpers & Utility Functions — src/helpers</a>
<a href="documentation/docs/coffee-script.html">The CoffeeScript Module — src/coffee-script</a>
<a href="documentation/docs/cake.html">Cake & Cakefiles — src/cake</a>
<a href="documentation/docs/command.html">"coffee" Command-Line Utility — src/command</a>
<a href="documentation/docs/optparse.html">Option Parsing — src/optparse</a>
<a href="documentation/docs/repl.html">Interactive REPL — src/repl</a>
<a href="documentation/docs/sourcemap.html">Source Maps — src/sourcemap</a>
</div>
</div>
</div>
<div class="container">
<span class="bookmark" id="top"></span>
<p>
<b>CopheeScript is a little language that compiles into PHP.</b>
It derives from <a href='http://coffeescript.org'>CoffeeScript</a>,
a popular (and gorgeous) language that compiles into JavaScript.
It adds a few PHP-specific features while aiming to support as much
CoffeeScript syntax as possible and similarly beautify your
server-side code.
</p>
<p>
The golden rule of CopheeScript is: <i>"It's just PHP"</i>. The code
compiles one-to-one into the equivalent PHP, and there is
no interpretation at runtime. You can use any existing PHP library
seamlessly from CopheeScript (and vice-versa). The compiled output is
(somewhat) readable and pretty-printed. It targets PHP >= 5.4 (with a few
language features for newer PHP runtimes only).
</p>
<p>
<b>Latest Version:</b>
<a href="http://github.com/jashkenas/coffeescript/tarball/1.9.1">1.9.1</a>
</p>
<pre>npm install -g coffee-script</pre>
<h2>
<span id="overview" class="bookmark"></span>
Overview
</h2>
<p><i>CopheeScript on the left, compiled PHP output on the right.</i></p>
<div class='code'><pre><code><span class="comment"># Assignment:</span>
$number = <span class="number">42</span>
$opposite = <span class="literal">true</span>
<span class="comment"># Conditions:</span>
$number = -<span class="number">42</span> <span class="keyword">if</span> $opposite
<span class="comment"># Functions:</span>
<span class="function"><span class="title">$square</span> = <span class="params">( $x )</span> -></span> $x * $x
<span class="comment"># Arrays:</span>
$list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]
<span class="comment"># Hashes:</span>
$math_hash =
<span class="attribute">root</span>: <span class="string">'sqrt'</span>
<span class="attribute">square</span>: $square
<span class="attribute">cube</span>: <span class="function"><span class="params">( $x )</span> -></span> $x * $square $x
<span class="comment"># Objects:</span>
$math = {{
<span class="attribute">root</span>: <span class="string">'sqrt'</span>
<span class="attribute">square</span>: $square
<span class="attribute">cube</span>: <span class="function"><span class="params">( $x )</span> -></span> $x * $square $x
}}
<span class="comment"># Splats:</span>
<span class="function"><span class="title">$race</span> = <span class="params">( $winner, $runners... )</span> -></span>
var_dump $winner, $runners
<span class="comment"># Existence:</span>
echo <span class="string">"I knew it!"</span> <span class="keyword">if</span> $elvis?
<span class="comment"># Array comprehensions:</span>
$cubes = ( $math.cube $num <span class="keyword">for</span> $num <span class="keyword">of</span> $list )
</code></pre><pre><code>$number = <span class="number">42</span>;
$opposite = <span class="literal">true</span>;
<span class="keyword">if</span> ($opposite) {
$number = -<span class="number">42</span>;
}
$square = <span class="function"><span class="keyword">function</span><span class="params">($x)</span> {</span>
<span class="keyword">return</span> $x * $x;
};
$list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];
$math_hash = [
<span class="string">'root'</span> => <span class="string">'sqrt'</span>,
<span class="string">'square'</span> => $square,
<span class="string">'cube'</span> => <span class="function"><span class="keyword">function</span><span class="params">($x)</span> <span class="title">use</span> <span class="params">(&$square)</span> {</span>
<span class="keyword">return</span> $x * $square($x);
}
];
$math = (object) [
<span class="string">'root'</span> => <span class="string">'sqrt'</span>,
<span class="string">'square'</span> => $square,
<span class="string">'cube'</span> => <span class="function"><span class="keyword">function</span><span class="params">($x)</span> <span class="title">use</span> <span class="params">(&$square)</span> {</span>
<span class="keyword">return</span> $x * $square($x);
}
];
$race = <span class="function"><span class="keyword">function</span><span class="params">()</span> {</span>
$__args = func_get_args();
$winner = $__args[<span class="number">0</span>]; $runners = array_slice($__args, <span class="number">1</span>);
<span class="keyword">return</span> var_dump($winner, $runners);
};
<span class="keyword">if</span> ( isset( $elvis )) {
echo(<span class="string">"I knew it!"</span>);
}
$cubes = call_user_func((<span class="function"><span class="keyword">function</span><span class="params">()</span> <span class="title">use</span> <span class="params">(&$list,&$math)</span> {</span>
$results = [];
foreach ($list as $num) {
$results[] = $math->cube($num);
}
<span class="keyword">return</span> $results;
}));
</code></pre><script>window.example1 = "# Assignment:\n$number = 42\n$opposite = true\n\n# Conditions:\n$number = -42 if $opposite\n\n# Functions:\n$square = ( $x ) -> $x * $x\n\n# Arrays:\n$list = [1, 2, 3, 4, 5]\n\n# Hashes:\n$math_hash =\n root: 'sqrt'\n square: $square\n cube: ( $x ) -> $x * $square $x\n\n# Objects:\n$math = {{\n root: 'sqrt'\n square: $square\n cube: ( $x ) -> $x * $square $x\n}}\n\n# Splats:\n$race = ( $winner, $runners... ) ->\n var_dump $winner, $runners\n\n# Existence:\necho \"I knew it!\" if $elvis?\n\n# Array comprehensions:\n$cubes = ( $math.cube $num for $num of $list )\n"</script><br class='clear' /></div>
<h2>
<span id="installation" class="bookmark"></span>
Installation
</h2>
<p>
The CoffeeScript compiler is itself
<a href="documentation/docs/grammar.html">written in CoffeeScript</a>,
using the <a href="http://jison.org">Jison parser generator</a>. The
command-line version of <tt>coffee</tt> is available as a
<a href="http://nodejs.org/">Node.js</a> utility. The
<a href="extras/coffee-script.js">core compiler</a> however, does not
depend on Node, and can be run in any JavaScript environment, or in the
browser (see "Try CoffeeScript", above).
</p>
<p>
To install, first make sure you have a working copy of the latest stable version of
<a href="http://nodejs.org/">Node.js</a>. You can then install CoffeeScript globally
with <a href="http://npmjs.org">npm</a>:
</p>
<pre>
npm install -g coffee-script</pre>
<p>
When you need CoffeeScript as a dependency, install it locally:
</p>
<pre>
npm install --save coffee-script</pre>
<p>
If you'd prefer to install the latest <b>master</b> version of CoffeeScript, you
can clone the CoffeeScript
<a href="http://github.com/jashkenas/coffeescript">source repository</a>
from GitHub, or download
<a href="http://github.com/jashkenas/coffeescript/tarball/master">the source</a> directly.
To install the latest master CoffeeScript compiler with npm:
</p>
<pre>
npm install -g jashkenas/coffeescript</pre>
<p>
Or, if you want to install to <tt>/usr/local</tt>, and don't want to use
npm to manage it, open the <tt>coffee-script</tt> directory and run:
</p>
<pre>
sudo bin/cake install</pre>
<h2>
<span id="usage" class="bookmark"></span>
Usage
</h2>
<p>
Once installed, you should have access to the <tt>coffee</tt> command,
which can execute scripts, compile <tt>.coffee</tt> files into <tt>.js</tt>,
and provide an interactive REPL. The <tt>coffee</tt> command takes the
following options:
</p>
<table>
<tr>
<td><code>-c, --compile</code></td>
<td>
Compile a <tt>.coffee</tt> script into a <tt>.js</tt> JavaScript file
of the same name.
</td>
</tr>
<tr>
<td><code>-m, --map</code></td>
<td>
Generate source maps alongside the compiled JavaScript files. Adds
<tt>sourceMappingURL</tt> directives to the JavaScript as well.
</td>
</tr>
<tr>
<td width="25%"><code>-i, --interactive</code></td>
<td>
Launch an interactive CoffeeScript session to try short snippets.
Identical to calling <tt>coffee</tt> with no arguments.
</td>
</tr>
<tr>
<td><code>-o, --output [DIR]</code></td>
<td>
Write out all compiled JavaScript files into the specified directory.
Use in conjunction with <tt>--compile</tt> or <tt>--watch</tt>.
</td>
</tr>
<tr>
<td><code>-j, --join [FILE]</code></td>
<td>
Before compiling, concatenate all scripts together in the order they
were passed, and write them into the specified file.
Useful for building large projects.
</td>
</tr>
<tr>
<td><code>-w, --watch</code></td>
<td>
Watch files for changes, rerunning the specified command when any
file is updated.
</td>
</tr>
<tr>
<td><code>-p, --print</code></td>
<td>
Instead of writing out the JavaScript as a file, print it
directly to <b>stdout</b>.
</td>
</tr>
<tr>
<td><code>-s, --stdio</code></td>
<td>
Pipe in CoffeeScript to STDIN and get back JavaScript over STDOUT.
Good for use with processes written in other languages. An example:<br />
<tt>cat src/cake.coffee | coffee -sc</tt>
</td>
</tr>
<tr>
<td><code>-l, --literate</code></td>
<td>
Parses the code as Literate CoffeeScript. You only need to specify
this when passing in code directly over <b>stdio</b>, or using some sort
of extension-less file name.
</td>
</tr>
<tr>
<td><code>-e, --eval</code></td>
<td>
Compile and print a little snippet of CoffeeScript directly from the
command line. For example:<br /><tt>coffee -e "console.log num for num in [10..1]"</tt>
</td>
</tr>
<tr>
<td><code>-b, --bare</code></td>
<td>
Compile the JavaScript without the
<a href="#lexical-scope">top-level function safety wrapper</a>.
</td>
</tr>
<tr>
<td><code>-t, --tokens</code></td>
<td>
Instead of parsing the CoffeeScript, just lex it, and print out the
token stream: <tt>[IDENTIFIER square] [ASSIGN =] [PARAM_START (]</tt> ...
</td>
</tr>
<tr>
<td><code>-n, --nodes</code></td>
<td>
Instead of compiling the CoffeeScript, just lex and parse it, and print
out the parse tree:
<pre class="no_bar">
Expressions
Assign
Value "square"
Code "x"
Op *
Value "x"
Value "x"</pre>
</td>
</tr>
<tr>
<td><code>--nodejs</code></td>
<td>
The <tt>node</tt> executable has some useful options you can set,
such as<br /> <tt>--debug</tt>, <tt>--debug-brk</tt>, <tt>--max-stack-size</tt>,
and <tt>--expose-gc</tt>. Use this flag to forward options directly to Node.js.
To pass multiple flags, use <tt>--nodejs</tt> multiple times.
</td>
</tr>
</table>
<p>
<b>Examples:</b>
</p>
<ul>
<li>
Compile a directory tree of <tt>.coffee</tt> files in <tt>src</tt> into a parallel
tree of <tt>.js</tt> files in <tt>lib</tt>:<br />
<tt>coffee --compile --output lib/ src/</tt>
</li>
<li>
Watch a file for changes, and recompile it every time the file is saved:<br />
<tt>coffee --watch --compile experimental.coffee</tt>
</li>
<li>
Concatenate a list of files into a single script:<br />
<tt>coffee --join project.js --compile src/*.coffee</tt>
</li>
<li>
Print out the compiled JS from a one-liner:<br />
<tt>coffee -bpe "alert i for i in [0..10]"</tt>
</li>
<li>
All together now, watch and recompile an entire project as you work on it:<br />
<tt>coffee -o lib/ -cw src/</tt>
</li>
<li>
Start the CoffeeScript REPL (<tt>Ctrl-D</tt> to exit, <tt>Ctrl-V</tt>for multi-line):<br />
<tt>coffee</tt>
</li>
</ul>
<h2>
<span id="literate" class="bookmark"></span>
Literate CoffeeScript
</h2>
<p>
Besides being used as an ordinary programming language, CoffeeScript may
also be written in "literate" mode. If you name your file with a
<tt>.litcoffee</tt> extension, you can write it as a Markdown document —
a document that also happens to be executable CoffeeScript code. The compiler
will treat any indented blocks (Markdown's way of indicating source code)
as code, and ignore the rest as comments.
</p>
<p>
Just for kicks, a little bit of the compiler is currently implemented in this fashion:
See it
<a href="https://gist.github.com/jashkenas/3fc3c1a8b1009c00d9df">as a document</a>,
<a href="https://raw.github.com/jashkenas/coffeescript/master/src/scope.litcoffee">raw</a>,
and <a href="http://cl.ly/LxEu">properly highlighted in a text editor</a>.
</p>
<p>
I'm fairly excited about this direction for the language, and am looking
forward to writing (and more importantly, reading) more programs in this style.
More information about Literate CoffeeScript, including an
<a href="https://github.com/jashkenas/journo">example program</a>,
are <a href="http://ashkenas.com/literate-coffeescript">available in this blog post</a>.
</p>
<h2>
<span id="language" class="bookmark"></span>
Language Reference
</h2>
<p>
<i>
This reference is structured so that it can be read from top to bottom,
if you like. Later sections use ideas and syntax previously introduced.
Familiarity with PHP is assumed.
In all of the following examples, the source CopheeScript is provided on
the left, and the direct compilation into PHP is on the right.
</i>
</p>
<p>
<i>
Many of the examples can be loaded into the "Try CopheeScript"
console by pressing the <b>load</b> button on the left.
</i>
<p>
First, the basics: CopheeScript uses significant whitespace to delimit blocks of code.
You don't need to use semicolons <tt>;</tt> to terminate expressions,
ending the line will do just as well (although semicolons can still
be used to fit multiple expressions onto a single line).
Instead of using curly braces
<tt>{ }</tt> to surround blocks of code in <a href="#literals">functions</a>,
<a href="#conditionals">if-statements</a>,
<a href="#switch">switch</a>, and <a href="#try">try/catch</a>,
use indentation.
</p>
<p>
You don't need to use parentheses to invoke a function if you're passing
arguments. The implicit call wraps forward to the end of the line or block expression.<br />
<tt>var_dump array_keys $object</tt> → <tt>var_dump(array_keys($object));</tt>
</p>
<p>
<span id="literals" class="bookmark"></span>
<b class="header">Functions</b>
Functions are defined by an optional list of parameters in parentheses,
an arrow, and the function body. The empty function looks like this:
<tt>-></tt>
</p>
<div class='code'><pre><code><span class="function"><span class="title">$square</span> = <span class="params">( $x )</span> -></span> $x * $x
<span class="function"><span class="title">$cube</span> = <span class="params">( $x )</span> -></span> $square( $x ) * $x
</code></pre><pre><code>$square = <span class="function"><span class="keyword">function</span><span class="params">($x)</span> {</span>
<span class="keyword">return</span> $x * $x;
};
$cube = <span class="function"><span class="keyword">function</span><span class="params">($x)</span> <span class="title">use</span> <span class="params">(&$square)</span> {</span>
<span class="keyword">return</span> $square($x) * $x;
};
</code></pre><script>window.example2 = "$square = ( $x ) -> $x * $x\n$cube = ( $x ) -> $square( $x ) * $x\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example2);'>load</div><br class='clear' /></div>
<p>
Functions may also have default values for arguments (like PHP), which will be used
if the incoming argument is missing.
</p>
<div class='code'><pre><code><span class="function"><span class="title">$fill</span> = <span class="params">( $container, $liquid = <span class="string">"coffee"</span> )</span> -></span>
<span class="string">"Filling the <span class="subst">#{ $container }</span> with <span class="subst">#{ $liquid }</span>..."</span>
</code></pre><pre><code>$fill = function($container, $liquid="coffee") {
return "Filling the " . $container . " with " . $liquid . "...";
};
</code></pre><script>window.example3 = "$fill = ( $container, $liquid = \"coffee\" ) ->\n \"Filling the #{ $container } with #{ $liquid }...\"\n\n\n\n\n\n\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example3);'>load</div><br class='clear' /></div>
<p>
To specify typed function arguments, you generally have to use the hacky syntax
<tt>Type_$arg</tt>. The only exception is that the final argument to a function
can use the expected whitespace-separated <tt>Type $arg</tt> syntax.
</p>
<div class='code'><pre><code><span class="function"><span class="title">$first_n</span> = <span class="params">( array_$arr, $n )</span> -></span>
array_slice $arr, <span class="number">0</span>, $n
<span class="function"><span class="title">$first_one</span> = <span class="params">( array $arr )</span> -></span>
$arr[<span class="number">0</span>]
</code></pre><pre><code>$first_n = <span class="function"><span class="keyword">function</span><span class="params">(array $arr, $n)</span> {</span>
<span class="keyword">return</span> array_slice($arr, <span class="number">0</span>, $n);
};
$first_one = <span class="function"><span class="keyword">function</span><span class="params">(array $arr)</span> {</span>
<span class="keyword">return</span> $arr[<span class="number">0</span>];
};
</code></pre><script>window.example4 = "$first_n = ( array_$arr, $n ) ->\n array_slice $arr, 0, $n\n\n$first_one = ( array $arr ) ->\n $arr[0]\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example4);'>load</div><br class='clear' /></div>
<p>
The "killer feature" of CopheeScript (especially when coding in a more
functional style) is auto-generated <tt>use</tt> clauses on closures.
This means that variables from outer scopes are able to be freely
referenced (and they are in fact always passed by reference) inside closures.
</p>
<div class='code'><pre><code>$x = <span class="number">6</span>
<span class="function"><span class="title">$add_x_and_arg</span> = <span class="params">( $y )</span> -></span>
<span class="function"><span class="params">( $z )</span> -></span>
$x + $y + $z
$add_x_and_arg( <span class="number">4</span> )( <span class="number">5</span> ) <span class="comment"># 15</span>
</code></pre><pre><code>$x = <span class="number">6</span>;
$add_x_and_arg = <span class="function"><span class="keyword">function</span><span class="params">($y)</span> <span class="title">use</span> <span class="params">(&$x)</span> {</span>
<span class="keyword">return</span> <span class="function"><span class="keyword">function</span><span class="params">($z)</span> <span class="title">use</span> <span class="params">(&$x,&$y)</span> {</span>
<span class="keyword">return</span> $x + $y + $z;
};
};
call_user_func($add_x_and_arg(<span class="number">4</span>), <span class="number">5</span>);
</code></pre><script>window.example5 = "$x = 6\n\n$add_x_and_arg = ( $y ) ->\n ( $z ) ->\n $x + $y + $z\n\n$add_x_and_arg( 4 )( 5 ) # 15\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example5);'>load</div><br class='clear' /></div>
<p>
<span id="objects_and_arrays" class="bookmark"></span>
<b class="header">Objects and Arrays</b>
For syntactic convenience,
CopheeScript has different literals for hashes (associative arrays) and
lists even though both compile to PHP arrays.
Object literals are similar to hash literals but use double curly braces (<tt>{{</tt>).
When each property is listed on its own line,
the commas are optional. Objects/hashes may be created using indentation instead
of explicit braces, similar to <a href="http://yaml.org">YAML</a>.
Object properties and methods are accessed using the dot (<tt>.</tt>) operator.
</p>
<div class='code'><pre><code>$song = [<span class="string">"do"</span>, <span class="string">"re"</span>, <span class="string">"mi"</span>, <span class="string">"fa"</span>, <span class="string">"so"</span>]
$singers = { <span class="attribute">Jagger</span>: <span class="string">"Rock"</span>, <span class="attribute">Elvis</span>: <span class="string">"Roll"</span> }
$singers_obj = {{ <span class="attribute">Jagger</span>: <span class="string">"Rock"</span>, <span class="attribute">Elvis</span>: <span class="string">"Roll"</span> }}
echo $singers_obj.Jagger
$bitlist = [
<span class="number">1</span>, <span class="number">0</span>, <span class="number">1</span>
<span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>
<span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span>
]
$kids =
<span class="attribute">brother</span>:
<span class="attribute">name</span>: <span class="string">"Max"</span>
<span class="attribute">age</span>: <span class="number">11</span>
<span class="attribute">sister</span>:
<span class="attribute">name</span>: <span class="string">"Ida"</span>
<span class="attribute">age</span>: <span class="number">9</span>
</code></pre><pre><code>$song = [<span class="string">"do"</span>, <span class="string">"re"</span>, <span class="string">"mi"</span>, <span class="string">"fa"</span>, <span class="string">"so"</span>];
$singers = [
<span class="string">'Jagger'</span> => <span class="string">"Rock"</span>,
<span class="string">'Elvis'</span> => <span class="string">"Roll"</span>
];
$singers_obj = (object) [
<span class="string">'Jagger'</span> => <span class="string">"Rock"</span>,
<span class="string">'Elvis'</span> => <span class="string">"Roll"</span>
];
echo($singers_obj->Jagger);
$bitlist = [<span class="number">1</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span>];
$kids = [
<span class="string">'brother'</span> => [
<span class="string">'name'</span> => <span class="string">"Max"</span>,
<span class="string">'age'</span> => <span class="number">11</span>
],
<span class="string">'sister'</span> => [
<span class="string">'name'</span> => <span class="string">"Ida"</span>,
<span class="string">'age'</span> => <span class="number">9</span>
]
];
</code></pre><script>window.example6 = "$song = [\"do\", \"re\", \"mi\", \"fa\", \"so\"]\n\n$singers = { Jagger: \"Rock\", Elvis: \"Roll\" }\n\n$singers_obj = {{ Jagger: \"Rock\", Elvis: \"Roll\" }}\necho $singers_obj.Jagger\n\n$bitlist = [\n 1, 0, 1\n 0, 0, 1\n 1, 1, 0\n]\n\n$kids =\n brother:\n name: \"Max\"\n age: 11\n sister:\n name: \"Ida\"\n age: 9\n\n\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example6);'>load</div><br class='clear' /></div>
<p>
There are a few shorthands for when keys in a hash/object literal correspond to the
variable/property name of the value. <small><i>The shorthands starting with <tt>.</tt> are
tailored for our ORM at Zeel, where we use <tt>.column_name: value</tt> hashes to
specify SQL <tt>WHERE</tt> clauses.</i></small>
</p>
<div class='code'><pre><code>$snack = <span class="string">'peanuts'</span>
$drink = <span class="string">'beer'</span>
<span class="property">@seat</span> = <span class="string">'blanket'</span>
<span class="property">@company</span> = <span class="string">'you'</span>
$picnic = { snack, <span class="property">@seat</span>, .drink, .<span class="property">@company</span> }
</code></pre><pre><code>$snack = <span class="string">'peanuts'</span>;
$drink = <span class="string">'beer'</span>;
$<span class="keyword">this</span>->seat = <span class="string">'blanket'</span>;
$<span class="keyword">this</span>->company = <span class="string">'you'</span>;
$picnic = [
<span class="string">'snack'</span> => $snack,
<span class="string">'seat'</span> => $<span class="keyword">this</span>->seat,
<span class="string">'.drink'</span> => $drink,
<span class="string">'.company'</span> => $<span class="keyword">this</span>->company
];
</code></pre><script>window.example7 = "$snack = 'peanuts'\n$drink = 'beer'\n@seat = 'blanket'\n@company = 'you'\n\n$picnic = { snack, @seat, .drink, .@company }\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example7);'>load</div><br class='clear' /></div>
<!-- <p> -->
<!-- In JavaScript, you can't use reserved words, like <tt>class</tt>, as properties -->
<!-- of an object, without quoting them as strings. CoffeeScript notices reserved words -->
<!-- used as keys in objects and quotes them for you, so you don't have to worry -->
<!-- about it (say, when using jQuery). -->
<!-- </p> -->
<!-- <div class='code'><pre><code>$('.account').attr class: 'active'
echo $object.class
</code></pre><pre><code>$(<span class="string">'.account'</span>)->attr([
<span class="string">'class'</span> => <span class="string">'active'</span>
]);
echo($object-><span class="keyword">class</span>);
</code></pre><script>window.example8 = "$('.account').attr class: 'active'\n\necho $object.class\n\n\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example8);'>load</div><br class='clear' /></div> -->
<!-- <p> -->
<!-- <span id="lexical-scope" class="bookmark"></span> -->
<!-- <b class="header">Lexical Scoping and Variable Safety</b> -->
<!-- The CoffeeScript compiler takes care to make sure that all of your variables -->
<!-- are properly declared within lexical scope — you never need to write -->
<!-- <tt>var</tt> yourself. -->
<!-- </p> -->
<!-- <div class='code'><pre><code>$outer = <span class="number">1</span>
<span class="function"><span class="title">$changeNumbers</span> = -></span>
$inner = -<span class="number">1</span>
$outer = <span class="number">10</span>
$inner = $changeNumbers()
</code></pre><pre><code>$outer = <span class="number">1</span>;
$changeNumbers = <span class="function"><span class="keyword">function</span><span class="params">()</span> <span class="title">use</span> <span class="params">(&$outer)</span> {</span>
$inner = -<span class="number">1</span>;
<span class="keyword">return</span> $outer = <span class="number">10</span>;
};
$inner = $changeNumbers();
</code></pre><script>window.example9 = "$outer = 1\n$changeNumbers = ->\n $inner = -1\n $outer = 10\n$inner = $changeNumbers()\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example9);'>load</div><br class='clear' /></div> -->
<!-- <p> -->
<!-- Notice how all of the variable declarations have been pushed up to -->
<!-- the top of the closest scope, the first time they appear. -->
<!-- <b>outer</b> is not redeclared within the inner function, because it's -->
<!-- already in scope; <b>inner</b> within the function, on the other hand, -->
<!-- should not be able to change the value of the external variable of the same name, and -->
<!-- therefore has a declaration of its own. -->
<!-- </p> -->
<!-- <p> -->
<!-- This behavior is effectively identical to Ruby's scope for local variables. -->
<!-- Because you don't have direct access to the <tt>var</tt> keyword, -->
<!-- it's impossible to shadow an outer variable on purpose, you may only refer -->
<!-- to it. So be careful that you're not reusing the name of an external -->
<!-- variable accidentally, if you're writing a deeply nested function. -->
<!-- </p> -->
<!-- <p> -->
<!-- Although suppressed within this documentation for clarity, all -->
<!-- CoffeeScript output is wrapped in an anonymous function: -->
<!-- <tt>(function(){ ... })();</tt> This safety wrapper, combined with the -->
<!-- automatic generation of the <tt>var</tt> keyword, make it exceedingly difficult -->
<!-- to pollute the global namespace by accident. -->
<!-- </p> -->
<!-- <p> -->
<!-- If you'd like to create top-level variables for other scripts to use, -->
<!-- attach them as properties on <b>window</b>, or on the <b>exports</b> -->
<!-- object in CommonJS. The <b>existential operator</b> (covered below), gives you a -->
<!-- reliable way to figure out where to add them; if you're targeting both -->
<!-- CommonJS and the browser: <tt>exports ? this</tt> -->
<!-- </p> -->
<p>
<span id="conditionals" class="bookmark"></span>
<b class="header">If, Else, Unless, and Conditional Assignment</b>
<b>If/else</b> statements can be written without the use of parentheses and
curly brackets. As with functions and other block expressions,
multi-line conditionals are delimited by indentation. There's also a handy
postfix form, with the <tt>if</tt> or <tt>unless</tt> at the end.
</p>
<p>
CopheeScript can compile <b>if</b> statements into PHP expressions,
using the ternary operator when possible, and closure wrapping otherwise. There
is no explicit ternary statement in CopheeScript — you simply use
a regular <b>if</b> statement on a single line.
</p>
<div class='code'><pre><code>$mood = $greatlyImproved <span class="keyword">if</span> $singing
<span class="comment"># Notice the difference when compiled</span>
$mood =
$greatlyImproved <span class="keyword">if</span> $singing
<span class="keyword">if</span> $happy <span class="keyword">and</span> $knowsIt
$clapsHands()
$chaChaCha()
<span class="keyword">else</span>
$showIt()
$date = <span class="keyword">if</span> $friday <span class="keyword">then</span> $sue <span class="keyword">else</span> $jill
</code></pre><pre><code><span class="keyword">if</span> ($singing) {
$mood = $greatlyImproved;
}
$mood = $singing ? $greatlyImproved : <span class="literal">null</span>;
<span class="keyword">if</span> ($happy && $knowsIt) {
$clapsHands();
$chaChaCha();
} <span class="keyword">else</span> {
$showIt();
}
$date = $friday ? $sue : ($jill);
</code></pre><script>window.example10 = "$mood = $greatlyImproved if $singing\n# Notice the difference when compiled\n$mood =\n $greatlyImproved if $singing\n\nif $happy and $knowsIt\n $clapsHands()\n $chaChaCha()\nelse\n $showIt()\n\n$date = if $friday then $sue else $jill\n\n\n\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example10);'>load</div><br class='clear' /></div>
<p>
<span id="splats" class="bookmark"></span>
<b class="header">Splats...</b>
PHP's <b>func_get_args()</b> is a useful way to work with
functions that accept variable numbers of arguments. CopheeScript provides
splats <tt>...</tt>, both for function definition as well as invocation,
making variable numbers of arguments a little bit more palatable.
</p>
<div class='code'><pre><code>$gold = $silver = $rest = <span class="string">"unknown"</span>
<span class="function"><span class="title">$awardMedals</span> = <span class="params">( $first, $second, $others... )</span> -></span>
$gold = $first
$silver = $second
$rest = $others
$contenders = [
<span class="string">"Michael Phelps"</span>
<span class="string">"Liu Xiang"</span>
<span class="string">"Yao Ming"</span>
<span class="string">"Allyson Felix"</span>
<span class="string">"Shawn Johnson"</span>
<span class="string">"Roman Sebrle"</span>
<span class="string">"Guo Jingjing"</span>
<span class="string">"Tyson Gay"</span>
<span class="string">"Asafa Powell"</span>
<span class="string">"Usain Bolt"</span>
]
$awardMedals $contenders...
echo <span class="string">"Gold: "</span> + $gold
echo <span class="string">"Silver: "</span> + $silver
echo <span class="string">"The Field: "</span> + $rest
</code></pre><pre><code>$gold = $silver = $rest = <span class="string">"unknown"</span>;
$awardMedals = <span class="function"><span class="keyword">function</span><span class="params">()</span> <span class="title">use</span> <span class="params">(&$gold,&$silver,&$rest)</span> {</span>
$__args = func_get_args();
$first = $__args[<span class="number">0</span>]; $second = $__args[<span class="number">1</span>]; $others = array_slice($__args, <span class="number">2</span>);
$gold = $first;
$silver = $second;
<span class="keyword">return</span> $rest = $others;
};
$contenders = [<span class="string">"Michael Phelps"</span>, <span class="string">"Liu Xiang"</span>, <span class="string">"Yao Ming"</span>, <span class="string">"Allyson Felix"</span>, <span class="string">"Shawn Johnson"</span>, <span class="string">"Roman Sebrle"</span>, <span class="string">"Guo Jingjing"</span>, <span class="string">"Tyson Gay"</span>, <span class="string">"Asafa Powell"</span>, <span class="string">"Usain Bolt"</span>];
call_user_func_array($awardMedals, $contenders);
echo(<span class="string">"Gold: "</span> + $gold);
echo(<span class="string">"Silver: "</span> + $silver);
echo(<span class="string">"The Field: "</span> + $rest);
</code></pre><script>window.example11 = "$gold = $silver = $rest = \"unknown\"\n\n$awardMedals = ( $first, $second, $others... ) ->\n $gold = $first\n $silver = $second\n $rest = $others\n\n$contenders = [\n \"Michael Phelps\"\n \"Liu Xiang\"\n \"Yao Ming\"\n \"Allyson Felix\"\n \"Shawn Johnson\"\n \"Roman Sebrle\"\n \"Guo Jingjing\"\n \"Tyson Gay\"\n \"Asafa Powell\"\n \"Usain Bolt\"\n]\n\n$awardMedals $contenders...\n\necho \"Gold: \" + $gold\necho \"Silver: \" + $silver\necho \"The Field: \" + $rest\n\n\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example11);'>load</div><br class='clear' /></div>
<p>
<span id="loops" class="bookmark"></span>
<b class="header">Loops and Comprehensions</b>
Most of the loops you'll write in CopheeScript will be <b>comprehensions</b>
over arrays, objects, and ranges. Comprehensions replace (and compile into)
<b>foreach</b> loops, with optional guard clauses and the value of the current array index.
Unlike foreach loops, array comprehensions are expressions, and can be returned
and assigned.
</p>
<div class='code'><pre><code><span class="comment"># Eat lunch.</span>
$eat $food <span class="keyword">for</span> $food <span class="keyword">of</span> [<span class="string">'toast'</span>, <span class="string">'cheese'</span>, <span class="string">'wine'</span>]
<span class="comment"># Fine five course dining.</span>
$courses = [<span class="string">'greens'</span>, <span class="string">'caviar'</span>, <span class="string">'truffles'</span>, <span class="string">'roast'</span>, <span class="string">'cake'</span>]
$menu $i + <span class="number">1</span>, $dish <span class="keyword">for</span> $i, $dish <span class="keyword">of</span> $courses
<span class="comment"># Health conscious meal.</span>
$foods = [<span class="string">'broccoli'</span>, <span class="string">'spinach'</span>, <span class="string">'chocolate'</span>]
$eat $food <span class="keyword">for</span> $food <span class="keyword">of</span> $foods <span class="keyword">when</span> $food <span class="keyword">isnt</span> <span class="string">'chocolate'</span>
</code></pre><pre><code>foreach ([<span class="string">'toast'</span>, <span class="string">'cheese'</span>, <span class="string">'wine'</span>] as $food) {
$eat($food);
}
$courses = [<span class="string">'greens'</span>, <span class="string">'caviar'</span>, <span class="string">'truffles'</span>, <span class="string">'roast'</span>, <span class="string">'cake'</span>];
foreach ($courses as $i => $dish) {
$menu($i + <span class="number">1</span>, $dish);
}
$foods = [<span class="string">'broccoli'</span>, <span class="string">'spinach'</span>, <span class="string">'chocolate'</span>];
foreach ($foods as $food) {
<span class="keyword">if</span> ($food !== <span class="string">'chocolate'</span>) {
$eat($food);
}
}
</code></pre><script>window.example12 = "# Eat lunch.\n$eat $food for $food of ['toast', 'cheese', 'wine']\n\n# Fine five course dining.\n$courses = ['greens', 'caviar', 'truffles', 'roast', 'cake']\n$menu $i + 1, $dish for $i, $dish of $courses\n\n# Health conscious meal.\n$foods = ['broccoli', 'spinach', 'chocolate']\n$eat $food for $food of $foods when $food isnt 'chocolate'\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example12);'>load</div><br class='clear' /></div>
<p>
Comprehensions should be able to handle most places where you otherwise
would use a loop, <b>each</b>/<b>forEach</b>, <b>map</b>, or <b>select</b>/<b>filter</b>, for example:
<tt>$shortNames = ($name for $name in $list when strlen( $name ) < 5)</tt><br />
If you know the start and end of your loop, or would like to step through
in fixed-size increments, you can use a range to specify the start and
end of your comprehension.
</p>
<div class='code'><pre><code>$countdown = ( $num <span class="keyword">for</span> $num <span class="keyword">in</span> [<span class="number">10.</span><span class="number">.1</span>])
</code></pre><pre><code>$countdown = call_user_func((<span class="function"><span class="keyword">function</span><span class="params">()</span> {</span>
$results = [];
<span class="keyword">for</span> ($num = <span class="number">10</span>; $num >= <span class="number">1</span>; $num--) {
$results[] = $num;
}
<span class="keyword">return</span> $results;
}));
</code></pre><script>window.example13 = "$countdown = ( $num for $num in [10..1])\n\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example13);'>load</div><br class='clear' /></div>
<p>
Note how because we are assigning the value of the comprehensions to a
variable in the example above, CopheeScript is collecting the result of
each iteration into an array. Sometimes functions end with loops that are
intended to run only for their side-effects. Be careful that you're not
accidentally returning the results of the comprehension in these cases,
by adding a meaningful return value — like <tt>true</tt> — or <tt>null</tt>,
to the bottom of your function.
</p>
<p>
To step through a range comprehension in fixed-size chunks,
use <tt>by</tt>, for example:<br />
<tt>$evens = ($x for $x in [0..10] by 2)</tt>
</p>
<p>
Comprehensions can also be used to iterate over the keys and values in
a hash.
</p>
<div class='code'><pre><code>$yearsOld = <span class="attribute">max</span>: <span class="number">10</span>, <span class="attribute">ida</span>: <span class="number">9</span>, <span class="attribute">tim</span>: <span class="number">11</span>
$ages = <span class="keyword">for</span> $child, $age <span class="keyword">of</span> $yearsOld
<span class="string">"<span class="subst">#{ $child }</span> is <span class="subst">#{ $age }</span>"</span>
</code></pre><pre><code>$yearsOld = [
<span class="string">'max'</span> => <span class="number">10</span>,
<span class="string">'ida'</span> => <span class="number">9</span>,
<span class="string">'tim'</span> => <span class="number">11</span>
];
$ages = call_user_func((<span class="function"><span class="keyword">function</span><span class="params">()</span> <span class="title">use</span> <span class="params">(&$yearsOld)</span> {</span>
$results = [];
foreach ($yearsOld as $child => $age) {
$results[] = $child . <span class="string">" is "</span> . $age;
}
<span class="keyword">return</span> $results;
}));
</code></pre><script>window.example14 = "$yearsOld = max: 10, ida: 9, tim: 11\n\n$ages = for $child, $age of $yearsOld\n \"#{ $child } is #{ $age }\"\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example14);'>load</div><br class='clear' /></div>
<p>
The only low-level loop that CopheeScript provides is the <b>while</b> loop. The
main difference from PHP is that the <b>while</b> loop can be used
as an expression, returning an array containing the result of each iteration
through the loop.
</p>
<div class='code'><pre><code><span class="comment"># Econ 101</span>
<span class="keyword">if</span> $<span class="keyword">this</span>.studyingEconomics
$buy() <span class="keyword">while</span> $supply > $demand
$sell() <span class="keyword">until</span> $supply > $demand
<span class="comment"># Nursery Rhyme</span>
$num = <span class="number">6</span>
$lyrics = <span class="keyword">while</span> $num -= <span class="number">1</span>
<span class="string">"<span class="subst">#{ $num }</span> little monkeys, jumping on the bed.
One fell out and bumped his head."</span>
</code></pre><pre><code><span class="keyword">if</span> ($<span class="keyword">this</span>->studyingEconomics) {
<span class="keyword">while</span> ($supply > $demand) {
$buy();
}
<span class="keyword">while</span> (!($supply > $demand)) {
$sell();
}
}
$num = <span class="number">6</span>;
$lyrics = call_user_func((<span class="function"><span class="keyword">function</span><span class="params">()</span> <span class="title">use</span> <span class="params">(&$num)</span> {</span>
$results = [];
<span class="keyword">while</span> ($num -= <span class="number">1</span>) {
$results[] = $num . <span class="string">" little monkeys, jumping on the bed. One fell out and bumped his head."</span>;
}
<span class="keyword">return</span> $results;
}));
</code></pre><script>window.example15 = "# Econ 101\nif $this.studyingEconomics\n $buy() while $supply > $demand\n $sell() until $supply > $demand\n\n# Nursery Rhyme\n$num = 6\n$lyrics = while $num -= 1\n \"#{ $num } little monkeys, jumping on the bed.\n One fell out and bumped his head.\"\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example15);'>load</div><br class='clear' /></div>
<p>
For readability, the <b>until</b> keyword is equivalent to <tt>while not</tt>,
and the <b>loop</b> keyword is equivalent to <tt>while true</tt>.
</p>
<p>
When using a loop to generate functions, it's common to insert
a closure wrapper in order to ensure that loop variables are closed over,
and all the generated functions don't just share the final values. CopheeScript
provides the <tt>do</tt> keyword, which immediately invokes a passed function,
forwarding any arguments.
</p>
<div class='code'><pre><code><span class="keyword">for</span> $filename <span class="keyword">of</span> $list
<span class="keyword">do</span> <span class="function"><span class="params">( $filename )</span> -></span>
$fs.readFile $filename, <span class="function"><span class="params">( $err, $contents )</span> -></span>
$compile $filename, $contents.toString()
</code></pre><pre><code>$fn = <span class="function"><span class="keyword">function</span><span class="params">($filename)</span> <span class="title">use</span> <span class="params">(&$compile,&$fs)</span> {</span>
<span class="keyword">return</span> $fs->readFile($filename, <span class="function"><span class="keyword">function</span><span class="params">($err, $contents)</span> <span class="title">use</span> <span class="params">(&$filename,&$compile)</span> {</span>
<span class="keyword">return</span> $compile($filename, $contents->toString());
});
};
foreach ($list as $filename) {
$fn($filename);
}
</code></pre><script>window.example16 = "for $filename of $list\n do ( $filename ) ->\n $fs.readFile $filename, ( $err, $contents ) ->\n $compile $filename, $contents.toString()\n"</script><div class='minibutton load' onclick='javascript: loadConsole(example16);'>load</div><br class='clear' /></div>
<p>
<span id="slices" class="bookmark"></span>
<b class="header">Array Slicing and Splicing with Ranges</b>
Ranges can also be used to extract slices of arrays.
With two dots (<tt>3..6</tt>), the range is inclusive (<tt>3, 4, 5, 6</tt>);
with three dots (<tt>3...6</tt>), the range excludes the end (<tt>3, 4, 5</tt>).
Slices indices have useful defaults. An omitted first index defaults to
zero and an omitted second index defaults to the size of the array.
</p>
<div class='code'><pre><code>$numbers = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]
$start = $numbers[<span class="number">0.</span><span class="number">.2</span>]
$middle = $numbers[<span class="number">3.</span>..-<span class="number">2</span>]
$end = $numbers[-<span class="number">2.</span>.]
$copy = $numbers[..]
</code></pre><pre><code>$numbers = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>];
$start = array_slice( $numbers, <span class="number">0</span>, <span class="number">3</span> );
$middle = array_slice( $numbers, <span class="number">3</span>, -<span class="number">2</span> );
$end = array_slice( $numbers, -<span class="number">2</span> );