Hi Mark,

2009/10/21 Mark Nutter <manutte...@gmail.com>:
>
> (defn blank? [s] (every? #(Character/isWhitespace %) s))

<snip>

> user=> (defn hexchar? [c] (re-find #"[0-9A-Fa-f]" c))

<snip>

> user=> (defn hex? [s] (every? #(hexchar? %) s))
> #'user/hex?
> nil
> user=> (hex? "a")
> #<CompilerException java.lang.ClassCastException: java.lang.Character
> cannot be cast to java.lang.CharSequence (NO_SOURCE_FILE:0)>

If you'll look at the argument types of the two predicates, you'll
notice that Character/isWhiteSpace takes a char as an argument whereas
you predicate takes a String (or more specifically a CharSequence).
The (every?) form is trying to call (hexchar?) with char arguments and
it blows up.

One way to fix this would be to produce a string out of c:

(defn hexchar? [c] (re-find #"[0-9A-Fa-f]" (str c)))

but you should really change hexchar? into something more elegant.

A few style pointers:

- predicates, i.e. functions that end in ?, should really return a boolean
- you can replace #(hexchar? %) with hexchar? in the every? form. This
doesn't work in the Character/isWhitespace case because that's a Java
call.

> Mark

HTH!

-- 
  ! Lauri

--~--~---------~--~----~------------~-------~--~----~
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