JanetDocsPlaygroundI'm feeling luckyGitHub sign in

and



    macro
    boot.janet on line 275, column 1

    (and & forms)

    Evaluates to the last argument if all preceding elements are 
    truthy, otherwise evaluates to the first falsey argument.


See also:band1 exampleSign in to add an example
Loading...
(and true true)   # => true
(and true false)  # => false

(and true false nil)  # => false
(and true nil false)  # => nil

(and)       # => true
(and true)  # => true

(and 1)                  # => 1
(and 1 "hello")          # => "hello"
(and [false] 1 "world")  # => "world"

# note that `and` does not behave as you might expect
# when used with `apply` and `splice`:
(and 1 2 3)                 # => 3
(and (splice [1 2 3]))      # => (1 2 3)
(apply and [1 2 3])         # => (if 1 (if 2 3 2) 1)
(eval (apply and [1 2 3]))  # => 3

# if you need an `and` which you can feed to `apply`,
# you can use a reduce-based implementation:
(defn and2 [& xs]
  (reduce
    (fn [a b] (if (not a) a b))
    true xs))

# however, note that `and2` does not implement short-circuiting:
# janet:23:> (and false (do (print "hello") true))
# false
# janet:24:> (and2 false (do (print "hello") true))
# hello
# false

# alternatively, you can use `all` for this case.
(all truthy?  [true 0 1     'a :a "a" [] {}])  # => true
(all truthy?  [true 0 nil   'a :a "a" [] {}])  # => nil
(all truthy?  [true 0 false 'a :a "a" [] {}])  # => false
(apply (partial all truthy?) [[true 0 1 'a :a "a" [] {}]])  # => true
cellularmitosisPlayground