What is the difference among the functions doall dorun doseq and for?

后端 未结 2 1900
野性不改
野性不改 2021-01-31 01:55

What is the difference between the functions doall, dorun, doseq, and for ?

I found some information scattered throug

2条回答
  •  孤独总比滥情好
    2021-01-31 02:39

    You can see how dorun and doall relate to one another by looking at the (simplified) source code:

    (defn dorun [coll]
      (when (seq coll) (recur (next coll))))
    
    (defn doall [coll] (dorun coll) coll)
    
    • dorun runs through the sequence, forgetting it as it goes, ultimately returning nil.
    • doall returns its sequence argument, now realised by the dorun.

    Similarly, we could implement doseq in terms of dorun and for:

    (defmacro doseq [seq-exprs & body]
      `(dorun (for ~seq-exprs ~@body)))
    

    For some reason, performance perhaps, this is not done. The standard doseq is written out in full, imitating for.

提交回复
热议问题