Int division in scala - 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

Related

Why value of y and +y is same in scala REPL in below scenario

scala> val y=20
val y: Int = 20
scala> y
val res0: Int = 20
scala> +y
val res1: Int = 20
The answer makes sense. +y is equivalent to a function call of +(y) (though it's an operation). In this case if you did -y it would give an answer of -20, which is again correct.
This is very typical for most programming languages.

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

How to use previous expression's result in Scala REPL?

// when in Scala REPL
scala> 1
res0: Int = 1
How can I reuse an expression's result in another expression?
For example:
scala> 1
res0: Int = 1
scala> the_previous_expression + 1
// = 2
You can reuse the previous expression's result by looking its REPL output on the next line, the word starting with res.
// when in Scala REPL
scala> 1
res0: Int = 1 // <-- res0 is the handle that you can use
For example:
scala> 1
res0: Int = 1
scala> res0 + 1
res1: Int = 2
scala> res1 + 1
res2: Int = 3
// and so on
You can also use it with others:
scala> () => "hey!" // anonymous function
res0: () => String = $$Lambda$1104/1658578510#6cff61fc
scala> res0()
res1: String = hey

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

List#() on Intermediate List

I can get the index = 2 item from a List using apply.
scala> List(1,2,3).apply(2)
res3: Int = 3
scala> val x = List(1,2,3)
x: List[Int] = List(1, 2, 3)
scala> x(2)
res4: Int = 3
scala> List(1,2,3).apply(2)
res5: Int = 3
But, why can't I do the following?
scala> List(1,2,3).(2)
<console>:1: error: identifier expected but '(' found.
List(1,2,3).(2)
^
you don't need .
scala> List(1,2,3)(2)
res1: Int = 3