Jump to content

Loops/Nested: Difference between revisions

Line 130:
We explicitly return a status flag from the inner loop:
 
<lang clojure>((ns nested
(:import (java.util Random)))
Line 137:
"Random integer in [1, max]."
(inc (.nextInt generator max))))
 
(defn make-vec [n f]
"Calls f n times and returns the result as a vector."
(into [] (map (fn [_] (f)) (range 0 n))))
 
(defn create-matrix [width height]
(letfn [(create-row [] (make-vectake width (repeatedly #(random-int 20))))]
(make-vectake height (repeatedly create-row))))
 
(defn print-matrix [matrix]
(loop [[row & rs] matrix]
(condp = (loop [[x & xs] row]
(println x)
(if (= x 20)
:stop
(if xs
(recur xs)
:continue)))
:continue (when rs (recur rs))
:stop nil)))
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.