I'm looking to split an Int into a List of digits, i.e 1234 becomes
List(1,2,3,4).
This seems to work but feels a bit long winded (mainly the toString
bit):
scala> 1234.toString.map(_.asDigit).toList
res3: List[Int] = List(1, 2, 3, 4)
Is there a better way to do this?
Richard
Is there a better way to do this?
Thanks
Richard
And to complete the hat trick of implementation styles:
def unfold[T1,T2](x: T1)(fn: T1 => Option[(T2, T1)]): Stream[T2] =
fn(x) match {
case None => Stream()
case Some((result, next)) => result #:: unfold(next)(fn)
}
def digits(n: Int) =
unfold(n)(n => if (n == 0) None else Some((n % 10, n / 10)))
.toList.reverse
--
Seth Tisue | Northwestern University | http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/
Nice... but this could still blow up the stack iirc.
Nice... but this could still blow up the stack iirc.
Am 21.11.2011 19:15, schrieb Josh Suereth:
> So do recursive functions:
>
> // Assumes positive integers
> def digits(n: Int): Seq[Int] = n match {
> case 0 => Vector.empty
> case _ => digits(n / 10) :+ (x % 10)
> }
>
> Note: The above does *not* trampoline, so it could blow the stack. You can
> unwind that with a helper function:
Blow the stack with an Int? Maybe with some BigInt, but not with Int or
Long, or am I missing something?
- --
Tsch���--->...Stefan
- ---------------------------
Don't visit my homepage at:
http://home.arcor-online.net/hirnstrom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAk7LOAAACgkQQeATqGpDnRpowQCeOqN+f1QPirZjoCa0dFffDWd9
si4AnAnRdpx5MJ7XSrsxGKdkDGfcSl9E
=d8mE
-----END PGP SIGNATURE-----
I think that's from the REPL not from the compiler - but I didn't grep the
sources :-)
Greetings
Bernd
Am 21.11.2011 17:01, schrieb imrichardcole:
> Hi,
>
> I'm looking to split an Int into a List of digits, i.e 1234 becomes
> List(1,2,3,4).
Here is a comparision of some algorithms, mentioned in this Thread.
- From 1=40 000 to 10=400 000 Ints converted:
0 whileDigits toStrDigits matchDigits unfoldDigits iterateDigits
carryDigits
1 0.146 0.336 0.753 0.327 0.51 0.04
2 0.201 0.614 1.424 0.547 0.894 0.068
3 0.276 0.973 2.239 0.927 1.359 0.149
4 0.347 1.509 2.781 1.335 1.802 0.118
5 0.427 1.801 3.419 1.623 2.277 0.258
6 0.48 1.563 4.221 2.132 2.834 0.23
7 0.595 1.874 5.072 2.101 3.729 0.849
8 0.718 2.903 8.226 2.936 4.641 1.144
9 0.694 4.838 9.073 4.754 5.184 0.996
10 1.173 5.911 9.161 4.223 5.651 0.772
carryDigits wasn't mentioned so far:
@tailrec
def carryDigits (n: Int, carry: List[Int]): List [Int] =
if (n < 10) n :: carry
else carryDigits (n/10, (n % 10) :: carry)
def digits6 (i: I) : O = i.map (n => carryDigits (n, List ()))
The whole benchmark source in the comments of
> https://gist.github.com/1372818#comments
Using big Ints up to MaxValue does not make much difference, compared to
Ints of size up to 9999.
- --
Tsch���--->...Stefan
- ---------------------------
Don't visit my homepage at:
http://home.arcor-online.net/hirnstrom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAk7NEh8ACgkQQeATqGpDnRqbsQCdEvglBKJispoCwmzEup/h/IIN
gWcAnAsvvziRmxuKHEpsX3YG8DWzZF/V
=2+1q
-----END PGP SIGNATURE-----