This page is no longer maintained — Please continue to the home page at www.scala-lang.org

List[Int] to Int value

6 replies
csg
Joined: 2012-02-11,
User offline. Last seen 42 years 45 weeks ago.
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
ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: List[Int] to Int value
Folds are safer with empty lists.  (Could foldLeft, but no reason to here.)

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

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
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

Tom Switzer
Joined: 2011-07-19,
User offline. Last seen 42 years 45 weeks ago.
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
Stefan Wagner
Joined: 2011-04-08,
User offline. Last seen 42 years 45 weeks ago.
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.

andreak
Joined: 2009-04-24,
User offline. Last seen 2 years 22 weeks ago.
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)

Anwar Rizal
Joined: 2011-07-05,
User offline. Last seen 42 weeks 5 days ago.
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:
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)

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland