On Wed, Jul 27, 2011 at 8:25 PM, Sean Corfield <seancorfi...@gmail.com> wrote:
> On Wed, Jul 27, 2011 at 4:32 PM, Ken Wesson <kwess...@gmail.com> wrote:
>> => (.format (java.util.Formatter.) "%d" (into-array Object [(bigint 2)]))
>> #<Formatter 2>
>>
>> (2N isn't recognized as a BigInteger literal by Clojure 1.2, it seems.)
>>
>> In my copy of Clojure 1.2, format also seems to work:
>>
>> => (format "%d" (bigint 2))
>> "2"
>
> In Clojure 1.2:
>
> (type (bigint 2)) => java.math.BigInteger
>
> In Clojure 1.3:
>
> (type (bigint 2)) => clojure.lang.BigInt
> (type 2N) => clojure.lang.BigInt

What the devil? Why was this done? Seems like wheel reinvention to me.

And format should account for it. A simple change will do it:

(defn format
  "Formats a string using java.lang.String.format, see
java.util.Formatter for format
  string syntax"
  {:added "1.0"
   :static true}
  ^String [fmt & args]
  (String/format fmt (to-array (map fixup args))))

where

(defn fixup [o]
  (cond
    (instance? clojure.lang.BigInt o) (.toBigInteger o)
    ...
    :else o))

Of course, this suggests a generalization of condp:

(defn replace [rmap coll]
  (let [s (map #(if-let [[_ v] (find rmap %)] v %) coll)]
    (if (seq? coll)
      s
      (into (empty coll) s))))

(defmacro condx [symb expr & clauses]
  (let [default (if (odd? (count clauses)) [:else (last clauses)] [])]
    `(cond
       ~@(mapcat
           (fn [[v x]]
             [(replace {symb v} expr) x])
           (partition 2 clauses))
       ~@default)))

(defn fixup [o]
  (condx class (instance? class o)
    clojure.lang.BigInt (.toBigInteger o)
    ...
    o))

Of course, full flexibility might warrant a multimethod for fixup so
that format can be extended to any type. Format tends to occur in I/O
bound code so the runtime overhead of multimethods is probably
acceptable -- indeed, the pprint contrib library already employs them
in a very similar role.

-- 
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

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