--
Cecil Westerhof
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
2013/1/29 Alec Zorab <alec...@gmail.com>:
> val source = io.Source.fromFile(k)
> val linesIterator = source.getLines
> //... do some stuff...
> source.close()
Two strange things happen:
- Lines are read in parts instead of complete (but not always)
- At a certain moment the program crashes with:
java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:277)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:337)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:176)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:153)
at java.io.BufferedReader.readLine(BufferedReader.java:316)
at java.io.BufferedReader.readLine(BufferedReader.java:379)
at scala.io.BufferedSource$BufferedLineIterator.hasNext(BufferedSource.scala:67)
at scala.collection.Iterator$class.foreach(Iterator.scala:772)
at scala.io.BufferedSource$BufferedLineIterator.foreach(BufferedSource.scala:43)
at HondenBelasting$.processFile(hondenbelasting.scala:201)
My code is:
val source = io.Source.fromFile(filename)
val linesIterator = source.getLines
for (line <- linesIterator) {
--
Cecil Westerhof
Forwarding to list.
is Source.fromFile still a valid function to use ?I can't see fromFile function in the follwoing URL
moreover getting following error while trying:Welcome to Scala version 2.7.7.final (Java HotSpot(TM) Client VM, Java 1.6.0_22).Type in expressions to have them evaluated.Type :help for more information.scala> for(lines <- Source.fromFile("test.txt", "UTF8").getLines()){| println(line)| }<console>:5: error: not found: value Sourcefor(lines <- Source.fromFile("test.txt", "UTF8").getLines()){^
scala>
On Wednesday, January 30, 2013 9:33:37 AM UTC+11, AlecZorab wrote:
You want to upgrade to the version indicated here and use Source.fromPath:
> Welcome to Scala version 2.7.7.final
http://stackoverflow.com/a/17643854
I’m reading Odersky, et al. Programming in Scala, Second Edition, and in section 7.4 (page 124) he gives the example:
def scalaFiles =
for {
file <- filesHere
if file.getName.endsWith(".scala")
} yield file
He states that the syntax of a for-yield expression is:
for clauses yield body
Where the yield “goes before the entire body.”
I have two questions. First, it seems like here the “generator” or what he calls “clauses” for the for loop is in the braces rather than in the “normal” parens. Second, it seems there is no “body” in this example.
He then gives a second example:
val forLineLengths =
for {
file <- filesHere
if file.getName.endsWith(".scala")
line <- fileLines(file)
trimmed = line.trim
if trimmed.matches(".*for.*")
} yield trimmed.length
Again, the nested iteration “generator” in the braces and there is no body.
Can one have a syntax like:
for (generator) yield x { body where we do some work and calculate x}
Thanks,
Mike Shields
I’m reading Odersky, et al. Programming in Scala, Second Edition, and in section 7.4 (page 124) he gives the example:
def scalaFiles =
for {
file
if file.getName.endsWith(".scala")
} yield file
He states that the syntax of a for-yield expression is:
for clauses yield body
Where the yield “goes before the entire body.”
I have two questions. First, it seems like here the “generator” or what he calls “clauses” for the for loop is in the braces rather than in the “normal” parens. Second, it seems there is no “body” in this example.
He then gives a second example:
val forLineLengths =
for {
file
if file.getName.endsWith(".scala")
line
trimmed = line.trim
if trimmed.matches(".*for.*")
} yield trimmed.length
Again, the nested iteration “generator” in the braces and there is no body.
Can one have a syntax like:
for (generator) yield x { body where we do some work and calculate x}
Thanks,
Mike Shields
Eduardo,
For #1, I think you can use parens and use separate lines without commas:
val strList = List("one", "to", "two", "three", "five", "seven")
for (str <- strList
if str.endsWith("e")
) yield str.length
Seems to work. So I’m still not sure when to use () and when to use {}.
For #2, I’m coming to Scala from C/C++, python, perl, etc., but not java so I’m not sure what a block is java is, but if I understand you, I can put a single expression or a block in {} with the yielded value the last expression.
I’m sure your third point is not correct:
scala> for (i<-1 to 10) yield i + 0.1f
res0: scala.collection.immutable.IndexedSeq[Float] = Vector(1.1, 2.1, 3.1, 4.1,5.1, 6.1, 7.1, 8.1, 9.1, 10.1)
What I really want to figure out how to do is something like:
val strList = List("one", "to", "two", "three", "five", "seven")
for (str <- strList
if str.endsWith("e")
) yield x { val x = str.length -1}
But this gives and error as “x” is undefined for the yield, and I want to do calculations to figure out what to yield in each loop. I know how to do this is C in an imperative style: Define a collection before the loop and fill in with the loop, one member at a time. I’m trying to understand the Scala way to do the same thing in a functional style. I can’t figure out how to use yield with the body of the for loop where the calculations may be not trivial.
Every example with yield that I have seen has nothing being done in the body (after the yield expression).
Thanks,
Mike
Eduardo,
For #1, I think you can use parens and use separate lines without commas:
val strList = List("one", "to", "two", "three", "five", "seven")
for (str
if str.endsWith("e")
) yield str.length
Seems to work. So I’m still not sure when to use () and when to use {}.
For #2, I’m coming to Scala from C/C++, python, perl, etc., but not java so I’m not sure what a block is java is, but if I understand you, I can put a single expression or a block in {} with the yielded value the last expression.
I’m sure your third point is not correct:
scala> for (i
res0: scala.collection.immutable.IndexedSeq[Float] = Vector(1.1, 2.1, 3.1, 4.1,5.1, 6.1, 7.1, 8.1, 9.1, 10.1)
What I really want to figure out how to do is something like:
val strList = List("one", "to", "two", "three", "five", "seven")
for (str
if str.endsWith("e")
) yield x { val x = str.length -1}
But this gives and error as “x” is undefined for the yield, and I want to do calculations to figure out what to yield in each loop. I know how to do this is C in an imperative style: Define a collection before the loop and fill in with the loop, one member at a time. I’m trying to understand the Scala way to do the same thing in a functional style. I can’t figure out how to use yield with the body of the for loop where the calculations may be not trivial.
Every example with yield that I have seen has nothing being done in the body (after the yield expression).
Thanks,
Mike
From: eduardo.m.cavalcanti [mailto:eduardo.m....@uol.com.br]
Sent: Thursday, July 25, 2013 7:31 PM
To: Michael Shields
Cc: scala...@googlegroups.com
Subject: Re: [scala-user] Newbie question about for..yield
1- If you use parens, separate the expressions with commas, i.e.
for (file; if file.getName.endsWith(".scala"); ... ; ...)
2- For the yield block, follow same rule as if block in java: just one expression, no need for braces. yielded value is the last expression.
3 - You did not asked but: the yielded result type is the same as the type of the expression to the right of the
Someone please correct if I am wrong or unprecise.
Eduardo,
For #1, I think you can use parens and use separate lines without commas:
val strList = List("one", "to", "two", "three", "five", "seven")
for (str
if str.endsWith("e")
) yield str.length
Seems to work. So I’m still not sure when to use () and when to use {}.
For #2, I’m coming to Scala from C/C++, python, perl, etc., but not java so I’m not sure what a block is java is, but if I understand you, I can put a single expression or a block in {} with the yielded value the last expression.
I’m sure your third point is not correct:
scala> for (i
res0: scala.collection.immutable.IndexedSeq[Float] = Vector(1.1, 2.1, 3.1, 4.1,5.1, 6.1, 7.1, 8.1, 9.1, 10.1)
What I really want to figure out how to do is something like:
val strList = List("one", "to", "two", "three", "five", "seven")
for (str
if str.endsWith("e")
) yield x { val x = str.length -1}
But this gives and error as “x” is undefined for the yield, and I want to do calculations to figure out what to yield in each loop. I know how to do this is C in an imperative style: Define a collection before the loop and fill in with the loop, one member at a time. I’m trying to understand the Scala way to do the same thing in a functional style. I can’t figure out how to use yield with the body of the for loop where the calculations may be not trivial.
Every example with yield that I have seen has nothing being done in the body (after the yield expression).
Thanks,
Mike
From: eduardo.m.cavalcanti [mailto:eduardo.m....@uol.com.br]
Sent: Thursday, July 25, 2013 7:31 PM
To: Michael Shields
Cc: scala...@googlegroups.com
Subject: Re: [scala-user] Newbie question about for..yield
1- If you use parens, separate the expressions with commas, i.e.
for (file; if file.getName.endsWith(".scala"); ... ; ...)
2- For the yield block, follow same rule as if block in java: just one expression, no need for braces. yielded value is the last expression.
3 - You did not asked but: the yielded result type is the same as the type of the expression to the right of the
Someone please correct if I am wrong or unprecise.
def scalaFiles =
for { // these are for clauses.
file <- filesHere
if file.getName.endsWith(".scala")
} yield {
file // this is the body
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.