- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
List[Int] to Int value
Sat, 2012-02-11, 00:59
Hello all,
Newbie at Scala.
Need to convert a List of integers to its value, that's it:
List(1,8,3).toInt = 183
Obviously toInt function does not exists.
I am trying to acomplish it using reduceLeft, but i miss the index. it will be (or will not):
List(1,8,3).reduceLeft((a,b) => a*10^_missing_a_index_ + b*10^_missing_b_index) // ^ means pow
I' ve tried with Seqs, but that's too much for me at this point of the learning curve.
Any hint, better/proper way?
Thanks a lot
Newbie at Scala.
Need to convert a List of integers to its value, that's it:
List(1,8,3).toInt = 183
Obviously toInt function does not exists.
I am trying to acomplish it using reduceLeft, but i miss the index. it will be (or will not):
List(1,8,3).reduceLeft((a,b) => a*10^_missing_a_index_ + b*10^_missing_b_index) // ^ means pow
I' ve tried with Seqs, but that's too much for me at this point of the learning curve.
Any hint, better/proper way?
Thanks a lot
Sat, 2012-02-11, 01:11
#2
Re: List[Int] to Int value
Well, perhaps this is not how you want to do it, but I think it's quite clear.
scala> List(1,8,3).mkString.toInt
res1: Int = 183
On Fri, Feb 10, 2012 at 21:59, csg wrote:
> Hello all,
>
> Newbie at Scala.
>
> Need to convert a List of integers to its value, that's it:
>
> List(1,8,3).toInt = 183
>
> Obviously toInt function does not exists.
>
> I am trying to acomplish it using reduceLeft, but i miss the index. it will
> be (or will not):
>
> List(1,8,3).reduceLeft((a,b) => a*10^_missing_a_index_ +
> b*10^_missing_b_index) // ^ means pow
>
> I' ve tried with Seqs, but that's too much for me at this point of the
> learning curve.
>
> Any hint, better/proper way?
>
> Thanks a lot
Sat, 2012-02-11, 01:21
#3
Re: List[Int] to Int value
Reduce and fold(Left) pass the previous result in as the 1st arg. So, a hint? a*10+b.
On 2012-02-10 6:59 PM, "csg" <carlossg00@gmail.com> wrote:Hello all,
Newbie at Scala.
Need to convert a List of integers to its value, that's it:
List(1,8,3).toInt = 183
Obviously toInt function does not exists.
I am trying to acomplish it using reduceLeft, but i miss the index. it will be (or will not):
List(1,8,3).reduceLeft((a,b) => a*10^_missing_a_index_ + b*10^_missing_b_index) // ^ means pow
I' ve tried with Seqs, but that's too much for me at this point of the learning curve.
Any hint, better/proper way?
Thanks a lot
Sat, 2012-02-11, 06:51
#4
Re: List[Int] to Int value
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Am 11.02.2012 00:59, schrieb csg:
> Hello all,
>
> Need to convert a List of integers to its value, that's it:
>
> List(1,8,3).toInt = 183
How about:
("" /: List (1,8,3)) (_ + _).toInt
Rex' solution, translated into this condensed form:
(0 /: List (1,8,3)) (10 * _ + _)
(or not) is even better, since pure numeric.
Sat, 2012-02-11, 10:41
#5
Re: List[Int] to Int value
On 02/11/2012 06:42 AM, Stefan Wagner wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Am 11.02.2012 00:59, schrieb csg:
>> Hello all,
>>
>> Need to convert a List of integers to its value, that's it:
>>
>> List(1,8,3).toInt = 183
>
> How about:
>
> ("" /: List (1,8,3)) (_ + _).toInt
>
> Rex' solution, translated into this condensed form:
>
> (0 /: List (1,8,3)) (10 * _ + _)
>
> (or not) is even better, since pure numeric.
Not the latter only produces desired output if the numbers in the list
is between 1 and 9 while the other (String-based) copes with lists like
List(10, 20, 9)
Sat, 2012-02-11, 11:21
#6
Re: List[Int] to Int value
scala> def toInt(xs:List[Int], acc:Int) : Int = | xs match { | case Nil => acc | case x::rest if (x < 10) => toInt(rest, acc * 10 + x)
| case x::rest => toInt(x %10 :: rest, acc * 10 + (x / 10)) | }toInt: (xs: List[Int], acc: Int)Int
scala> toInt(List(2, 4, 1,0), 0) res4: Int = 2410
scala> toInt(List(21, 4, 21,0), 0)res5: Int = 214210
On Sat, Feb 11, 2012 at 10:30 AM, Andreas Joseph Krogh <andreak@officenet.no> wrote:
scala> toInt(List(2, 4, 1,0), 0) res4: Int = 2410
scala> toInt(List(21, 4, 21,0), 0)res5: Int = 214210
On Sat, Feb 11, 2012 at 10:30 AM, Andreas Joseph Krogh <andreak@officenet.no> wrote:
On 02/11/2012 06:42 AM, Stefan Wagner wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Am 11.02.2012 00:59, schrieb csg:
Hello all,
Need to convert a List of integers to its value, that's it:
List(1,8,3).toInt = 183
How about:
("" /: List (1,8,3)) (_ + _).toInt
Rex' solution, translated into this condensed form:
(0 /: List (1,8,3)) (10 * _ + _)
(or not) is even better, since pure numeric.
Not the latter only produces desired output if the numbers in the list is between 1 and 9 while the other (String-based) copes with lists like List(10, 20, 9)
List(1,8,3).fold(0){(a,b) => 10*a + b}
--Rex
On Fri, Feb 10, 2012 at 6:59 PM, csg <carlossg00@gmail.com> wrote: