Skip to content

Instantly share code, notes, and snippets.

@pesterhazy
Created May 11, 2015 08:48
Show Gist options
  • Save pesterhazy/e191d0157b6f5cfe2ed8 to your computer and use it in GitHub Desktop.
Save pesterhazy/e191d0157b6f5cfe2ed8 to your computer and use it in GitHub Desktop.
when-seq: conditionally apply operation on sequence in threading macro
(defn when-seq
"If condition is true, perform seq operation f on xs with args; otherwise return f.
Intended for use in the thread first macro. For example:
(-> []
(when-seq x conj x))
will be [:foo] if x is :foo, [] if x is nil"
[xs condition f & args]
(if condition
(apply f xs args)
xs))
@pesterhazy
Copy link
Author

clojure.core/cond-> is probably more useful

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