there's another edge case when using and/or :
getting passed an unbound var where for example `nil?` and `str` applied
to it don't throw, and of course also `or` and `and`, ie.:
=> (def a)
#'cgws.notcore/a
=> a
#<Unbound Unbound: #'cgws.notcore/a>
=> (nil? a)
false
=> (str a)
"Unbound: #'cgws.notcore/a"
=> (or a)
#<Unbound Unbound: #'cgws.notcore/a>
=> (or 1 2 a)
1
=> (or a 1 2)
#<Unbound Unbound: #'cgws.notcore/a>
=> (and 1 2 3 a)
#<Unbound Unbound: #'cgws.notcore/a>
=> (and a 1 2 3)
3
=> (type a)
clojure.lang.Var$Unbound
=> (cond a 2)
2
=> (when a 3)
3
=> (if a 4 5)
4
=> (bound? #'a)
false
=> (bound? a) ; in case anyone was wondering
ClassCastException clojure.lang.Var$Unbound cannot be cast to
clojure.lang.Var clojure.core/bound?/fn--4837 (core.clj:4954)
=> (defn test1 [input]
(cond (and (not (nil? input)))
(println "received nice input=`" input "`")
:else
(throw (RuntimeException. (str "bad input:" input)))))
#'cgws.notcore/test1
=> (test1 1)
received nice input=` 1 `
nil
=> (test1 nil)
RuntimeException bad input: cgws.notcore/test1 (NO_SOURCE_FILE:5)
=> (test1 a)
received nice input=` #<Unbound Unbound: #'cgws.notcore/a> `
nil
but I guess I should've put this in its proper thread aka here:
https://groups.google.com/forum/#!msg/clojure/LmpcTRPUAY0/8ieaRmM7pIUJ
On Wed, May 22, 2013 at 1:28 PM, atkaaz <[email protected]> wrote:
>
>
>
> On Wed, May 22, 2013 at 3:06 AM, Peter Mancini <[email protected]> wrote:
>
>> I noticed that '(nil nil true) will cause "and" to produce false, so I
>> am aware of that edge case. Anything else I should be aware of?
>>
>> What about the other edge?
> user=> (reduce #(and %1 %2) '(1 true 2))
> 2
> user=> (eval (conj '(1 true 3) 'and))
> 3
>
> user=> (doc and)
> -------------------------
> clojure.core/and
> ([] [x] [x & next])
> Macro
> Evaluates exprs one at a time, from left to right. If a form
> returns logical false (nil or false), and returns that value and
> doesn't evaluate any of the other expressions, otherwise it returns
> the value of the last expr. (and) returns true.
> nil
>
>
>> Thanks.
>>
>> --
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to [email protected]
>> Note that posts from new members are moderated - please be patient with
>> your first post.
>> To unsubscribe from this group, send email to
>> [email protected]
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>
>
>
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.