when-first

clojure.core

  • (when-first bindings & body)
bindings => x xs

Same as (when (seq xs) (let [x (first xs)] body))

1 Example top

  • user=> (when-first [a [1 2 3]] a)
    1
    user=> (when-first [a []] a)
    nil
    user=> (when-first [a nil] a)
    nil
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:4050 top

(defmacro when-first
  "bindings => x xs

  Same as (when (seq xs) (let [x (first xs)] body))"
  {:added "1.0"}
  [bindings & body]
  (assert-args when-first
     (vector? bindings) "a vector for its binding"
     (= 2 (count bindings)) "exactly 2 forms in binding vector")
  (let [[x xs] bindings]
    `(when (seq ~xs)
       (let [~x (first ~xs)]
         ~@body))))
Vars in clojure.core/when-first:
Used in 0 other vars

Comments top

No comments for when-first. Log in to add a comment.