I am able to debug scala controller in play framework using netbeans IDE but the issue is I cannot set breakpoint when I am within a loop like:
val strs: List[String] = txt.split("\\s+").toList
for (str <- strs) {
if (str == "some")
.....
}
When I try to set breakpoint at line " if (str == " netbeans tells me: Broken breakpoint: it is not possible to stop on this line.
Most of my code is within loops, so I am stuck.
"For" expression is translated to "map":
http://www.artima.com/pins1ed/for-expressions-revisited.html#23.4
Regards
Related
I have a string that I need transform to "canonical" view and for do that I need to call replaceAll() many times on string. I made it work next way:
val text = "Java Scala Fother Python JS C# Child"
val replacePatterns = List("Java", "Scala", "Python", "JS", "C#")
var replaced = text
for (pattern <- replacePatterns) {
replaced = replaced.replaceAll(pattern, "")
}
This code is result in replaced = "Fother Child" as I want, but it looks very imperative and I want eliminate accumulator "replaced".
Is there a way in Scala to handle it in one line without var's?
Thanks.
Use a fold over the list of patterns and the text to be processed as start point:
replacePatterns.foldLeft(text){case (res, pattern) => res.replaceAll(pattern, "")}
For some reason, intermediate values aren't being printed out in the REPL console (right hand side of the worksheet)
For instance, this is what I have:
object test {
val obj = new MyObject(1)
obj.value
}
class MyObject(x: Int) {
def value = x
}
In the REPL results, I only get the following:
defined module test
.
.
.
defined class MyObject
However, I don't get any of the intermediate results, such as when I evaluate x.value
I would expect something like:
> MyObject#14254345
> 1
after x.value
Any reason why this isn't printing out?
What ended up working for me in this case (and this might be particular to IntelliJ 14, since I've seen it working the other way in Eclipse) is I added the class inside the object block, like this:
object test {
val obj = new MyObject(1)
obj.value
class MyObject(x: Int) {
def value = x
}
}
This forced the REPL instance inside the worksheet to auto-evalute the result and print them out on the right hand side.
To make it work like it does in Eclipse, turn on "eclipse compatibility" mode. This worked for me using IntelliJ IDEA 2016.
Preferences > Language & Frameworks > Scala > Worksheet
Then, check the Use "eclipse compatibility" mode checkbox.
Sorry, I don't have enough reputation to comment so I have to write it here.
If you want to get the result you want, maybe you can try like this.
scala> :paste
// Entering paste mode (ctrl-D to finish)
object test {
val obj = new MyObject(1)
println(obj.value)
}
class MyObject(x: Int) {
def value = x
}
// Exiting paste mode, now interpreting.
defined object test
defined class MyObject
scala> test.obj
1
res4: MyObject = MyObject#1cd072a9
when you paste the code, test and MyObject are not initialized, certainly you can not get any print.
test.obj will cause test to be initialized so will obj, meantime, obj.value also get evaluated. However, if you don't something about it(like print), it's just a pure expression.
The screenshot of the following tiny scala script shows four breakpoints set.
object WC {
val code = """import org.apache.hadoop.conf.Configuration
// This class performs the map operation, translating raw input into the key-value
class TokenizerMapper extends Mapper[Object,Text,Text,IntWritable] {
"""
def sloc(str: String): Int = {
val x: List[String] = str.split("\\n").map(_.trim).toList.filter { line =>
line.length!=0 && !line.startsWith("import ") && !line.startsWith("/*") && !line.startsWith("//") && !line.equals("{") && !line.equals("}") }
println(x.mkString("\n"))
x.size
}
}
println (WC.sloc(WC.code))
Here are the peculiarities:
only the very last breakpoint - on the "println (WC.sloc(WC.code))" - is actually respected. The others are ignored
However, I can step through the other three lines in the debugger
I am wondering if others have discovered any "pattern" to how to get the scala debugger to respect the breakpoints. OK we know that the scala plugin is buggy - it is here a matter of trying to get the "most" out of what we have.
I am using the latest IJ ULtimate 12.1.6.
In order for the debugger to work properly IJ unfortunately needs a CLASS to be available - not just the Object. This may have been fixed in 13.
I am reading Martin Odersky's Programming in Scala, and I have been using vi and the command line to compile so far. I want to learn to use Eclipse and the Scala-IDE plug-in, but I lack a basic understanding of compiling and executing multiple source code files in Eclipse.
My IDE is as follows:
Ubuntu 12.04
Eclipse 3.7.2
Scala Plugin 2.0.1
Scala Library 2.9.2
I am using the Checksum example in Chapter 4 for practice. The source code is as follows:
Listings 4.1 & 4.2 / ChecksumAccumulator.scala:
class ChecksumAccumulator {
private var sum = 0
def add(b: Byte) { sum += b }
def checksum(): Int = ~(sum & 0xFF) + 1
}
import scala.collection.mutable.Map
object ChecksumAccumulator {
private val cache = Map[String, Int]()
def calculate(s: String): Int =
if (cache.contains(s))
cache(s)
else {
val acc = new ChecksumAccumulator
for (c <- s)
acc.add(c.toByte)
val cs = acc.checksum()
cache += (s -> cs)
cs
}
}
Listing 4.3 / Summer.scala:
import ChecksumAccumulator.calculate
object Summer {
def main(args: Array[String]) {
for (arg <- args)
println(arg + ": " + calculate(arg))
}
}
I can compile these classes using scalac Summer.scala ChecksumAccumulator.scala at the command line. I can then execute the object code with the command line scala Summer of love, which returns the checksum results for "of" and "love" (-213 and -182, respectively).
How would I build the object code using Eclipse and not the command line, and how would I call the Summer object code through Eclipse?
In Eclipse, you can just create a new Scala project, and put the source files there.
Once the files are part of the same project, there's no need to do anything special:
Just click on Run >> Run As >> Scala Application (or press Alt + Shift + X, S)
Have fun learning Scala :)
Working through a sample in Chapter 3 of "Programming in Scala", the following code doesn't seem working on Scala 2.8:
import scala.io.Source
if (args.length > 0) {
for (line <- Source.fromFile(args(0)).getLines)
print(line.length + " " + line)
}
else
Console.err.println("Filename required.")
Scala complains fromFile is expecting type java.io.File. With a bit of searching it seems that I should be using fromPath instead...
for (line <- Source.fromPath(args(0)).getLines)
However, I now get a puzzling error from that (puzzling to a beginner anyways):
... :4: error: missing arguments for method getLines in class Source;
follow this method with `_' if you want to treat it as a partially applied function
Error occurred in an application involving default arguments.
for (line <- Source.fromPath(args(0)).getLines)
^
one error found
I took a guess at trying...
for (line <- Source.fromPath(args(0)).getLines _)
And that didn't work. What's the Scala 2.8 way of making getLines work?
The signature of getLines is this:
def getLines(separator: String = compat.Platform.EOL): Iterator[String] = new LineIterator(separator)
So it has a default argument. You need to write getLines() instead, so that this default will be used.