dorun

added
1.0

ns
clojure.core

type
function

(dorun coll) (dorun n coll)

When lazy sequences are produced via functions that have side
effects, any effects other than those needed to produce the first
element in the seq do not occur until the seq is consumed. dorun can
be used to force any effects. Walks through the successive nexts of
the seq, does not retain the head and returns nil.

                user=> (dorun 5 (repeatedly #(println "hi")))
hi
hi
hi
hi
hi
hi
nil
            
                user=> (let [x (atom 0)]
         (dorun (take 10 (repeatedly #(swap! x inc))))
         @x)
10
            
                user=> (dorun (map #(println "hi" %) ["mum" "dad" "sister"]))
hi mum
hi dad
hi sister
nil
            
                ;;map a function which makes database calls over a vector of values 
user=> (map #(db/insert :person {:name %}) ["Fred" "Ethel" "Lucy" "Ricardo"])
JdbcSQLException The object is already closed [90007-170]  org.h2.message.DbE
xception.getJdbcSQLException (DbException.java:329)

;;database connection was closed before we got a chance to do our transactions
;;lets wrap it in dorun
user=> (dorun (map #(db/insert :person {:name %}) ["Fred" "Ethel" "Lucy" "Ricardo"]))
DEBUG :db insert into person values name = 'Fred'
DEBUG :db insert into person values name = 'Ethel'
DEBUG :db insert into person values name = 'Lucy'
DEBUG :db insert into person values name = 'Ricardo'
nil