| layout | default |
|---|---|
| title | Sequences |
| permalink | /outline/sequences.html |
{::options parse_block_html="true" /}
{% comment %}
http://clojurebridge.github.io/curriculum/outline/sequences.html
{% endcomment %}
{: .slide-title .chapter}
- What are sequences
- Functions for sequences
doseqdotimes
In Clojure, we can say every data structure is a sequence. So far, we learned
vectorandmap, both of which are sequence. String is also a sequence. When something is seq-able, it is a sequence. {: ng-show="block11" .description}
If something is seq-able, it returns the first item in the sequence by the
firstfunction. This is a good test whether it is a sequence or not. {: ng-show="block12" .description}
(turtle-names)
;=> [:trinity :neo :oracle :cypher] ; vector
(first (turtle-names))
;=> :trinity ; the first item
(:trinity (state))
;=> {:x 0, :y 0, :angle 90, :color [30 30 30]} ; map
(first (:trinity (state)))
[:x 0] ; the first item
(first "Hello, World!") ; string
;=> \H ; the first item
(first :trinity) ; keyword is not seq-able
;=> IllegalArgumentException Don't know how to create ISeq from:
clojure.lang.Keyword clojure.lang.RT.seqFrom (RT.java:528)Clojure is very good at iterate over a sequence. There are many functions that interact on sequences. For example,
doseq,dotimes,for,loop,doall, ordorun.We already saw
mapandreducefunctions in "Functions that takes other functions" section. These are also functions for sequences. {: ng-show="block21" .description}
The
doseq(for do a sequence) is one of well-used functions for sequences, and works quite similar tomapfunction. The function repeatedly evaluates given body form with each element in the given sequence. {: ng-show="block31" .description}
The
doseqfunction takes bindings as arguments. The arguments might be an odd-looking vector:[name sequence]. When each element of a sequence is iterated over, the element is assigned to the name one by one. {: ng-show="block32" .description}
;; doseq example
(doseq [n (turtle-names)] (forward n 40))- Turtles Walk (more function study)
- section 5 and later
- Snowflakes (animation)
- step 4 and later
- Twinkle Twinkle Little Star (making sounds)
chordfunction and later
The
dotimes(for do number of times) is another well-used function for sequences. Likedoseq, the function repeatedly evaluates given body form. The difference is the binding in the argument. Thedotimestakes:[name max-integer]. {: ng-show="block41" .description}
The
dotimesfunction is the closest to so-called for-loop in other programming languages. This function allows us an index access to the given sequence by a combination withnth. {: ng-show="block42" .description}
;; assuming there are multiple turtles
(def names (turtle-names))
(dotimes [n (count names)] (right (nth names n) (* 45 n)))- Turtles Walk (more function study)
- section 6 and later
- Snowflakes (animation)
- step 5-4 and later
- Twinkle Twinkle Little Star (making sounds)
- complete Twinkle Little Star section
{% comment %}
🌟 A link below is for a slide only. Go to README.md instead. 🌟
{% endcomment %}