Skip to content

Instantly share code, notes, and snippets.

@finalfantasia
Last active September 26, 2019 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save finalfantasia/4b89a0c03752713743df8f16f298b166 to your computer and use it in GitHub Desktop.
Save finalfantasia/4b89a0c03752713743df8f16f298b166 to your computer and use it in GitHub Desktop.
`doseq`, `doall`, `dorun`, `run!`, and `for`

If you have a lazy-seq with side-effects, you almost certainly don't want to let it out of your sight. You're likely to get very strange behavior unless you're exceedingly careful. Most likely, if you've got a lazy-seq with side-effects you should force it with dorun or doall immediately. Use doall if you care about the values in the produced lazy-seq, otherwise use dorun.

This means that dorun should almost always show up right next to the form producing the lazy-seq, which means doseq is very likely a better choice, as it is more efficient and usually more succinct than dorun combined with a lazy-seq producer.

run! is a convenience function that can take place of (dorun (map ,,,)).

for is in rather a different category, since unlike the others it produces a lazy-seq rather than forcing anything. Use for when it's a more convenient way to express the lazy-seq you want than the equivalent combination of map, filter, take-while, etc.

source: https://groups.google.com/d/msg/clojure/8ebJsllH8UY/FxbiHmwJE2oJ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment