On Sep 30, 2010, at 10:37 PM, HiHeelHottie wrote:
> (ns test-test.parse
> (:use [clojure.contrib.string :only (split)]))
>
> (defn parse-char [m c]
> (condp = (:state m)
> :degree (cond
> (Character/isDigit c) (assoc m :degree (+ (* (:degree
> m) 10) (Character/digit c 10)))
> (Character/isWhitespace c) (assoc
> m :state :whitespace :buf (conj (:buf m) (:degree m) " ") :degree 0))
> :whitespace (cond
> (Character/isDigit c) (assoc
> m :state :degree :degree (+ (* (:degree m) 10) (Character/digit c
> 10)))
> (Character/isWhitespace c) m)))
>
> (defn parse [s]
> (let [m (reduce parse-char {:state :degree :degree 0 :buf []} (str s
> " "))]
> (apply str (:buf m))))
>
> (println (parse "1 2 33"))
One minor improvement would be to use clojure.string/join instead of str. That
way you won't have to manually append a space after each number in :buf (nor
use apply).
--
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