Digging into deeply nested sequence in Clojure

less than 1 minute read

Notes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
;; dig.clj

(defn dig [sequence & indexes]
  (let
   [first-index (first indexes)
    rest-indexes (rest indexes)
    first-indexed-value (get sequence first-index)]
    (if (empty? rest-indexes)
      first-indexed-value
      (recur first-indexed-value rest-indexes))))


(dig [1 [1 2 {:a "a" :b "b"}] 3] 1)

(dig [1 [1 2 {:a "a" :b "b"}] 3] 1 2)

(dig [1 [1 2 {:a "a" :b "b"}] 3] 1 2 :a)

(dig [1 [1 2 {:a "a" :b "b"}] 3] 1 2 :c)

(dig [1 [1 2 {:a "a" :b "b"}] 3] 1 2 :c 4)

Updated: