Re: Get digits of a number

2013-01-20 Thread Qiu Xiafei
yes... it would be better. On Sun, Jan 20, 2013 at 2:30 AM, David Brown wrote: > Qiu Xiafei writes: > > > (defn num->digits > > [num] > > (loop [n num res []] > > (if (zero? n) > > res > > (recur (long (/ n 10)) (cons (mod n 10) res) > > How about (quot n 10) instead o

Re: Get digits of a number

2013-01-19 Thread David Brown
Qiu Xiafei writes: > (defn num->digits >   [num] >   (loop [n num res []] >     (if (zero? n) >       res >       (recur (long (/ n 10)) (cons (mod n 10) res) How about (quot n 10) instead of (long (/ n 10))? No sense in the extra step. David -- You received this message because you ar

Re: Get digits of a number

2013-01-19 Thread Qiu Xiafei
(defn num->digits [num] (loop [n num res []] (if (zero? n) res (recur (long (/ n 10)) (cons (mod n 10) res) 在 2011年2月17日星期四UTC+8下午1时29分10秒,Andreas Kostler写道: > > Is there an easy and idiomatic way of getting the digits of a number in > clojure? > > (defn explode-to-digits

Re: Get digits of a number

2011-02-17 Thread Robert McIntyre
I normally use (defn digits [n] (map #(Integer/parseInt (str %)) (seq (str n You can adapt it to read in different bases easily. sincerely, --Robert McIntyre On Thu, Feb 17, 2011 at 9:45 PM, Matthew Boston wrote: > How about using the Java api Character/getNumbericValue, like so: > > (

Re: Get digits of a number

2011-02-17 Thread Matthew Boston
How about using the Java api Character/getNumbericValue, like so: (defn explode-to-digits [number] (map #(Character/getNumericValue %) (str number))) user => (explode-to-digits 12345) (1 2 3 4 5) On Feb 17, 4:45 pm, Mike Meyer wrote: > On Thu, 17 Feb 2011 15:27:47 -0600 > > Michael Gardner w

Re: Get digits of a number

2011-02-17 Thread Mike Meyer
On Thu, 17 Feb 2011 15:27:47 -0600 Michael Gardner wrote: > On Feb 17, 2011, at 1:36 PM, Mike Meyer wrote: > > > My turn... > > > > (defn to-digits > > "Create a seq of digits from a number." > > [i] > > ^{:user/comment "For Euler Problems (Specifically 16)"} > > (map {\0 0 \1 1 \2 2 \3 3 \

Re: Get digits of a number

2011-02-17 Thread Michael Gardner
On Feb 17, 2011, at 1:36 PM, Mike Meyer wrote: > My turn... > > (defn to-digits > "Create a seq of digits from a number." > [i] > ^{:user/comment "For Euler Problems (Specifically 16)"} > (map {\0 0 \1 1 \2 2 \3 3 \4 4 \5 5 \6 6 \7 7 \8 8 \9 9} > (str i))) > > No assumption about repr

Re: Get digits of a number

2011-02-17 Thread Mike Meyer
On Thu, 17 Feb 2011 10:26:05 -0800 (PST) JMatt wrote: > On Feb 16, 10:29 pm, Andreas Kostler > wrote: > > Is there an easy and idiomatic way of getting the digits of a number in > > clojure? > Here is my attempt at this from a few months ago: My turn... (defn to-digits "Create a seq of digit

Re: Get digits of a number

2011-02-17 Thread JMatt
On Feb 16, 10:29 pm, Andreas Kostler wrote: > Is there an easy and idiomatic way of getting the digits of a number in > clojure? Here is my attempt at this from a few months ago: (defn to-digit "Create a seq of digits from a number." ^{:user/comment "For Euler Problems (Specifically 16)"}

Re: Get digits of a number

2011-02-17 Thread Ken Wesson
On Thu, Feb 17, 2011 at 1:39 AM, Shantanu Kumar wrote: > On Feb 17, 11:09 am, Ken Wesson wrote: >> On Thu, Feb 17, 2011 at 12:29 AM, Andreas Kostler >> >> wrote: >> > Is there an easy and idiomatic way of getting the digits of a number in >> > clojure? >> >> > (defn explode-to-digits [number] >

Re: Get digits of a number

2011-02-16 Thread Ken Wesson
On Thu, Feb 17, 2011 at 12:29 AM, Andreas Kostler wrote: > Is there an easy and idiomatic way of getting the digits of a number in > clojure? > > (defn explode-to-digits [number] >        (map #(- (int %) (int \0)) (str number))) > (explode-to-digits 123456) > => (1 2 3 4 5 6) > > Seems a bit clu

Re: Get digits of a number

2011-02-16 Thread Shantanu Kumar
On Feb 17, 11:39 am, Shantanu Kumar wrote: > On Feb 17, 11:09 am, Ken Wesson wrote: > > > > > > > > > > > On Thu, Feb 17, 2011 at 12:29 AM, Andreas Kostler > > > wrote: > > > Is there an easy and idiomatic way of getting the digits of a number in > > > clojure? > > > > (defn explode-to-digits

Re: Get digits of a number

2011-02-16 Thread Shantanu Kumar
On Feb 17, 11:09 am, Ken Wesson wrote: > On Thu, Feb 17, 2011 at 12:29 AM, Andreas Kostler > > wrote: > > Is there an easy and idiomatic way of getting the digits of a number in > > clojure? > > > (defn explode-to-digits [number] > >        (map #(- (int %) (int \0)) (str number))) > > (explod

Re: Get digits of a number

2011-02-16 Thread Saul Hazledine
On Feb 17, 6:29 am, Andreas Kostler wrote: > Is there an easy and idiomatic way of getting the digits of a number in > clojure? > > (defn explode-to-digits [number] >         (map #(- (int %) (int \0)) (str number))) > (explode-to-digits 123456) > => (1 2 3 4 5 6) Sorry, my first answer was care

Re: Get digits of a number

2011-02-16 Thread Andreas Kostler
Hi Saul, > (defn explode-to-digits [number] >(seq (str number))) This gives you a sequence of character representations for the digits e.g: (explode-to-digits 1234) => (\1 \2 \3 \4) On 17/02/2011, at 4:01 PM, Saul Hazledine wrote: > On Feb 17, 6:29 am, Andreas Kostler > wrote: >> Is there

Re: Get digits of a number

2011-02-16 Thread Saul Hazledine
On Feb 17, 6:29 am, Andreas Kostler wrote: > Is there an easy and idiomatic way of getting the digits of a number in > clojure? > > (defn explode-to-digits [number] >         (map #(- (int %) (int \0)) (str number))) > (explode-to-digits 123456) > => (1 2 3 4 5 6) I'd do it this way: (defn expl

Get digits of a number

2011-02-16 Thread Andreas Kostler
Is there an easy and idiomatic way of getting the digits of a number in clojure? (defn explode-to-digits [number] (map #(- (int %) (int \0)) (str number))) (explode-to-digits 123456) => (1 2 3 4 5 6) Seems a bit clunky... Andreas -- You received this message because you are subscribed