Skip to content

Latest commit

 

History

History
162 lines (124 loc) · 5.34 KB

sequences.md

File metadata and controls

162 lines (124 loc) · 5.34 KB
layout title permalink
default
Sequences
/outline/sequences.html

{::options parse_block_html="true" /}

{% comment %}

http://clojurebridge.github.io/curriculum/outline/sequences.html

{% endcomment %}

[BONUS]

Sequences

{: .slide-title .chapter}

  • What are sequences
  • Functions for sequences
    • doseq
    • dotimes
### What are sequences? {: .slide_title .slide}

Clojure's data structures

In Clojure, we can say every data structure is a sequence. So far, we learned vector and map, both of which are sequence. String is also a sequence. When something is seq-able, it is a sequence. {: ng-show="block11" .description}

first item or not

If something is seq-able, it returns the first item in the sequence by the first function. This is a good test whether it is a sequence or not. {: ng-show="block12" .description}

#### Results of `first`
(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)
### Functions for sequences

Clojure is very good at iterate over a sequence. There are many functions that interact on sequences. For example, doseq, dotimes, for, loop, doall, or dorun.

We already saw map and reduce functions in "Functions that takes other functions" section. These are also functions for sequences. {: ng-show="block21" .description}

#### `doseq`

The doseq(for do a sequence) is one of well-used functions for sequences, and works quite similar to map function. The function repeatedly evaluates given body form with each element in the given sequence. {: ng-show="block31" .description}

The doseq function 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))
#### EXERCISE 1
#### `dotimes`

The dotimes(for do number of times) is another well-used function for sequences. Like doseq, the function repeatedly evaluates given body form. The difference is the binding in the argument. The dotimes takes: [name max-integer]. {: ng-show="block41" .description}

The dotimes function 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 with nth. {: ng-show="block42" .description}

;; assuming there are multiple turtles
(def names (turtle-names))
(dotimes [n (count names)] (right (nth names n) (* 45 n)))
#### EXERCISE 2

{% comment %}

🌟 A link below is for a slide only. Go to README.md instead. 🌟

{% endcomment %}

Return to the first slide, or go to the [curriculum outline](/curriculum/#/1).