Ok, I've got a couple thousand lines of Clojure under my belt, but
this has me stumped, unless it's a compiler etc. issue.  If I'm
missing something dumb, what is it, please?

I have a function that is failing a not-nil precondition.  Here are
four versions of the same test; only #3 works correctly.  Obviously I
can work around the problem, but am I doing something wrong?

user> (def fq (frequencies "abbccc"))
#'user/fq

user> fq
{\a 1, \b 2, \c 3}

user> (fq \a)
1

user> ; incorrectly fails :pre
(defn test-fun1
  [mp k]
  {:pre (not (nil? (mp k)))}
  (mp k))
#'user/test-fun1

user> (test-fun1 fq \a)

Assert failed: (nil? (mp k))
  [Thrown class java.lang.AssertionError]

user> ; incorrectly fails :pre
(defn test-fun2
  [mp k]
  {:pre (not= nil (mp k))}
  (mp k))
#'user/test-fun2

user> (test-fun2 fq \a)

Assert failed: nil
  [Thrown class java.lang.AssertionError]

user> ; correctly survives :pre
(defn test-fun3
  [mp k]
  {:pre (not= 0 (mp k 0))}
  (mp k))

#'user/test-fun3
user> (test-fun3 fq \a)
1

user> ; incorrectly fails :pre
(defn test-fun4
  [mp k]
  {:pre (not= nil (mp k nil))}
  (mp k))
#'user/test-fun4

user> (test-fun4 fq \a)

Assert failed: nil
  [Thrown class java.lang.AssertionError]

Thanks in advance,
-Tom

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to