How to fix this code? Enable the java parseDouble in scalafx program - scala

I am a new learner of scala and I am coding a simple scalafx calculator and now I face a problem. I cannot find another suitable code to replace the parseDouble. I know the parseDouble is the member of java double, not scala double. I just put the code here to express my thoughts. Can someone help me to fix the code?

You can invoke the Java parser directly...
val n1 = java.lang.Double.parseDouble(display.getText())
...or you can use the Scala .toDouble.
val n1 = display.getText().toDouble

Related

Meaning of symbol :+: in Scala?

What does the symbol :+: mean in Scala Programming language?
object Devices {
private[endpoints] def endpoints(myService: MyService[Future], pService: PService[Future]) =
"devices" / (
banDevice(myService, pService) :+:
unbanDevice(myService, pService)
)
This is the code snippet. I'm using finagle library.
I've read that scala can have function names as symbols. Is this an example of that?
Where do I find the defention of this finagle defined methods?
Is it possible to override these methods?
Scala Does not have this function , it can be inside one of the libraries that you are using. According to your code , you might be using finagle !
Hence What Peter Neyens pointed out : https://github.com/finagle/finch/blob/master/docs/endpoint.md#coproduct-endpoints.
Seems to explain what you are trying to find out.
P.s: Its just a method in one of the libraries and nothing else.
In answer to your updated questions:
Yes, that code is calling the function (actually a method) named :+:.
You can probably find the API published online (maybe here).
If the class is not sealed and the method is not final then, yes, you should be able to override it.

List Processing Scala

I am in the process of self teaching myself scala.
I know java but I'm having a little problem implementing certain programs with scala.
I am reading a book specifically for learning scala and I'm trying to attempt one of the exercises for expanding my knowledge.
How would you implement this in scala?
I do have some idea on how to start it using Java.
Your help is appreciated.
I think you need something like this:
evalMono(mono: (Double, Double), x:Double) = mono._1 * Math.pow(x, mono._2)
evalPoly(poly: List[(Double, Double), x:Double) = poly.map (item => evalMono(item, x)).sum

How can I generate my own ScalaSig?

I've dynamically defined a Scala class, but in order to use it "properly" it needs to have a ScalaSig.
So, how might I generate a ScalaSig outside of normal compilation? Perhaps from a tree? Maybe like:
val tb = runtimeMirror(getClass.getClassLoader).mkToolBox()
val classDef = """class MyRecord(x: String)"""
val tree = showRaw(tb.parse(classDef))
But where does the pickler come in?
Thanks for any advice
-Julian
Artisanal-Pickle-Maker will reproduce a Scala pickled signature byte-for-byte (see restrictions).
Tapping into the compiler's pickler phase, as well as reuse of the Pickler's code, proved too challenging, so instead I used PickleBuffer, ShowPickled and a whole lotta diff -y to figure out how to generate arbitrary pickled Scala sigs.

Scala's compiler lexer

I'm trying to get a list of tokens (I'm most interested in keywords) and their positions for a given scala source file.
I think there is a lexer utility inside scala compiler, but I can't find it. Can you point me into the right direction?
A simple lexer for a Scala-like language is provided in a standard library.
A small utility program which tokenizes Scala source using the same lexer as compiler does lives here
Scalariform has an accurate Scala lexer you can use:
import scalariform.lexer._
val tokens = ScalaLexer.rawTokenise("class A", forgiveErrors = true)
val keywords = tokens.find(_.tokenType.isKeyword)
val comments = tokens.find(_.tokenType.isComment)
Parser Combinators might help in what you are trying to achieve here, especially if you later on are not only interessted in keyword parsing.

Mixing Java code in Scala program?

Is this allowed in Scala code:
DomNode node = node.getFirstChild()
where DomNode is Java type from external java library and getFirstChild() is defined on DomNode type.
I am porting existing java program to scala and it would be very convenient if I leave original java declerations as is to minimize porting efforts.
You can use Java classes in a Scala program, but you would ofcourse have to use Scala syntax:
val node: DomNode = node.getFirstChild()
You cannot use Java syntax in the form Type variableName.
edit (thanks to ericacm) - You can also just specify
val node = node.getFirstChild()
so you don't have to specify the type of node explicitly; you can let Scala infer the type.
IntelliJ IDEA can translate from Java to Scala for you. If you paste Java code into a ".scala" file IntelliJ IDEA notices it and asks you if you would like to try an automatic conversion. You might wanna check it out.
PS
I never tried it out myself...