when-first

added
1.0

ns
clojure.core

type
macro

(when-first bindings & body)

bindings => x xs

Roughly the same as (when (seq xs) (let [x (first xs)] body)) but xs is evaluated only once

                user=> (when-first [a [1 2 3]] a)
1
user=> (when-first [a []] :x)
nil
user=> (when-first [a nil] :x)
nil
            
                ; Note that the 'when' switches on the truthiness of the sequence, not the truthiness of the elements within the sequence.

user=> (when-first [a [nil 2 3]] 
         (print (str "Picked: " (prn-str a))))
Picked: nil
nil