Can double literal end with a dot? - scala

I'm migrating from Scala 2.9 to Scala 2.11.0-M5.
Following double field initialization with a constant floating point literal fails.
Code example:
class Test {
val okDouble = 0.0
val badDouble = 0.
val nextValue = 0
}
Scala interpreter error:
scala> class Test {
| val okDouble = 0.0
| val badDouble = 0.
| val nextValue = 0
<console>:4: error: identifier expected but 'val' found.
val nextValue = 0
The problem here is the dot at the end of badDouble definition.
Should 0.0 be always used for double literals now?

Double literals ending with dot were deprecated in Scala 2.10 and removed from the language in Scala 2.11:
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_45).
scala> 3.
<console>:1: warning: This lexical syntax is deprecated. From scala 2.11, a dot
will only be considered part of a number if it is immediately followed by a digit.
3.
^

Related

How to generate Scala Range for Double?

I am not able to generate Scala ranges for Double.
I have read StackOverflow and there are many snippets which show double ranges but none of that works on my Scala REPL 2.13.0
9.474 to 49.474 by 1.0
1d to 1000d by 1d
(1.0 to 2.0 by 0.01)
^
error: value to is not a member of Double
What is the reason I cannot use to and by to generate double ranges in my Scala REPL
I am on a macOS with Scala 2.13.0
With Scala 2.12 I get a deprecation warning:
scala> 9.474 to 49.474 by 1.0
<console>:12: warning: method to in trait FractionalProxy is deprecated (since 2.12.6): use BigDecimal range instead
9.474 to 49.474 by 1.0
So maybe it is not supported anymore in 2.13. According to the warning you can do:
scala> BigDecimal(9.474) to BigDecimal(49.474) by BigDecimal(1.0)
res6: scala.collection.immutable.NumericRange.Inclusive[scala.math.BigDecimal] = NumericRange 9.474 to 49.474
This also works:
BigDecimal(9.474) to BigDecimal(49.474) by 1
If you do .foreach(println) on both versions you see that without BigDecimal the result looks not so great:
9.474
10.474
..
31.474
32.474000000000004
33.474000000000004
...
From the Release Notes:
Assorted deprecated methods and classes throughout the standard library have been removed entirely.
You can always create your own function that will create Range (or Seq/Iterator) for you.
// https://scastie.scala-lang.org/ZPfpF37bRlKfUPnMyOzJDw
import scala.util.Try
def rangeInclusiveDouble(from:Double, to:Double, by:Double = 1.0) = {
assume(by != 0, "'by' cannot by 0")
assume((to - from) * by > 0, "range has reversed order (arguments 'from', 'to' and 'by' will produce infinite range)")
val check: Double => Boolean = if (by > 0) _ <= to else _ >= to
Iterator.iterate(from)(_+by).takeWhile(check)
}
//usage example:
rangeInclusiveDouble(1.1, 5.1).toSeq
//List(1.1, 2.1, 3.1, 4.1, 5.1)
rangeInclusiveDouble(1.1, 2.1, 0.1).toSeq //here you will see why range over double is tricky!
//List(1.1, 1.2000000000000002, 1.3000000000000003, 1.4000000000000004,...
Try(rangeInclusiveDouble(5.0, 1.0).toSeq)
// Failure(java.lang.AssertionError: assumption failed: range has reversed order (arguments 'from', 'to' and 'by' will produce infinite range))
Try(rangeInclusiveDouble(5.0, 1.0, 0).toSeq)
//Failure(java.lang.AssertionError: assumption failed: 'by' cannot by 0)
rangeInclusiveDouble(5.0, 1.0, -1).toSeq
//List(5.0, 4.0, 3.0, 2.0, 1.0)
It has its own problems as you can see... but works if you are careful about range limits.

Using vim editor to create a Scala script within REPL

Version of Scala I am using is Scala 2.12.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_121) and the Jline Library present is 2.14.3 .
This could sound silly but I am trying to figure out the problem when trying to create a scala file using editor cmd line vi or vim during the Scala REPL mode its throwing error. Below is my error .. Could you please let me know if there is any specific Scala Terminal console that I am suppose to use or am I doing something wrong ?
scala> vi test1.scala
<console>:1: error: ';' expected but '.' found.
vi test1.scala
I am able to do a VI and VIM as well in my system without the SCALA REPL mode but when I am in REPL I am not able to create a scala script file and execute it . What could be wrong ? Is there any settings that needs to be enabled for this ?
For saving your REPL history, use :save file.
There is limited support for using an external editor. The result of the edit is run immediately. After a reset, only the edited lines are in session history, so save will save only those lines.
$ EDITOR=gedit scala
Welcome to Scala 2.12.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_111).
Type in expressions for evaluation. Or try :help.
scala> val x = 42
x: Int = 42
scala> println(x)
42
scala> :edit -2
+val x = 17
+println(x)
17
x: Int = 17
scala> :hi 3
1896 val x = 17
1897 println(x)
1898 :hi 3
scala> :reset
Resetting interpreter state.
Forgetting this session history:
val x = 42
println(x)
val x = 17
println(x)
Forgetting all expression results and named terms: $intp, x
scala> :ed 1896+2
+val x = 5
+println(x)
5
x: Int = 5
scala> :save sc.sc
scala> :load sc.sc
Loading sc.sc...
x: Int = 5
5

scala spark paritions: understanding the output

I am running spark and scala. What is the meaning of the line that i get when i run rawblocks.partitions.length? My linkage folder had 10 files.
what does res1 and Int stand for?
Also is there a place where i
can find official documentation for spark methods? For example i
want to see details of textFile.
spark version 1.6.1
Using Scala version 2.10.5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_65)
scala> val rawblocks=sc.textFile("linkage")
rawblocks: org.apache.spark.rdd.RDD[String] = linkage MapPartitionsRDD[3] at textFile at <console>:27
scala> rawblocks.partitions.length
res1: Int = 10
The res1 and Int are not special to Spark: res1 is a name given in Scala REPL (shell) to unnamed values - results are numerated (starting from zero), for example:
scala> 10
res0: Int = 10
scala> "hello"
res1: String = hello
This should also give you a clue about Int - it's the inferred type of this value (Scala's Int is somewhat equivalent to Java's Integer).
Spark API: here's the documentation for the two primary entry points of Spark-core: SparkContext, RDD

Why is the .+() method deprecated in scala?

Just wondering why I'm seeing the following in my REPL:
scala> 5.+(9)
warning: there were 1 deprecation warning(s); re-run with -deprecation for details
res18: Double = 14.0
If you follow the instructions and run the REPL using scala -deprecation, you'll see the reason for the deprecation:
scala> 1 + 1
res0: Int = 2
scala> 1.+(1)
<console>:1: warning: This lexical syntax is deprecated. From scala 2.11,
a dot will only be considered part of a number if it is immediately followed
by a digit.
1.+(1)
^
res1: Double = 2.0
Tested on Scala 2.10.1

Scala operator oddity

When I invoke + on 2 I get an Int back, but when its done using explicit method call I get Double instead.
scala> 2+2
res1: Int = 4
scala> 2.+(2)
res2: Double = 4.0
It seems that .+() is invoked on implicit converted Int to Double.
scala> 2.+
<console>:16: error: ambiguous reference to overloaded definition,
both method + in class Double of type (x: Char)Double
and method + in class Double of type (x: Short)Double
match expected type ?
2.+
^
Why is that so ?
In Scala 2.9 and before, 2. is interpreted as 2.0 so the ambiguous dot denotes a float literal. You’d explicitly call the method by using the syntax (2).+(2).
The ambiguous floating point syntax will however be deprecated in 2.10:
scala> 2.+(2)
<console>:1: warning: This lexical syntax is deprecated. From scala 2.11, a dot will only be considered part of a number if it is immediately followed by a digit.
2.+(2)
^
<console>:2: warning: This lexical syntax is deprecated. From scala 2.11, a dot will only be considered part of a number if it is immediately followed by a digit.
2.+(2)
^
<console>:8: warning: This lexical syntax is deprecated. From scala 2.11, a dot will only be considered part of a number if it is immediately followed by a digit.
2.+(2)
^
res1: Double = 4.0
The reason is not in explicit method call -- by writing 2.+ you specifying Double on the left side and then calling addition operator on it:
scala> 2.
res0: Double = 2.0