(require '[clojure.string :as str])
(def numbers [2 3 5 7 11 13 17 19])
;; Clojure has no "break": a loop ends simply by not calling recur
(def s
(loop [[n & more] numbers, acc []]
(if (or (nil? n) (> n 10))
(str/join "-" acc)
(recur more (conj acc n)))))
;; s is "2-3-5-7"
(println "s is" s)
;; inside reduce the early exit is "reduced"
(println "with reduce:"
(str/join "-" (reduce (fn [acc n]
(if (> n 10) (reduced acc) (conj acc n)))
[]
numbers)))
;; and said declaratively it is just take-while
(println "with take-while:" (str/join "-" (take-while #(<= % 10) numbers)))