Is there standard "foreach" function in Clojure?

21,652

Solution 1

I think doseq might be what you're looking for:

(doseq [i [0 1 2 3]] (println i))

However this will still return nil - all forms in Clojure will evaluate to some value; there's no equivalent to Common Lisp's (values).

Solution 2

As of Clojure 1.7 you can use run!:

user=> (run! println (range 10))

0
1
2
3
4
5
6
7
8
9
=> nil  ; returns nil

See this answer to How to print each item of list on separate line clojure?

Solution 3

You could use doseq or doall. Following is the example using doall.

(defn div618 [p1 p2]
    (let [ratio [0.,0.191,0.236,0.382,0.5,0.618,0.809,1.]
          price (fn [r] (if (<= p1 p2) (+ p1 (* (- p2 p1) r)) (- p1 (* (- p1 p2) r))))]

    (doall (map #(println (format "-------%.3f   %.2f-------" %1 (price %1))) (if (<= p1 p2) (reverse ratio) ratio)))))

You could also separate the printing from the function calculation like this,

(defn div618 [p1 p2]
    (let [ratio [0.,0.191,0.236,0.382,0.5,0.618,0.809,1.]
          price (fn [r] (if (<= p1 p2) (+ p1 (* (- p2 p1) r)) (- p1 (* (- p1 p2) r))))]

    (map (fn [x] [x (price x)]) (if (<= p1 p2) (reverse ratio) ratio))))

(doall (map #(println (format "-------%.3f   %.2f-------" (first %1) (last %1))) (div618 1 2)))

Solution 4

map is used to apply a function f to every element of the seq, in your case your applying print which returns nil, so you get a intermediate seq of nils which you then throw away. When you want side effects like printing you use doseq.

Clojure does not have a single foreach form map, reduce filter etc. technically these are all foreach forms they all do something to each element in the seq. Also map is lazy your example will only print in the repl cause repl forces lazy seq to realize them selfs say if you pack this in a jar it won't do anything.

Solution 5

(defn div618 [p1 p2]
  (let [ratio [0.000 0.191 0.236 0.382 0.500 0.618 0.809 1.000]            
        price #(if (<= p1 p2)
                 (-> p2 (- p1) (* %) (+ p1))
                 (-> p1 (- p2) (* %) (- p1)))
        print-all #(doseq [item %]
                     (printf "-------%.3f   %.2f-------\n" item (price item)))]
    (if (<= p1 p2)
      (print-all (reverse ratio))
      (print-all ratio))))
Share:
21,652
z_axis
Author by

z_axis

e^(π.i) + 1 = 0

Updated on October 29, 2020

Comments

  • z_axis
    z_axis over 3 years
    Clojure> (doc foreach)
    Unable to resolve var: foreach in this context
    
    Clojure> (doc map)
    -------------------------
    clojure.core/map
    ([f coll] [f c1 c2] [f c1 c2 c3] [f c1 c2 c3 & colls])
    

    Of course, i can use the "map" to mimic "foreach" but the map always return nil, which makes the output ugly in the following function:

    (defn div618 [p1 p2]
        (let [ratio [0.,0.191,0.236,0.382,0.5,0.618,0.809,1.]
              price (fn [r] (if (<= p1 p2) (+ p1 (* (- p2 p1) r)) (- p1 (* (- p1 p2) r))))]
    
        (if (<= p1 p2)
            (**map** #(println (format "-------%.3f   %.2f-------" %1 (price %1))) (reverse ratio))
            (**map** #(println (format "-------%.3f   %.2f-------" %1 (price %1)))  ratio))))
    

    Sincerely!

  • z_axis
    z_axis over 12 years
    Clojure> (for [x (range 10)] (inc x)) (1 2 3 4 5 6 7 8 9 10) Clojure> (for [x (range 10)] (println x)) (nil nil nil nil nil nil nil nil nil nil)
  • Svante
    Svante over 12 years
    @z_axis: Tick the check mark on the left to accept an answer. Also, you might want to upvote it.
  • divs1210
    divs1210 over 8 years
    Note: no commas, ad-hoc left-to-right math syntax using thread-first (->), and doseq which is essentially Clojure's foreach.
  • Post Self
    Post Self about 5 years
    Exactly what I was looking for. Works like Julia's foreach