Weird behavior in Scala - scala

How to justify below codes in Scala ? Can someone explain why does it return True in Example 3 and False in first two examples ?
Example 1:
scala> val f1 = 5.2
val f1: Double = 5.2
scala> val f2 = 5.2F
val f2: Float = 5.2
scala> f1==f2
val res8: Boolean = false
Example 2:
scala> val f1 = 5.24
val f1: Double = 5.24
scala> val f2 = 5.24F
val f2: Float = 5.24
scala> f1==f2
val res9: Boolean = false
Example 3:
scala> val f1 = 5.25
val f1: Double = 5.25
scala> val f2 = 5.25F
val f2: Float = 5.25
scala> f1==f2
val res10: Boolean = true

Nothing to do with Scala, but most decimal factions cannot be accurately represented as binary floating point numbers. For those numbers, the Float value will be different from the Double value so == will return false.
In this case 5.25 does have an accurate binary floating point value (101.01) so both the Float and the Double are the same.

Related

How to access the replaceAllIn() counter?

I am using
val str2 = regex.replaceAllIn(str1, "other")
and need to count the number of replaces... There are a way retrieve the value of the internal replaceAllIn counter?
PS: this is usual in other languages (example), so I am supposing that Scala offer similar thing.
scala> val r = "x".r
r: scala.util.matching.Regex = x
scala> var i = 0
i: Int = 0
scala> r.replaceAllIn("xooxxox", m => { i += 1 ; "X" })
res0: String = XooXXoX
scala> i
res1: Int = 4
will do appendReplacement under the hood.
Takes another step but you could findAllIn and count the number found. Then do replaceAllIn.
scala> "foo".r.findAllIn("barbazfoobazfoo").size
res7: Int = 2

Scala subString function

Hi I am looking for a solution it will return a substring from string for the given indexes.For avoiding index bound exception currently using if and else check.Is there a better approach(functional).
def subStringEn(input:String,start:Int,end:Int)={
// multiple if check for avoiding index out of bound exception
input.substring(start,end)
}
Not sure what you want the function to do in case of index out of bound, but slice might fit your needs:
input.slice(start, end)
Some examples:
scala> "hello".slice(1, 2)
res6: String = e
scala> "hello".slice(1, 30)
res7: String = ello
scala> "hello".slice(7, 8)
res8: String = ""
scala> "hello".slice(0, 5)
res9: String = hello
Try is one way of doing it. The other way is applying substring only if length is greater than end using Option[String].
invalid end index
scala> val start = 1
start: Int = 1
scala> val end = 1000
end: Int = 1000
scala> Option("urayagppd").filter(_.length > end).map(_.substring(start, end))
res9: Option[String] = None
valid end index
scala> val end = 6
end: Int = 6
scala> Option("urayagppd").filter(_.length > end).map(_.substring(start, end))
res10: Option[String] = Some(rayag)
Also, you can combine filter and map to .collect as below,
scala> Option("urayagppd").collect { case x if x.length > end => x.substring(start, end) }
res14: Option[String] = Some(rayag)
scala> val end = 1000
end: Int = 1000
scala> Option("urayagppd").collect { case x if x.length > end => x.substring(start, end) }
res15: Option[String] = None

How to get value of variables in Scala, where name of variable is obtained as string

i have existing variables:
scala> a
res69: Double = 5.0
scala> b
res70: Double = 10.0
scala> c
res71: Double = 15.0
There is a list containing variable names as string like:
scala> val variableList = List("a","b","c")
variableList: List[String] = List(a, b, c)
How to get values of variables in this list. I am expecting output as:
List(5.0, 10.0, 15.0)
if the scope of question is limited to getting values of terms defined in scala REPL, following works:
> val a = 5.0
> val b = 10.0
> val c = 15.0
> val variableList = List("a", "b", "c")
> variableList.map(v => $intp.valueOfTerm(v).getOrElse("Undefined: " + v))
// List[AnyRef] = List(5.0, 10.0, 15.0)
$intp is the REPL's interpreter.IMain object.

One argument referencing another in the argument list

Occasionally, I encounter one argument wanting to reference another. For instance,
def monitor(time: Double, f: Double => Double, resolution: Double = time / 10) = {...}
Note that resolution refers to time. Are there languages where this is possible? Is it possible in Scala?
It is somewhat possible in Scala, but you have to curry the parameters:
def monitor(time: Double, f: Double => Double)(resolution: Double = time / 10)
You cannot do it in the way the question is posed.
I don't know any langage where this construction is possible, but a simple workaround is not difficult to find.
In scala, something like this is possible :
scala> def f(i : Int, j : Option[Int] = None) : Int = {
| val k = j.getOrElse(i * 2)
| i + k
| }
f: (i: Int, j: Option[Int])Int
scala> f(1)
res0: Int = 3
scala> f(1, Some(2))
res1: Int = 3
In scala, you can also make something like this :
scala> def g(i : Int)(j : Int = i * 2) = i + j
g: (i: Int)(j: Int)Int
scala> g(2)(5)
res6: Int = 7
scala> g(2)()
res7: Int = 6

Int division in scala

I have two Int values in Scala.
scala> val a = 3
a: Int = 3
scala> val b = 5
b: Int = 5
Now, I want to divide them and get Float. With as little boilerplate as possible.
If I do a/b, I get
scala> a/b
res0: Int = 0
I cannot do simple Java (float).
scala> ((Float)a)/b
<console>:9: error: value a is not a member of object Float
((Float)a)/b
^
What should I do?
The following line followed by its result should solve your problem.
scala> a.toFloat/b
res3: Float = 0.6
Alternative answer that uses type ascription:
scala> (a:Float)/b
res0: Float = 0.6