forked from HubSpot/youmightnotneedjquery
-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.html
842 lines (818 loc) · 30.9 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
<!DOCTYPE html>
<!-- Please edit the index.jade or the files in comparisons/, this file is generated--><!--[if lt IE 10 ]> <html class="old-ie"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<title>Swift is a lot like Scala</title>
<meta name="description" content="Examples of how Apple's Swift language is similar to Scala.">
<link rel="stylesheet" href="css/prism.css">
<link rel="stylesheet" href="css/index.css">
<script type="text/javascript" src="//use.typekit.net/ghy0wve.js"></script>
<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
<script src="js/prism.js"></script>
<script src="js/index.js"></script>
</head>
<body><a href="https://github.com/leverich/swiftislikescala"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/a6677b08c955af8400f44c6298f40e7d19cc5b2d/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677261795f3664366436642e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png"></a>
<div class="page">
<header class="page-header">
<div class="inner">
<h1>Swift is a lot like Scala</h1>
</div>
</header>
<div class="explanation">
<div class="inner">
<p>A new programming language called <a href="https://developer.apple.com/swift/">Swift</a> was announced at <a href="https://developer.apple.com/wwdc/">WWDC'14</a>. Here's a source-to-source comparison of Swift and <a href="http://www.scala-lang.org/">Scala</a> using the <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html">examples</a> given in "The Swift Programming Language" book published by Apple in the iTunes Store.</p>
<p>I suspect the two are related ;-) Syntacticly, it's fair to say that Swift is a dialect of Scala. Swift inherits from Scala most of the "banner" features listed by Apple: Type Inference, Closures, Tuples, Protocols, Extensions, Generics, Read-Eval-Print-Loop, etc.</p>
<p>
Despite their syntactic similarity, Swift's runtime environment is quite distinct from Scala's, and is perhaps the most interesting aspect of the new language.
Scala compiles to the JVM, uses garbage collection, and its object model transparently integrates with Java.
Swift compiles to native code, uses automatic reference counting, and its object model transparently integrates with Objective-C.
Thus, the similarity between the two languages does not extend much below the surface.
</p>
<p><a href="https://github.com/densh/talks/">Den Shabalin</a> provides another excellent <a href="http://www.scribd.com/doc/227879724/Swift-vs-Scala-2-11">point-by-point comparison</a>.</p>
<p>
p.s. Don't take this code too seriously! Most of it is
rote, minimalistic translation of the Swift examples given
by Apple. I give a handful of more idiomatic Scala
translations. Feel free to submit a GitHub issue/pull request
if you find anything unbearably crass.
</p>
</div>
</div>
<header class="search">
<input type="search" placeholder="Search..." required>
</header>
<div class="comparisons">
<div class="empty-message">Your search didn't match any comparisons.</div>
<section id="basics" class="odd">
<div class="inner">
<h2>Basics</h2>
<div id="hello_world" class="comparison">
<h3><a href="#hello_world">Hello World</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>println("Hello, world!")
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>println("Hello, world!")
</code></pre>
</div>
</div>
</div>
<div id="variables_and_constants" class="comparison">
<h3><a href="#variables_and_constants">Variables And Constants</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var myVariable = 42
myVariable = 50
let myConstant = 42
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var myVariable = 42
myVariable = 50
val myConstant = 42
</code></pre>
</div>
</div>
</div>
<div id="explicit_types" class="comparison">
<h3><a href="#explicit_types">Explicit Types</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>let explicitDouble: Double = 70
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>val explicitDouble: Double = 70
</code></pre>
</div>
</div>
</div>
<div id="type_coercion" class="comparison">
<h3><a href="#type_coercion">Type Coercion</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>let label = "The width is "
let width = 94
let widthLabel = label + String(width)
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>val label = "The width is "
val width = 94
val widthLabel = label + width
</code></pre>
</div>
</div>
</div>
<div id="string_interpolation" class="comparison">
<h3><a href="#string_interpolation">String Interpolation</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>let apples = 3
let oranges = 5
let fruitSummary = "I have \(apples + oranges) " +
"pieces of fruit."
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>val apples = 3
val oranges = 5
val fruitSummary = s"I have ${apples + oranges} " +
" pieces of fruit."
</code></pre>
</div>
</div>
</div>
<div id="range_operator" class="comparison">
<h3><a href="#range_operator">Range Operator</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..count {
println("Person \(i + 1) is called \(names[i])")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>val names = Array("Anna", "Alex", "Brian", "Jack")
val count = names.length
for (i <- 0 until count) {
println(s"Person ${i + 1} is called ${names(i)}")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack
</code></pre>
</div>
</div>
</div>
<div id="inclusive_range_operator" class="comparison">
<h3><a href="#inclusive_range_operator">Inclusive Range Operator</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>for index in 1...5 {
println("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>for (index <- 1 to 5) {
println(s"$index times 5 is ${index * 5}")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
</code></pre>
</div>
</div>
</div>
</div>
</section>
<section id="collections">
<div class="inner">
<h2>Collections</h2>
<div id="arrays" class="comparison">
<h3><a href="#arrays">Arrays</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var shoppingList = ["catfish", "water",
"tulips", "blue paint"]
shoppingList[1] = "bottle of water"
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var shoppingList = Array("catfish",
"water", "tulips", "blue paint")
shoppingList(1) = "bottle of water"
</code></pre>
</div>
</div>
</div>
<div id="maps" class="comparison">
<h3><a href="#maps">Maps</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var occupations = [
"Malcolm": "Captain",
"Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var occupations = scala.collection.mutable.Map(
"Malcolm" -> "Captain",
"Kaylee" -> "Mechanic"
)
occupations("Jayne") = "Public Relations"
</code></pre>
</div>
</div>
</div>
<div id="empty_collections" class="comparison">
<h3><a href="#empty_collections">Empty Collections</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>let emptyArray = String[]()
let emptyDictionary = Dictionary<String, Float>()
let emptyArrayNoType = []
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>val emptyArray = Array[String]()
val emptyDictionary = Map[String, Float]()
val emptyArrayNoType = Array()
</code></pre>
</div>
</div>
</div>
</div>
</section>
<section id="functions" class="odd">
<div class="inner">
<h2>Functions</h2>
<div id="functions" class="comparison">
<h3><a href="#functions">Functions</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>func greet(name: String, day: String) -> String {
return "Hello \(name), today is \(day)."
}
greet("Bob", "Tuesday")
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>def greet(name: String, day: String): String = {
return s"Hello $name, today is $day."
}
greet("Bob", "Tuesday")
</code></pre>
</div>
</div>
<div data-browser="idiomatic" class="browser idiomatic">
<h4>IDIOMATIC SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>def greet(name: String, day: String): String =
s"Hello $name, today is $day."
greet("Bob", "Tuesday")
</code></pre>
</div>
</div>
</div>
<div id="tuple_return" class="comparison">
<h3><a href="#tuple_return">Tuple Return</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>func getGasPrices() -> (Double, Double, Double) {
return (3.59, 3.69, 3.79)
}
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>def getGasPrices(): (Double, Double, Double) = {
return (3.59, 3.69, 3.79)
}
</code></pre>
</div>
</div>
<div data-browser="idiomatic" class="browser idiomatic">
<h4>IDIOMATIC SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>def getGasPrices = (3.59, 3.69, 3.79)
</code></pre>
</div>
</div>
</div>
<div id="variable_number_of_arguments" class="comparison">
<h3><a href="#variable_number_of_arguments">Variable Number Of Arguments</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
sumOf(42, 597, 12)
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>def sumOf(numbers: Int*): Int = {
var sum = 0
for (number <- numbers) {
sum += number
}
return sum
}
sumOf(42, 597, 12)
</code></pre>
</div>
</div>
<div data-browser="idiomatic" class="browser idiomatic">
<h4>IDIOMATIC SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>Array(42, 597, 12).sum
</code></pre>
</div>
</div>
</div>
<div id="function_type" class="comparison">
<h3><a href="#function_type">Function Type</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>func makeIncrementer() -> (Int -> Int) {
func addOne(number: Int) -> Int {
return 1 + number
}
return addOne
}
var increment = makeIncrementer()
increment(7)
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>def makeIncrementer(): Int => Int = {
def addOne(number: Int): Int = {
return 1 + number
}
return addOne
}
var increment = makeIncrementer()
increment(7)
</code></pre>
</div>
</div>
<div data-browser="idiomatic" class="browser idiomatic">
<h4>IDIOMATIC SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>def makeIncrementer: Int => Int =
(number: Int) => 1 + number
var increment = makeIncrementer
increment(7)
</code></pre>
</div>
</div>
</div>
<div id="map" class="comparison">
<h3><a href="#map">Map</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var numbers = [20, 19, 7, 12]
numbers.map({ number in 3 * number })
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var numbers = Array(20, 19, 7, 12)
numbers.map( number => 3 * number )
</code></pre>
</div>
</div>
</div>
<div id="sort" class="comparison">
<h3><a href="#sort">Sort</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>sort([1, 5, 3, 12, 2]) { $0 > $1 }
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>Array(1, 5, 3, 12, 2).sortWith(_ > _)
</code></pre>
</div>
</div>
</div>
<div id="named_arguments" class="comparison">
<h3><a href="#named_arguments">Named Arguments</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>func area(#width: Int, #height: Int) -> Int {
return width * height
}
area(width: 10, height: 10)
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>def area(width: Int, height: Int): Int = {
return width * height
}
area(width = 10, height = 10)
</code></pre>
</div>
</div>
</div>
</div>
</section>
<section id="classes">
<div class="inner">
<h2>Classes</h2>
<div id="declaration" class="comparison">
<h3><a href="#declaration">Declaration</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>class Shape {
var numberOfSides = 0
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>class Shape {
var numberOfSides = 0
def simpleDescription(): String = {
return s"A shape with $numberOfSides sides."
}
}
</code></pre>
</div>
</div>
<div data-browser="idiomatic" class="browser idiomatic">
<h4>IDIOMATIC SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>class Shape (var numberOfSides: Int = 0) {
def simpleDescription =
s"A shape with $numberOfSides sides."
}
</code></pre>
</div>
</div>
</div>
<div id="usage" class="comparison">
<h3><a href="#usage">Usage</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var shape = new Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
</code></pre>
</div>
</div>
</div>
<div id="subclass" class="comparison">
<h3><a href="#subclass">Subclass</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>class NamedShape {
var numberOfSides: Int = 0
var name: String
init(name: String) {
self.name = name
}
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
class Square: NamedShape {
var sideLength: Double
init(sideLength: Double, name: String) {
self.sideLength = sideLength
super.init(name: name)
numberOfSides = 4
}
func area() -> Double {
return sideLength * sideLength
}
override func simpleDescription() -> String {
return "A square with sides of length
\(sideLength)."
}
}
let test = Square(sideLength: 5.2, name: "my test square")
test.area()
test.simpleDescription()
</code></pre>
</div>
</div>
<div data-browser="idiomatic" class="browser idiomatic">
<h4>IDIOMATIC SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>class NamedShape(var name: String,
var numberOfSides: Int = 0) {
def simpleDescription =
s"A shape with $numberOfSides sides."
}
class Square(var sideLength: Double, name: String)
extends NamedShape(name, numberOfSides = 4) {
def area = sideLength * sideLength
override def simpleDescription =
s"A square with sides of length $sideLength."
}
val test = new Square(5.2, "my test square")
test.area
test.simpleDescription
</code></pre>
</div>
</div>
</div>
<div id="checking_type" class="comparison">
<h3><a href="#checking_type">Checking Type</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var movieCount = 0
var songCount = 0
for item in library {
if item is Movie {
++movieCount
} else if item is Song {
++songCount
}
}
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var movieCount = 0
var songCount = 0
for (item <- library) {
if (item.isInstanceOf[Movie]) {
movieCount += 1
} else if (item.isInstanceOf[Song]) {
songCount += 1
}
}
</code></pre>
</div>
</div>
</div>
<div id="pattern_match" class="comparison">
<h3><a href="#pattern_match">Pattern Match</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var movieCount = 0
var songCount = 0
for item in library {
switch item {
case let movie as Movie:
++movieCount
println("Movie: '\(movie.name)', dir. \(movie.director)")
case let song as Song:
++songCount
println("Song: '\(song.title)'")
}
}
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>var movieCount = 0
var songCount = 0
for (item <- library) {
item match {
case movie: Movie =>
movieCount += 1
println(s"Movie: '${movie.name}', dir. ${movie.director}")
case song: Song =>
songCount += 1
println(s"Song: '${song.title}'")
}
}
</code></pre>
</div>
</div>
</div>
<div id="downcasting" class="comparison">
<h3><a href="#downcasting">Downcasting</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>for object in someObjects {
let movie = object as Movie
println("Movie: '\(movie.name)', dir. \(movie.director)")
}
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>for (obj <- someObjects) {
val movie = obj.asInstanceOf[Movie]
println(s"Movie: '${movie.name}', dir. ${movie.director}")
}
</code></pre>
</div>
</div>
</div>
<div id="protocol" class="comparison">
<h3><a href="#protocol">Protocol</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>protocol Nameable {
func name() -> String
}
func f<T: Nameable>(x: T) {
println("Name is " + x.name())
}
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>trait Nameable {
def name(): String
}
def f[T <: Nameable](x: T) = {
println("Name is " + x.name())
}
</code></pre>
</div>
</div>
</div>
<div id="extensions" class="comparison">
<h3><a href="#extensions">Extensions</a></h3>
<div data-browser="swift" class="browser swift">
<h4>SWIFT</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>extension Double {
var km: Double { return self * 1_000.0 }
var m: Double { return self }
var cm: Double { return self / 100.0 }
var mm: Double { return self / 1_000.0 }
var ft: Double { return self / 3.28084 }
}
let oneInch = 25.4.mm
println("One inch is \(oneInch) meters")
// prints "One inch is 0.0254 meters"
let threeFeet = 3.ft
println("Three feet is \(threeFeet) meters")
// prints "Three feet is 0.914399970739201 meters"
</code></pre>
</div>
</div>
<div data-browser="scala" class="browser scala">
<h4>SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>object Extensions {
implicit class DoubleUnit(d: Double) {
def km: Double = { return d * 1000.0 }
def m: Double = { return d }
def cm: Double = { return d / 100.0 }
def mm: Double = { return d / 1000.0 }
def ft: Double = { return d / 3.28084 }
}
}
import Extensions.DoubleUnit
val oneInch = 25.4.mm
println(s"One inch is $oneInch meters")
// prints "One inch is 0.0254 meters"
val threeFeet = 3.ft
println(s"Three feet is $threeFeet meters")
// prints "Three feet is 0.914399970739201 meters"
</code></pre>
</div>
</div>
<div data-browser="idiomatic" class="browser idiomatic">
<h4>IDIOMATIC SCALA</h4>
<div data-lang="scala" class="code-block language-scala">
<pre><code>object Extensions {
implicit class DoubleUnit(d: Double) {
def km = d * 1000.0
def m = d
def cm = d / 100.0
def mm = d / 1000.0
def ft = d / 3.28084
}
}
import Extensions.DoubleUnit
val oneInch = 25.4.mm
println(s"One inch is $oneInch meters")
// prints "One inch is 0.0254 meters"
val threeFeet = 3.ft
println(s"Three feet is $threeFeet meters")
// prints "Three feet is 0.914399970739201 meters"
</code></pre>
</div>
</div>
</div>
</div>
</section>
</div>
<footer><p>Shamelessly forked from <a href="http://youmightnotneedjquery.com/">http://youmightnotneedjquery.com/</a>.</footer>
<div class="changelog">
<p>2014 July 16 - Added additional comparisons to 'idiomatic' Scala usage.</p>
<p>2014 July 16 - Pattern match example from Fredrik Ekholdt.</p>
<p>2014 June 4 - Cleanup of Subclass from Gerald Loeffler.</p>
<p>2014 June 4 - Added typecasting, protocols, and extensions.</p>
</div>
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-20537147-2', 'leverich.github.io');
ga('send', 'pageview');
</script>
<div style="-webkit-transform: translateZ(0);"></div>
</body>