Quantcast

Beginning Scala book problem.

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Beginning Scala book problem.

Amador Antonio Cuenca
Hi guys,

I've start reading the Beginning scala by David Pollak and I've noticed that there is a mistake in a example:

import scala.io._

object Sum {
  def main(args: Array[String]): Unit = {
    println("Enter some numbers and press ctrl-D (Unix/Mac) ctrl-C (Windows)")
    val input = Source.fromInputStream(System.in)
    val lines = input.getLines.collect
    println("Sum " + sum(lines))
  }

  def toInt(s: String): Option[Int] = {
    try {
      Some(Integer.parseInt(s))
    } catch {
        case e: NumberFormatException => None
    }
  }

  def sum(in: Seq[String]): Int = {
    val ints = in.flatMap(toInt(_))
    ints.foldLeft(0)((a, b) => a + b)
  }

}

The following error appear:

"Missing arguments for method collect in trait Iterator; follow this method with ´_' if you want to treat it as a partially applied
function"


By the way, I've another doubt, can I use this code:
ints reduceLeft((a, b) => a + b)
instead this one:
ints.foldLeft(0)((a, b) => a + b)
And if I can, what is the difference between them?

PD: I know that I disturb you guys a lot, but I'm very interested in learn scala and I'll appreciate your help.

Regards,
--
TSU. Amador Cuenca
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Beginning Scala book problem.

Alex Boisvert-2
On Thu, Oct 7, 2010 at 7:14 AM, Amador Antonio Cuenca <[hidden email]> wrote:
Hi guys,

I've start reading the Beginning scala by David Pollak and I've noticed that there is a mistake in a example:

import scala.io._

object Sum {
  def main(args: Array[String]): Unit = {
    println("Enter some numbers and press ctrl-D (Unix/Mac) ctrl-C (Windows)")
    val input = Source.fromInputStream(System.in)
    val lines = input.getLines.collect
    println("Sum " + sum(lines))
  }

This is caused by a non-backward-compatible change in the Scala collections between 2.7.7 and 2.8.

The Iterator.collect() method in 2.7.7 returns a Seq.  In 2.8, it is used to perform a conditional map using a PartialFunction.

You can use input.getLines.toSeq instead.


By the way, I've another doubt, can I use this code:
ints reduceLeft((a, b) => a + b)
instead this one:
ints.foldLeft(0)((a, b) => a + b)
And if I can, what is the difference between them?

They are equivalent for all cases except when the collection is empty.   If the collection is empty, the reduceLeft() method will fail whereas the foldLeft method will return 0, as expected.

alex

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Beginning Scala book problem.

Philippe Lhoste
In reply to this post by Amador Antonio Cuenca
On 07/10/2010 16:14, Amador Antonio Cuenca wrote:
> I've start reading the Beginning scala by David Pollak and I've noticed that there is a
> mistake in a example:

Hello.
I am a beginner too, so I can't tell for sure in the particular case you expose, but don't
forget that this book (and lot of material/tutorials on the Web) pre-date Scala 2.8 which
made deep incompatible changes, particularly on the collection API.
So there is probably no "mistake" in the book (not impossible, though, see the errata
list!) but perhaps some code to adapt to the new API.

Actually, if I paste input.getLines.collect in Google, I find some pages addressing the
issue, like:
http://apress.com/book/errata/1183 -- The book was not written for RC3... it was written
for Scala 2.7.4

and a blog entry giving a solution:
http://ilopez.com/post/858172040/adventures-in-scala-sum-java -- What you need to do is
add a .toSeq at the end for val lines = input.getLines.toSeq

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Beginning Scala book problem.

Amador Antonio Cuenca
Well, now ?ve another problem. The program's result is always 0.
I look up at the code and I noticed that:

Example input: 1, 2, 3

val input = Source fromInputStream(System.in)
val lines = input.getLines.toSeq
println(lines) //show: Stream(1, 2, 3, ?)

But this part:

def sum(in: Seq[String]): Int = {
    val ints = in.flatMap(s => toInt(s))
    ints.foldLeft(0)((a, b) => a + b)
}

Never works,

If I run it individually each line:

val ints = lines.flatMap(s => toInt(s)) //If I try to do something with that sequence doesn't work! for example, I tried to do this: val p: List[String] = lines.toList...
println(ints)
val res = ints.foldLeft(0)((a, b) => a + b)
println(res)


I think that flatMap has a non-backward-compatible change,

PD: You know a book based on Scala 2.8???

Regards,

--
TSU. Amador Cuenca
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Beginning Scala book problem.

Naftoli Gugenheim
I don't know, it works for me...

scala> Stream("1", "2", "3").flatMap{s => try {Some(Integer.parseInt(s))} catch { case e: NumberFormatException=>None} } .foldLeft(0)(_ + _)
res1: Int = 6


On Thu, Oct 7, 2010 at 12:11 PM, Amador Antonio Cuenca <[hidden email]> wrote:
Well, now ?ve another problem. The program's result is always 0.
I look up at the code and I noticed that:

Example input: 1, 2, 3


val input = Source fromInputStream(System.in)
val lines = input.getLines.toSeq
println(lines) //show: Stream(1, 2, 3, ?)

But this part:


def sum(in: Seq[String]): Int = {
    val ints = in.flatMap(s => toInt(s))

    ints.foldLeft(0)((a, b) => a + b)
}

Never works,

If I run it individually each line:

val ints = lines.flatMap(s => toInt(s)) //If I try to do something with that sequence doesn't work! for example, I tried to do this: val p: List[String] = lines.toList...
println(ints)
val res = ints.foldLeft(0)((a, b) => a + b)
println(res)


I think that flatMap has a non-backward-compatible change,

PD: You know a book based on Scala 2.8???

Regards,

--
TSU. Amador Cuenca

dpp
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Beginning Scala book problem.

dpp
In reply to this post by Amador Antonio Cuenca
Amador,

You're welcome to ask questions about Beginning Scala on this list or the Lift mailing list.

The code was written against Scala 2.7.3 and 2.8 introduces some breaking changes.

Here's an update to the code that works under Scala 2.8.0:

import scala.io._

object Sum {
  def main(args: Array[String]): Unit = {
    println("Enter some numbers and press ctrl-D (Unix/Mac) ctrl-C (Windows)")
    val input = Source.fromInputStream(System.in)
    val lines = input.getLines.toList
    println("Sum " + sum(lines))
  }

  def toInt(s: String): Option[Int] = {
    try {
      Some(Integer.parseInt(s))
    } catch {
        case e: NumberFormatException => None
    }
  }

  def sum(in: Seq[String]): Int = {
    val ints = in.flatMap(toInt(_))
    ints.foldLeft(0)((a, b) => a + b)
  }

}


Note that I changed "getLines.collect" to "getLines.toList"  The code runs fine on my Linux machine... I did not test it on Windows.

I hope this helps and enjoy your path through Scala.

Thanks,

David

On Thu, Oct 7, 2010 at 3:14 PM, Amador Antonio Cuenca <[hidden email]> wrote:
Hi guys,

I've start reading the Beginning scala by David Pollak and I've noticed that there is a mistake in a example:

import scala.io._

object Sum {
  def main(args: Array[String]): Unit = {
    println("Enter some numbers and press ctrl-D (Unix/Mac) ctrl-C (Windows)")
    val input = Source.fromInputStream(System.in)
    val lines = input.getLines.collect
    println("Sum " + sum(lines))
  }

  def toInt(s: String): Option[Int] = {
    try {
      Some(Integer.parseInt(s))
    } catch {
        case e: NumberFormatException => None
    }
  }

  def sum(in: Seq[String]): Int = {
    val ints = in.flatMap(toInt(_))
    ints.foldLeft(0)((a, b) => a + b)
  }

}

The following error appear:

"Missing arguments for method collect in trait Iterator; follow this method with ´_' if you want to treat it as a partially applied
function"


By the way, I've another doubt, can I use this code:
ints reduceLeft((a, b) => a + b)
instead this one:
ints.foldLeft(0)((a, b) => a + b)
And if I can, what is the difference between them?

PD: I know that I disturb you guys a lot, but I'm very interested in learn scala and I'll appreciate your help.

Regards,
--
TSU. Amador Cuenca



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Blog: http://goodstuff.im
Surf the harmonics
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Beginning Scala book problem.

Amador Antonio Cuenca
Thanks you a lot, It work on both, windows and linux. You're doing an exelent work with this mailing list helping newbies. xD
 
Regards,
--
TSU. Amador Cuenca
Loading...