Get objects out of scala interpreter - scala

I spent hours investigating one topic. I am definitely out of my depth here. What I want is to run the scala interpreter programmatically and be able to extract object values from the interpreter. for example, if I send
val a = 1
val b = a + 1
I want to be able to read out b as an Int, not just a string printed out like
b = 2
The source code is dense. So far I don't see any part which would allow such an extraction. Any experts here who can give me a tip or tell me this is utter nonsense?
How do I get typed objects out of the scala interpreter between sessions?

Use JSR 223.
Welcome to Scala version 2.11.7 [...]
scala> import javax.script._
import javax.script._
scala> val engine = (new ScriptEngineManager).getEngineByName("scala")
engine: javax.script.ScriptEngine = scala.tools.nsc.interpreter.IMain#4233e892
scala> engine.eval("val a = 1")
res0: Object = 1
scala> engine.eval("val b = a + 1")
res1: Object = 2
scala> engine.eval("b").asInstanceOf[Int]
res2: Int = 2

Related

Why Scala in REPL (or worksheet) and Scala in IDE don't work the same? [duplicate]

as expected reassignment is giving error like below in REPL
scala> val a=1
a: Int = 1
scala> a=2
<console>:12: error: reassignment to val
a=2
^
But the below reassignment is not giving error in REPL when a=2 preceded with val.
scala> val a=1
a: Int = 1
scala> val a=2
a: Int = 2
When I execute the below code in Intellij its giving error.
object Test {
def main(args: Array[String]) {
val x = 1
val x = 2
}
}
Why val a=1 and val a=2 are not giving any error in REPL(error if it is only a=2) but error in Intellij.
From Scala docs REPL overview:
every line of input is compiled separately.
dependencies on previous lines are included by automatically generated imports.
Combining these two facts, we can understand that they are not in the same namespace, unlike the example you provided which 2 variables called x are in the same class.
The REPL is intended for rapid friction-less experimentation. It would be very annoying if you had to restart from scratch just because you accidentally mistyped val a = 32 when you meant val a = 23.
Therefore, the REPL is designed in such a way that it gives the appearance of breaking the rules of Scala, although it actually doesn't. The code that gets actually compiled corresponding to the code you entered looks a little bit like this:
object line$1 {
val a=1
}
object line$2 {
import line$1._
val a=2
}

val behavior in scala REPL and Intellij

as expected reassignment is giving error like below in REPL
scala> val a=1
a: Int = 1
scala> a=2
<console>:12: error: reassignment to val
a=2
^
But the below reassignment is not giving error in REPL when a=2 preceded with val.
scala> val a=1
a: Int = 1
scala> val a=2
a: Int = 2
When I execute the below code in Intellij its giving error.
object Test {
def main(args: Array[String]) {
val x = 1
val x = 2
}
}
Why val a=1 and val a=2 are not giving any error in REPL(error if it is only a=2) but error in Intellij.
From Scala docs REPL overview:
every line of input is compiled separately.
dependencies on previous lines are included by automatically generated imports.
Combining these two facts, we can understand that they are not in the same namespace, unlike the example you provided which 2 variables called x are in the same class.
The REPL is intended for rapid friction-less experimentation. It would be very annoying if you had to restart from scratch just because you accidentally mistyped val a = 32 when you meant val a = 23.
Therefore, the REPL is designed in such a way that it gives the appearance of breaking the rules of Scala, although it actually doesn't. The code that gets actually compiled corresponding to the code you entered looks a little bit like this:
object line$1 {
val a=1
}
object line$2 {
import line$1._
val a=2
}

Is it instance name/id that scala REPL prints?

Tutorial mentions about mutable sets in the initial but why would the REPL change the instance name from res4 to res5 when a new element is added? Is 'res' not the instance name that REPL prints? Below is the code in context. Beginner in scala. Please bear if the question is trivial.
scala> val set = scala.collection.mutable.Set[Int]()
val set: scala.collection.mutable.Set[Int] = Set()
scala> set += 1
val res0: scala.collection.mutable.Set[Int] = Set(1)
scala> set += 2 += 3
val res1: scala.collection.mutable.Set[Int] = Set(1, 2, 3)
The reference did not change though, it means res0 == res1. Scala repl will generate names for expressions that are not assigned any name, no matter if it's mutable or not.
Additionally take a look at the docs. For mutable.Set, the method += results in Set.this.type. Since there is a value returned, it has to be assigned some name.

Making a folder/files and writing to them in scala? [duplicate]

This question already has answers here:
How to write to a file in Scala?
(19 answers)
Closed 9 years ago.
How can I make files and folders in scala and write to them? I understand how it works in other languages, but Scala is a bit trickier :/
This is different than the other question because it also asks how to make directories and files in Scala.
Paul Phillips just mentioned parenthetically on ML, "I wrote a library which does a bunch of wrapping of java.nio.file.Path", so you should wait to see what he came up with.
In the meantime, his previous work on java.io has wandered like a restless spirit but now haunts:
scala> import reflect.io._
import reflect.io._
scala> val f = File("my-test.txt")
f: scala.reflect.io.File = my-test.txt
scala> f writeAll ("hello", ",", " ", "world", "\n")
scala> :q
apm#mara:~/tmp$ cat my-test.txt
hello, world
There are also abstractions for Path and Directory.
scala> val d = Directory(".")
d: scala.reflect.io.Directory = .
scala> d.list
res0: Iterator[scala.reflect.io.Path] = non-empty iterator
scala> d.deepList()
res2: Iterator[scala.reflect.io.Path] = non-empty iterator
scala> d.deepFiles.toList
res4: List[scala.reflect.io.File] = List(./mkarray.scala, ./for29.scala, ./nofunc-wrapped.scala, ./inlined.scala, ./xmlex.scala, ./pet.scala, ./lookup.scala, ./CompilerTool.scala, ./badvargs.scala, ./tz0.scala, ./discontinuations.jar, ./serious.scala, ./badjunk.scala, ./version.scala, ./shapeless_2.11.jar, ./callbacks.scala, ./delayed.scala, ./privctor0.scala, ./nestedtags.scala, ./hw-repl.scala, ./schema-one.txt, ./tstest.scala, ./badimp.scala, ./matchprim.scala, ./sxemata-all.txt, ./arrow.scala, ./continuations2.jar, ./inliner.scala, ./oneq.scala, ./impmag.scala, ./enumuser.scala, ./auto.save, ./xmlregex.scala, ./strtyp.scala, ./orelse.scala, ./scalac-plugin.xml, ./futuremap.scala, ./applied.save, ./looker.scala, ./pat1, ./sounds.scala, ./shapeless_2.10.jar, ./funkstr.scala, ./stupid....
scala> val p = Path(".")
p: scala.reflect.io.Path = .
scala> val f = p / "mkarray.scala"
f: scala.reflect.io.Path = ./mkarray.scala

Scala convert Option to an Int

I have looked at these links
http://blog.danielwellman.com/2008/03/using-scalas-op.html
http://blog.tmorris.net/scalaoption-cheat-sheet/
I have a map of [String, Integer] and when I do a map.get("X") I get an option. I would like the following.
val Int count = map.get(key);
// If the key is there I would like value if it is not I want 0
How do I achieve this in one line? I need to do this several times. It looks a bit inefficient to write a function everytime for doing this. I am sure there is some intelligent one line quirk that I am missing but I really like to get the value into an integer in ONE line :)
Just use getOrElse method:
val count: Int = map.getOrElse(key,0);
Note also, that in Scala you write type after name, not before.
#om-nom-nom (classic screen name) has the correct answer, but in the interest of providing yet another way™
val count = map.get(key) fold(0)(num => num)
Before in-the-know users bash me with, "Option has no fold!", fold has been added to Option in Scala 2.10
getOrElse is of course better in the current case, but in some Some/None scenarios it may be interesting to 1-liner with fold like so (edited complements of #Debiliski who tested against latest 2.10 snapshot):
val count = map.get(k).fold(0)(dao.userlog.count(_))
I suppose in 2.9.2 and under we can already do:
val count = map get(k) map ( dao.userlog.count(_) ) getOrElse(0)
Which is to say, in Scala there is often more than one way to do the same thing: in the linked thread, the OP shows more than 10 alternative means to achieve Option fold ;-)
Yet another way.
import scalaz._, Scalaz._
scala> val m = Map(9 -> 33)
m: scala.collection.immutable.Map[Int,Int] = Map(9 -> 33)
scala> m.get(9).orZero
res3: Int = 33
scala> m.get(8).orZero
res4: Int = 0