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 cloj...@davidb.org wrote:

 Qiu Xiafei qiuxia...@gmail.com 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 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


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

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

Re: Get digits of a number

2013-01-19 Thread David Brown
Qiu Xiafei qiuxia...@gmail.com 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 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


Re: Get digits of a number

2011-02-17 Thread Ken Wesson
On Thu, Feb 17, 2011 at 1:39 AM, Shantanu Kumar
kumar.shant...@gmail.com wrote:
 On Feb 17, 11:09 am, Ken Wesson kwess...@gmail.com wrote:
 On Thu, Feb 17, 2011 at 12:29 AM, Andreas Kostler

 andreas.koestler.le...@gmail.com 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 clunky...

 How about

 (defn explode-to-digits [n]
   (if (= n 0)
     []
     (let [d (rem n 10)
           r (quot n 10)]
       (conj (explode-to-digits r) d

 or if you want to avoid consuming stack

 (defn explode-to-digits* [n out]
   (if (= n 0)
     out
     (let [d (rem n 10)
           r (quot n 10)]
       (recur r (cons d out)

 (defn explode-to-digits [n]
   (explode-to-digits* n nil))

 It should also be possible to make a lazy sequence version, but
 returning the digits in the reverse order.

 A bit shorter:

 (defn digits [n]
  (let [r (rem n 10)
        q (int (/ n 10))
        _ (println (format q=%d, r=%d q r))]
    (if (zero? q) [r]
      (into [r] (digits q)

 Not efficient though.

If you really want efficient:

(defn digits [n]
  (loop [n (int n)
 out nil]
(if (zero? n)
  out
  (recur
(unchecked-divide n 10)
(cons
  (unchecked-remainder n 10)
  out)

If you really /really/ want efficient:

(defn digits [n dig-max]
  (let [a (int-array dig-max)]
(loop [n (int n) i (int (dec dig-max))]
  (when-not (zero? n)
(aset a i (unchecked-remainder n 10))
(recur
  (unchecked-divide n 10)
  (unchecked-dec i
(seq a)))

Icky but should be fastest possible in Clojure. This one will throw an
exception if dig-max is too small and produce leading zeros if it's
bigger than the number of digits of n, and it will truncate inputs
that don't fit in a 32-bit signed int. You can wrap in (drop-while
zero? ...) to lose the latter behavior but it will slow things down,
or use Math/log10 and slow things down even more.

All of the variations I've posted will turn negative numbers into seqs
of digits that are all negated, e.g. (-1 0 -2 -4 -5 -6) and are liable
to hang, blow the heap (pretty slowly), or blow the stack (variously)
on fractions of any kind (Ratios, doubles, etc.).

In practice, if I needed something like this in production code, I'd
probably use one of the nicely idiomatic versions from earlier --
after all, the complexity is logarithmic in the magnitudes of the
numbers you're using anyway -- and handle negative and fractional
inputs in some more elegant manner. I'd probably also generalize it to
accept an arbitrary radix as an optional second parameter. This would
simply replace all the literal 10s in the code. It too would need to
be sanity checked, for being an integer and = 2.

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


Re: Get digits of a number

2011-02-17 Thread JMatt


On Feb 16, 10:29 pm, Andreas Kostler
andreas.koestler.le...@gmail.com 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)}
  [i  {:keys [type] :or {type int}}]
   (let [ss (str i)] (map type (map (fn [s] (- (int s) 48)) ss

I took a more pragmatic approach to this problem and just assumed the
representation for digits was ASCII. It's possible to simplify this
some but, as is, it'll take seqs of characters or strings. At the time
I remember thinking this function should really return bytes or shorts
or something smaller than an int. Thus the :type option.


JMatt

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


Re: Get digits of a number

2011-02-17 Thread Mike Meyer
On Thu, 17 Feb 2011 10:26:05 -0800 (PST)
JMatt jm...@jmatt.org wrote:
 On Feb 16, 10:29 pm, Andreas Kostler
 andreas.koestler.le...@gmail.com 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 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 representation here. But my Python background is
showing - Pythons dictionaries are used *everywhere* in the language,
and hence tuned as tightly as possible and thus blasted fast.

mike
-- 
Mike Meyer m...@mired.org http://www.mired.org/consulting.html
Independent Software developer/SCM consultant, email for more information.

O ascii ribbon campaign - stop html mail - www.asciiribbon.org

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


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 representation here. But my Python background is
 showing - Pythons dictionaries are used *everywhere* in the language,
 and hence tuned as tightly as possible and thus blasted fast.

Why not use Character/digit, as Saul suggested?

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


Re: Get digits of a number

2011-02-17 Thread Mike Meyer
On Thu, 17 Feb 2011 15:27:47 -0600
Michael Gardner gardne...@gmail.com 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 \4 4 \5 5 \6 6 \7 7 \8 8 \9 9}
(str  i)))
 Why not use Character/digit, as Saul suggested?

Because I'm not a java programmer, so my natural inclination is to use
Clojure tools (like the hashmap) rather than Java tools. Since I
hadn't seen a solutions using the hashamp - but had seen some more
complex variants - I thought this one might be of interest.

   mike
-- 
Mike Meyer m...@mired.org http://www.mired.org/consulting.html
Independent Software developer/SCM consultant, email for more information.

O ascii ribbon campaign - stop html mail - www.asciiribbon.org

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


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 m...@mired.org wrote:
 On Thu, 17 Feb 2011 15:27:47 -0600

 Michael Gardner gardne...@gmail.com 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 \4 4 \5 5 \6 6 \7 7 \8 8 \9 9}
         (str  i)))
  Why not use Character/digit, as Saul suggested?

 Because I'm not a java programmer, so my natural inclination is to use
 Clojure tools (like the hashmap) rather than Java tools. Since I
 hadn't seen a solutions using the hashamp - but had seen some more
 complex variants - I thought this one might be of interest.

    mike
 --
 Mike Meyer m...@mired.org          http://www.mired.org/consulting.html
 Independent Software developer/SCM consultant, email for more information.

 O ascii ribbon campaign - stop html mail -www.asciiribbon.org

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


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
matthew.bos...@gmail.com wrote:
 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 m...@mired.org wrote:
 On Thu, 17 Feb 2011 15:27:47 -0600

 Michael Gardner gardne...@gmail.com 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 \4 4 \5 5 \6 6 \7 7 \8 8 \9 9}
         (str  i)))
  Why not use Character/digit, as Saul suggested?

 Because I'm not a java programmer, so my natural inclination is to use
 Clojure tools (like the hashmap) rather than Java tools. Since I
 hadn't seen a solutions using the hashamp - but had seen some more
 complex variants - I thought this one might be of interest.

    mike
 --
 Mike Meyer m...@mired.org          http://www.mired.org/consulting.html
 Independent Software developer/SCM consultant, email for more information.

 O ascii ribbon campaign - stop html mail -www.asciiribbon.org

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

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


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


Re: Get digits of a number

2011-02-16 Thread Saul Hazledine
On Feb 17, 6:29 am, Andreas Kostler andreas.koestler.le...@gmail.com
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 explode-to-digits [number]
(seq (str number)))

Saul

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


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 andreas.koestler.le...@gmail.com
 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 explode-to-digits [number]
(seq (str number)))
 
 Saul
 
 -- 
 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

--
Programs must be written for people to read, and only incidentally for 
machines to execute.
- Abelson  Sussman, SICP
-- 
**
Andreas Koestler, Software Engineer
Leica Geosystems Pty Ltd
270 Gladstone Road, Dutton Park QLD 4102
Main: +61 7 3891 9772 Direct: +61 7 3117 8808
Fax: +61 7 3891 9336
Email: andreas.koest...@leica-geosystems.com

www.leica-geosystems.com*

when it has to be right, Leica Geosystems

Please  consider the environment before printing this email.





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


Re: Get digits of a number

2011-02-16 Thread Saul Hazledine
On Feb 17, 6:29 am, Andreas Kostler andreas.koestler.le...@gmail.com
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 careless and stupid - this time with
numbers:

(defn explode-to-digits [number]
   (map #(Character/digit % 10) (str number)))

Saul

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


Re: Get digits of a number

2011-02-16 Thread Shantanu Kumar


On Feb 17, 11:09 am, Ken Wesson kwess...@gmail.com wrote:
 On Thu, Feb 17, 2011 at 12:29 AM, Andreas Kostler

 andreas.koestler.le...@gmail.com 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 clunky...

 How about

 (defn explode-to-digits [n]
   (if (= n 0)
     []
     (let [d (rem n 10)
           r (quot n 10)]
       (conj (explode-to-digits r) d

 or if you want to avoid consuming stack

 (defn explode-to-digits* [n out]
   (if (= n 0)
     out
     (let [d (rem n 10)
           r (quot n 10)]
       (recur r (cons d out)

 (defn explode-to-digits [n]
   (explode-to-digits* n nil))

 It should also be possible to make a lazy sequence version, but
 returning the digits in the reverse order.

A bit shorter:

(defn digits [n]
  (let [r (rem n 10)
q (int (/ n 10))
_ (println (format q=%d, r=%d q r))]
(if (zero? q) [r]
  (into [r] (digits q)

Not efficient though.

Regards,
Shantanu

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


Re: Get digits of a number

2011-02-16 Thread Shantanu Kumar


On Feb 17, 11:39 am, Shantanu Kumar kumar.shant...@gmail.com wrote:
 On Feb 17, 11:09 am, Ken Wesson kwess...@gmail.com wrote:









  On Thu, Feb 17, 2011 at 12:29 AM, Andreas Kostler

  andreas.koestler.le...@gmail.com 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 clunky...

  How about

  (defn explode-to-digits [n]
    (if (= n 0)
      []
      (let [d (rem n 10)
            r (quot n 10)]
        (conj (explode-to-digits r) d

  or if you want to avoid consuming stack

  (defn explode-to-digits* [n out]
    (if (= n 0)
      out
      (let [d (rem n 10)
            r (quot n 10)]
        (recur r (cons d out)

  (defn explode-to-digits [n]
    (explode-to-digits* n nil))

  It should also be possible to make a lazy sequence version, but
  returning the digits in the reverse order.

 A bit shorter:

 (defn digits [n]
   (let [r (rem n 10)
         q (int (/ n 10))
         _ (println (format q=%d, r=%d q r))]
     (if (zero? q) [r]
       (into [r] (digits q)

 Not efficient though.

Oops, the earlier version that uses conj is shorter indeed.

Cheers,
Shantanu

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


Re: Get digits of a number

2011-02-16 Thread Ken Wesson
On Thu, Feb 17, 2011 at 12:29 AM, Andreas Kostler
andreas.koestler.le...@gmail.com 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 clunky...

How about

(defn explode-to-digits [n]
  (if (= n 0)
[]
(let [d (rem n 10)
  r (quot n 10)]
  (conj (explode-to-digits r) d

or if you want to avoid consuming stack

(defn explode-to-digits* [n out]
  (if (= n 0)
out
(let [d (rem n 10)
  r (quot n 10)]
  (recur r (cons d out)

(defn explode-to-digits [n]
  (explode-to-digits* n nil))

It should also be possible to make a lazy sequence version, but
returning the digits in the reverse order.

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