-
Notifications
You must be signed in to change notification settings - Fork 16
/
sequences.clj
1272 lines (1038 loc) · 29 KB
/
sequences.clj
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
;; gorilla-repl.fileformat = 1
;; **
;;; # Clojure Sequences
;;;
;;;
;; **
;; **
;;; ## Sequences overview
;;; ### Not really a data structure
;;; But it **looks** like a list when you print it.
;; **
;; @@
(range 5)
;; @@
;; **
;;; A list **is** a sequence, but a sequence is not necessarily a list.
;; **
;; @@
(seq? (list :a :b :c))
;; @@
;; **
;;; ### A sequence is a source of values
;;; * In some order
;;; * Vector or list: same as collection order
;;; * Map or set: arbitrary, but consistent
;;; * Sorted map or set: in sort order
;;; * Sort-of like Java [Iterator](http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html) or Ruby [Enumerator](http://www.ruby-doc.org/core-2.1.1/Enumerator.html)
;;; * May be **lazy**
;; **
;; **
;;; ### Partial computation
;;; * Most sequence functions return another sequence
;;; * Each sequence represents one step in the "job"
;;; * Compose a "stack" of lazy sequence operations
;;; * Consume the result as needed
;; **
;; **
;;; ## The Sequence library: Generating
;;;
;;; ### Evaluating infinite sequences
;;;
;;; _WARNING:_ Some of the examples below will result in infinite sequences. To prevent the worksheet from stalling, evaluate the following expression:
;; **
;; @@
(set! *print-length* 25)
;; @@
;; **
;;; This will set the printer to only print the first 20 items of each sequence. If the collection contains more items, the printer will print the first 25 items, followed by "..." to indicate the remaining items.
;;;
;;; Note that if you do not evaluate the above expression, but you evaluate an infinite sequence, the affected segment will turn green, and you will need to restart the Gorilla REPL server before reloading this page.
;; **
;; **
;;; ### Generating a sequence of numbers
;; **
;; @@
(range 10)
;; @@
;; @@
(range 40 45)
;; @@
;; @@
(range)
;; @@
;; **
;;; ### Sequence from data structure
;; **
;; @@
(seq [1 2 3])
;; @@
;; **
;;; * `seq` gets a sequence from any data structure
;;; * Also strings, Java arrays, and Iterable
;;; * Every sequence function calls `seq for you
;;; * You rarely need to call `seq` except to check for empty collection
;; **
;; **
;;; ### `seq` to check for empty
;;; * `seq` on empty collection returns `nil`
;;; * `nil` is logical false
;; **
;; @@
(if (seq "")
:not-empty
:empty)
;; @@
;; **
;;; ### Sequence from string
;; **
;; @@
(seq "Hello, World!")
;; @@
;; @@
(remove #{\a \e \i \o \u} "Hello, World!")
;; @@
;; **
;;; ### More sequences
;;; <table>
;;; <tr>
;;; <td>file-seq</td>
;;; <td>Files in directory</td>
;;; </tr>
;;; <tr>
;;; <td>line-seq</td>
;;; <td>Lines from java.io.BufferedReader</td>
;;; </tr>
;;; <tr>
;;; <td>re-seq</td>
;;; <td>Regex matches</td>
;;; </tr>
;;; <tr>
;;; <td>tree-seq</td>
;;; <td>Generic tree walker</td>
;;; </tr>
;;; <tr>
;;; <td>resultset-seq</td>
;;; <td>SQL query results</td>
;;; </tr>
;;; <tr>
;;; <td>xml-seq</td>
;;; <td>XML document nodes</td>
;;; </tr>
;;; <tr>
;;; <td>enumeration-seq</td>
;;; <td>Java Enumeration</td>
;;; </tr>
;;; <tr>
;;; <td>iterator-seq</td>
;;; <td>Java Iterator</td>
;;; </tr>
;;; </table>
;;;
;;; ## The Sequence library: Operations
;;; ### Iterating over a sequence
;; **
;; @@
(map (fn [n] (long (Math/pow 2 n)))
(range 9))
;; @@
;; **
;;; ### Iterating over many sequences
;; **
;; @@
(map +
[100 200 300 400 500]
(range 1 999)
[10 20 30])
;; @@
;; **
;;; _Note:_ Stops at end of **shortest** sequence.
;;;
;;; ### Iterating with `for`
;; **
;; @@
(for [n (range 9)]
(long (Math/pow 2 n)))
;; @@
;; **
;;; _Note:_ Not an imperative for-loop; lazy like `map`.
;;;
;;; ### Nested iteration with `for`
;; **
;; @@
(for [k [:a :b]
n (range 1 4)]
[k n])
;; @@
;; **
;;; _Note:_ Different from `map` with multiple sequences
;;;
;;; ### Shortening sequences
;; **
;; @@
(take 3 (range))
;; @@
;; @@
(drop 3 (range))
;; @@
;; @@
(take-while #(< % 5) (range))
;; @@
;; @@
(drop-while #(< % 5) (range))
;; @@
;; **
;;; ### Taking out items
;; **
;; @@
(filter even? (range 10))
;; @@
;; @@
(remove even? (range 10))
;; @@
;; **
;;; ### Grouping elements of a sequence
;; **
;; @@
(partition 3 (range 10))
;; @@
;; @@
(partition-all (range 10))
;; @@
;; @@
(group-by (fn [n] (if (even? n) :even :odd))
(range 10))
;; @@
;; **
;;; ### Reordering a sequence
;; **
;; @@
(sort '(10 37 3 10 44 6))
;; @@
;; **
;;; _Note:_ `sort` is not lazy.
;; **
;; **
;;; #### `sort` with comparator
;; **
;; @@
(sort (fn [x y] (- (compare x y))) '(10 37 3 10 44 6))
;; @@
;; **
;;; #### `sort-by`
;; **
;; @@
;; Note: Not a comparator
(sort-by :name [{:name "Bob" :age 33}
{:name "Ali" :age 39}
{:name "Mel" :age 22}
{:name "Jim" :age 58}])
;; @@
;; @@
;; Note: Not a comparator
(sort-by :age [{:name "Bob" :age 33}
{:name "Ali" :age 39}
{:name "Mel" :age 22}
{:name "Jim" :age 58}])
;; @@
;; **
;;; #### `shuffle`
;; **
;; @@
(shuffle (range 10))
;; @@
;; **
;;; _Note:_ `shuffle` returns a vector, is not lazy.
;;;
;;; #### `reverse`
;; **
;; @@
(reverse (range 10))
;; @@
;; **
;;; _Note:_ `reverse` is not lazy.
;; **
;; **
;;; ### Flattening sequences
;; **
;; @@
(mapcat (fn [i] (range i)) (range 7))
;; @@
;; @@
(flatten '(1 1 (2 3) (5 8 (13 21))))
;; @@
;; **
;;; _Note:_ `flatten` is usually a sign of poorly-constructed sequences. Prefer `mapcat` where possible.
;;;
;;; ### Combining sequences
;; **
;; @@
(concat [:a :b :c] (range 5))
;; @@
;; **
;;; _Note:_ Beware lazy "stacks."
;; **
;; **
;;; ### Utility sequences
;;; Typically used in combination with `map` or `reduce`.
;; **
;; @@
(repeat :b)
;; @@
;; @@
(repeatedly #(rand-int 100))
;; @@
;; @@
(cycle [:a :b :c])
;; @@
;; @@
(interpose \, "abc")
;; @@
;; @@
(apply str *1)
;; @@
;; @@
(interleave [1 2 3] [:a :b :c])
;; @@
;; @@
(iterate #(* 2 %) 2)
;; @@
;; **
;;; ## The Sequence library: Results
;;; ### Collecting result of a sequence
;; **
;; @@
(vec (filter even? (range 10)))
;; @@
;; @@
(set (map inc (range 10)))
;; @@
;; @@
(apply hash-map (range 10))
;; @@
;; @@
(apply str (interpose \, (range 4)))
;; @@
;; **
;;; ### Seqs into collections
;;; * `(into _coll_ _seq_)`
;;; * Adds elements of _seq_ to _coll_ using `conj`
;;; * "Pours" _seq_ into _coll_
;; **
;; @@
(into #{} "hello")
;; @@
;; @@
(into {} [[:x 1] [:y 2]])
;; @@
;; @@
(into () [:a :b :c])
;; @@
;; **
;;; ### Consuming a sequence for side effects
;; **
;; @@
(doseq [i (range 5)]
(prn i))
;; @@
;; **
;;; _Note:_ `doseq` always returns `nil`
;;;
;;; ### `doseq` over many sequences
;;; Creates nested iteration like `for`.
;; **
;; **
;;;
;; **
;; @@
(doseq [c [:a :b]
i (range 3)]
(prn c i))
;; @@
;; **
;;; ### `reduce`
;;; * `(reduce _function_ _init_ _seq_)`
;;; * _function_ takes two arguments
;;; * `reduce` calls `(_function_ _init_ (first _seq_))`
;;; * Return value becomes _init_ for the last step
;;; * Repeat util the end of the seq, return last init
;; **
;; @@
(reduce (fn [total item] (+ total (* 10 item)))
0 ; init
[1 2 3 4])
;; @@
;; **
;;; With no _init_, uses first element of seq
;; **
;; @@
(reduce + [1 1 2 3 5])
;; @@
;; **
;;; ### `some`
;;; * `(some _function_ _seq_)`
;;; * Maps _function_ over the seq
;;; * Returns _first_ logical true value of function
;;; * Or `nil` if nothing true
;; **
;; @@
(some #(zero? (rem % 5)) [9 22 35 76])
;; @@
;; @@
(some #(= 4 %) [1 3 5])
;; @@
;; **
;;; ### `some` with a set
;;; * Sets are functions
;;; * Can be used as linear search
;; **
;; @@
(some #{:b} [:a :b :c])
;; @@
;; @@
(some #{:foo} [:a :b :c])
;; @@
;; **
;;; ## Laziness
;;; * Sequences can by **lazy**
;;; * Compute results as needed
;;; * Only compute value once, then cached
;;; * Can be infinite!
;;; * Most Clojure functions which return sequences are lazy
;;;
;;; ### Don't mix side effects and laziness
;;; **Chunked sequences** lead to confusing results.
;; **
;; @@
(take 4 (map println (range 100)))
;; @@
;; **
;;; ### Use side effects at the end
;;; * Usually `doseq`
;;; * Sometimes `reduce`
;;; * Rarely `doall` or `dorun`
;;; * "Force" complete evaluation of lazy seq
;;; * Sequence must be finite!
;;; * `doall` returns the entire sequence (must fit in memory!)
;;; * `dorun` returns `nil`
;;;
;;; ### Beware lazy stacks
;; **
;; @@
(defn build-sequence [n]
(reduce (fn [sequence i]
(concat sequence (range i)))
nil
(range 1 (inc n))))
;; @@
;; @@
(build-sequence 6)
;; @@
;; @@
(first (build-sequence 4000))
;; @@
;; **
;;; #### Non-lazy alternative
;; **
;; @@
(defn build-vector [n]
(reduce (fn [v i]
(into v (range i)))
[]
(range 1 (inc n))))
;; @@
;; @@
(take 10 (build-vector 4000))
;; @@
;; **
;;; ### Non-lazy alternatives
;;; Many lazy sequence operations have non-lazy equivalents that return vectors.
;;;
;;; ```clojure
;;; ;; Return Lazy Seq ;; Return Vector
;;; map mapv
;;; filter filterv
;;; concat into []
;;;
;;; ;; O(n) on a seq ;; O(1) on a vector
;;; last peek
;;; butlast pop
;;; ```
;;;
;;; ## Sequence theory
;;; ### Sequence API
;;; * `(seq coll)`
;;; * If collection is not empty, return seq object on it
;;; * If collection is empty, return nil
;;; * `(first coll)` returns the first element
;;; * `(rest coll)` returns a sequence of the rest of the elements
;;; * Might by empty, but not necessarily `nil`
;;; * `(next coll)` is the same as `(seq (rest coll))`
;;; * `(cons x coll)` returns a new sequence: first is x, rest is coll
;;;
;;; ### Sequences over structures
;;; * Can treat any Clojure data structure as a seq
;;; * Lists actually _are_ seqs
;;; * Associative structures treated as sequence of pairs
;;;
;;; ### List as a Seq
;; **
;; @@
(def x '(1 2 3)) ; x is a list
;; @@
;; **
;;; ![Collections list as a seq](project-files/images/collections-seq-list-initial.svg)
;; **
;; @@
(def a (first x)) ; a is 1
;; @@
;; **
;;; ![Collections seq list first](project-files/images/collections-seq-list-first.svg)
;; **
;; @@
(def s (rest x)) ; s is a seq
;; @@
;; **
;;; ![Collections list as a seq](project-files/images/collections-seq-list-rest.svg)
;; **
;; @@
(def b (first (rest x))) ; b is 2
(def b (second x)) ; same thing
;; @@
;; **
;;; ![Collections list as a seq](project-files/images/collections-seq-list-second.svg)
;; **
;; **
;;; ### Seq over vector
;; **
;; @@
(def v [1 2 3]) ; v is a vector
;; @@
;; **
;;; ![Collections list as a seq](project-files/images/collections-seq-vector-initial.svg)
;; **
;; @@
(def s1 (seq v)) ; s1 is a seq
;; @@
;; **
;;; ![Collections seq vector seq](project-files/images/collections-seq-vector-seq.svg)
;; **
;; @@
(def a (first v)) ; a is 1
;; @@
;; **
;;; ![Collections seq vector first](project-files/images/collections-seq-vector-first.svg)
;; **
;; @@
(def s2 (rest v)) ; s2 is a seq
;; @@
;; **
;;; ![Collections seq vector rest](project-files/images/collections-seq-vector-rest.svg)
;; **
;; @@
(def b (first (rest v))) ; b is 2
(def b (second v)) ; same thing
;; @@
;; **
;;; ![Collections seq vector second](project-files/images/collections-seq-vector-second.svg)
;; **
;; **
;;; ### Sequences over functions
;;; * Can map a generator function to a seq
;;; * Seq is lazy, can be infinite
;;; * Can process more than fits in memory
;; **
;; @@
(def r (range 1 100)) ; r is a lazy seq
;; @@
;; **
;;; ![Collections seq lazy initial](project-files/images/collections-seq-lazy-initial.svg)
;; **
;; @@
(def a (first r)) ; a is 1
;; @@
;; **
;;; ![Collections seq lazy first](project-files/images/collections-seq-lazy-first.svg)
;; **
;; @@
(def s (rest r)) ; s is a lazy seq
;; @@
;; **
;;; ![Collections seq lazy rest](project-files/images/collections-seq-lazy-rest.svg)
;; **
;; @@
(def b (first (rest r))) ; b is 2
(def b (second r)) ; same thing
;; @@
;; **
;;; ![Collections seq lazy second](project-files/images/collections-seq-lazy-second.svg)
;; **
;; @@
(def c (first (rest (rest r)))) ; c is 3
(def c (nth r 2)) ; same thing
;; @@
;; **
;;; ![Collections seq lazy third](project-files/images/collections-seq-lazy-third.svg)
;;;
;;; ### Sequences and garbage collection
;; **
;; @@
(count (range 10000000))
;; @@
;; **
;;; ![Collections seq lazy GC safe](project-files/images/collections-seq-lazy-GC-safe.svg)
;;;
;;; ### Holding onto the head
;; **
;; @@
(def r (range 10000000))
(count r) ; out of memory error
;; @@
;; **
;;; ![Collections seq lazy GC unsafe](project-files/images/collections-seq-lazy-GC-unsafe.svg)
;;;
;;; ### Sequences in the REPL
;;; * REPL always prints sequences with parens
;;; * But it's not a list!
;;; * Infinite sequences take a long time to print
;; **
;; @@
(set! *print-length* 10) ; only print 10 things
;; @@
;; **
;;; ## Generating a raw sequence
;;; ```clojure
;;; ;; pseudocode
;;; (defn generate-seq [input-source]
;;; (lazy-seq ;macro creates lazy seq of body
;;; (when (more-available? input-source) ; termination check
;;; (cons (get-next-item input-source) ; construct "next seq
;;; (generate-seq input-source))))) ; recursive call (not recur)
;;; ```
;;;
;;; ## Combining sequence functions
;;; ### Sequence power
;;; * Generators
;;; * list, vector, map, SQL ResultSet, Stream, Directory, Iterator, XML, ...
;;; * Operations
;;; * map, filter, reduce, count, some, replace, ...
;;; * Generators * Operators = Power!
;;;
;;; ### Adopting the sequence mindset
;;; * Sequence library surface space is big
;;; * Most things you want to do are in there somewhere
;;; * If you find youself explicitly iterating, look for a function
;;; * The Clojure Cheatsheet helps
;;;
;;; ### Combining sequence functions
;; **
;; @@
;; Sum of the first 50 odd integers
(reduce + (take 50 (filter odd? (range))))
;; @@
;; @@
;; Frequence of vowels in the docstring of 'ns'
(frequencies (re-seq #"[aeiou]" (:doc (meta #'ns))))
;; @@
;; **
;;; ### Thread-last for readability
;;; * "Thread-last"
;;; * Invoke each step with the prior result as the last argument
;;; * Example:
;; **
;; @@
(->> (range) (filter odd?) (take 50) (reduce +))
;; @@
;; **
;;; ### The Fibonacci sequence
;;;
;;; To create a tail-recursive Fibonnaci definition we have to replace the computation of a F(n) = F(n-1) + F(n-2) with something that only has a single recursive call. The trick is to instead recast this definition as F(n, n+1) = F(n-1, n).
;;;
;;; * Start with a seed pair `[0 1]`
;;; * Use `iterate` to produce an infinite sequence by applying a function from that seed
;;; * Each time we recieve a pair of numbers in the sequence we return a new pair that computes the overlapping "next" pair: `[a b] -> [b (a+b)]`:
;; **
;; @@
(take 10
(iterate (fn [[a b]] [b (+ a b)])
[0 1]))
;; @@
;; =>
;;; {"type":"list-like","open":"<span class='clj-lazy-seq'>(</span>","close":"<span class='clj-lazy-seq'>)</span>","separator":" ","items":[{"type":"list-like","open":"<span class='clj-vector'>[</span>","close":"<span class='clj-vector'>]</span>","separator":" ","items":[{"type":"html","content":"<span class='clj-long'>0</span>","value":"0"},{"type":"html","content":"<span class='clj-long'>1</span>","value":"1"}],"value":"[0 1]"},{"type":"list-like","open":"<span class='clj-vector'>[</span>","close":"<span class='clj-vector'>]</span>","separator":" ","items":[{"type":"html","content":"<span class='clj-long'>1</span>","value":"1"},{"type":"html","content":"<span class='clj-long'>1</span>","value":"1"}],"value":"[1 1]"},{"type":"list-like","open":"<span class='clj-vector'>[</span>","close":"<span class='clj-vector'>]</span>","separator":" ","items":[{"type":"html","content":"<span class='clj-long'>1</span>","value":"1"},{"type":"html","content":"<span class='clj-long'>2</span>","value":"2"}],"value":"[1 2]"},{"type":"list-like","open":"<span class='clj-vector'>[</span>","close":"<span class='clj-vector'>]</span>","separator":" ","items":[{"type":"html","content":"<span class='clj-long'>2</span>","value":"2"},{"type":"html","content":"<span class='clj-long'>3</span>","value":"3"}],"value":"[2 3]"},{"type":"list-like","open":"<span class='clj-vector'>[</span>","close":"<span class='clj-vector'>]</span>","separator":" ","items":[{"type":"html","content":"<span class='clj-long'>3</span>","value":"3"},{"type":"html","content":"<span class='clj-long'>5</span>","value":"5"}],"value":"[3 5]"},{"type":"list-like","open":"<span class='clj-vector'>[</span>","close":"<span class='clj-vector'>]</span>","separator":" ","items":[{"type":"html","content":"<span class='clj-long'>5</span>","value":"5"},{"type":"html","content":"<span class='clj-long'>8</span>","value":"8"}],"value":"[5 8]"},{"type":"list-like","open":"<span class='clj-vector'>[</span>","close":"<span class='clj-vector'>]</span>","separator":" ","items":[{"type":"html","content":"<span class='clj-long'>8</span>","value":"8"},{"type":"html","content":"<span class='clj-long'>13</span>","value":"13"}],"value":"[8 13]"},{"type":"list-like","open":"<span class='clj-vector'>[</span>","close":"<span class='clj-vector'>]</span>","separator":" ","items":[{"type":"html","content":"<span class='clj-long'>13</span>","value":"13"},{"type":"html","content":"<span class='clj-long'>21</span>","value":"21"}],"value":"[13 21]"},{"type":"list-like","open":"<span class='clj-vector'>[</span>","close":"<span class='clj-vector'>]</span>","separator":" ","items":[{"type":"html","content":"<span class='clj-long'>21</span>","value":"21"},{"type":"html","content":"<span class='clj-long'>34</span>","value":"34"}],"value":"[21 34]"},{"type":"list-like","open":"<span class='clj-vector'>[</span>","close":"<span class='clj-vector'>]</span>","separator":" ","items":[{"type":"html","content":"<span class='clj-long'>34</span>","value":"34"},{"type":"html","content":"<span class='clj-long'>55</span>","value":"55"}],"value":"[34 55]"}],"value":"([0 1] [1 1] [1 2] [2 3] [3 5] [5 8] [8 13] [13 21] [21 34] [34 55])"}
;; <=
;; **
;;; Then map `first` over the pairs to just grab only the first value of each pair:
;; **
;; @@
(take 10
(map first
(iterate (fn [[a b]] [b (+ a b)])
[0 1])))
;; @@
;; =>
;;; {"type":"list-like","open":"<span class='clj-lazy-seq'>(</span>","close":"<span class='clj-lazy-seq'>)</span>","separator":" ","items":[{"type":"html","content":"<span class='clj-long'>0</span>","value":"0"},{"type":"html","content":"<span class='clj-long'>1</span>","value":"1"},{"type":"html","content":"<span class='clj-long'>1</span>","value":"1"},{"type":"html","content":"<span class='clj-long'>2</span>","value":"2"},{"type":"html","content":"<span class='clj-long'>3</span>","value":"3"},{"type":"html","content":"<span class='clj-long'>5</span>","value":"5"},{"type":"html","content":"<span class='clj-long'>8</span>","value":"8"},{"type":"html","content":"<span class='clj-long'>13</span>","value":"13"},{"type":"html","content":"<span class='clj-long'>21</span>","value":"21"},{"type":"html","content":"<span class='clj-long'>34</span>","value":"34"}],"value":"(0 1 1 2 3 5 8 13 21 34)"}
;; <=
;; **
;;; Then wrap the whole thing up into an infinite sequence:
;; **
;; @@
(def fibs
(map first
(iterate (fn [[a b]] [b (+ a b)])
[0 1])))
;; @@
;; @@
(take 5 fibs) ; consume as many as you need
;; @@
;; **
;;; ## LAB: Sums and Ciphers
;;;
;;; **Note:** All lab solutions are in the section following this one if you want to check your answers!
;;;
;;; ### Fibonacci sum
;;; Clojure's `take` function is commonly used to work with limited portions of infinite sequences.
;;;
;;; Given this infinite, lazy sequence of Fibonacci numbers:
;; **
;; @@
;; Be sure to evaluate this section
(def fibs
(map first
(iterate (fn [[a b]] [b (+ a b)])
[0 1])))
;; @@
;; **
;;; Find the sum of the first fifty Fibonacci numbers.
;; **
;; @@
;; @@
;; **
;;; ## Reduce vs. apply
;;; The solution to the previous exercise can be written with `apply` instead of `reduce`. Why?
;;;
;;; For most uses of `reduce`, this is not the case. Why not?
;; **
;; **
;;; ### Prime sum
;;; The `take` and `drop` functions, as well as variants like `take-while` and `drop-while`, can be combined to select subsequences out of a larger sequence.
;;;
;;; Given this finite, lazy, and inefficient sequence of prime numbers:
;; **
;; @@
;; Make sure to evaluate this section
(def primes
(letfn [(next-prime [known-primes n]
(lazy-seq
(if (some #(zero? (rem n %)) known-primes)
(next-prime known-primes (inc n))
(cons n (next-prime (conj known-primes n) (inc n))))))]
(next-prime [] 2)))
;; @@
;; **
;;; Find the sum of the first fifty primes over one hundred.
;; **
;; @@
;; @@
;; **
;;; ### Ranges of letters
;;; Suppose we want to make a collection of all the capital letters, from _A_ to _Z_. Clojure's `range` function works on numbers, but we can convert between characters and numbers using the `char` and `int` functions.
;;;
;;; Using those functions, plus `map` and `range`, define `letters` as a sequence of capital letters.
;; **
;; @@
(def letters ___)
;; @@
;; **
;;; ### Rotating a sequence
;;; In the next few exercises, we will implement the famous ROT-13 cipher, also known as the Caesar cipher.
;;;
;;; ROT-13 works by "rotating" the alphabet 13 places to the left. So _A_ becomes _N_, _B_ becomes _O_, and so on.
;;;
;;; Clojure's `cycle` function takes a sequential collection and returns the elements of that sequence repeated in an infinite cycle. For example:
;; **
;; @@
(take 10 (cycle [:A :B :C]))
;; @@
;; **
;;; Using `cycle`, rotate the alphabet 13 places to the left.
;; **
;; @@
;; @@
;; **
;;; ### Generic rotation
;;; Define a function which takes two arguments, a collection and a number _n_, and rotates the collection by _n_ places.
;; **
;; @@
(defn rotate [coll n]
___)
;; @@
;; **
;;; ### ROT-13 pairs
;;; Clojure's `map` function takes multiple collection arguments. Use this fact to create a sequence of pairs in the ROT-13 cipher, like `[\A \N]`.
;; **
;; @@
;; @@
;; **
;;; ### ROT-13 as a map
;;; We will define a cipher as a map from plain-text characters to cipher-text characters. We already have a sequence of pairs. Use Clojure's `into` function to convert this sequence into a map.
;; **
;; @@
;; @@
;; **
;;; ### Building a map with reduce
;;; Clojure has a shortcut for this method of constructing maps called `zipmap`. Use `zipmap` to create the same map as the previous exercise. Define this map as `rot13-cipher`.
;; **
;; @@
(def rot13-cipher ___)
;; @@
;; **
;;; ### Invoking the map
;;; In the classic ROT-13 cipher, non-letter characters are left unchanged. Define a function rot13-one-char which takes a single character as its argument. If that character is in the rot13-cipher map, it returns the corresponding value. If the character is not in the map, it returns the original character unchanged.
;;;
;;; _Note:_ Our cipher **only works on capital letters.**
;; **
;; @@
(defn rot13-one-char [c]
___)
;; @@
;; **
;;; ### Enciphering text
;;; Define a function `rot13` that takes a string argument and returns the ROT-13 enciphered version.
;;;
;;; Remember, our cipher **only works on capital letters**.
;;;
;;; _Hint:_ `apply str` will convert a collection of characters to a string.
;; **
;; @@
;; HINT
(rot13 "HELLO, WORLD!") ;; => "URYYB, JBEYQ!"
;; @@
;; @@
;; HINT
;; The ROT-13 cipher is its own inverse, so we can use the same function to decipher text:
(rot13 "URYYB, JBEYQ!") ;; => "HELLO, WORLD!"
;; @@
;; @@
(defn rot13 [text]