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

// 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

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: How to make a closure not to see changes in its free variable?

In the following code snippet, closure foo see the changes made in x as it should in scala. However, how can I make local variable y in foo hold value of x permanently and not see changes?
scala> var x = 10
x: Int = 10
scala> val foo = (a:Int) => {val y = x; a + y}
foo: Int => Int = <function1>
scala> foo(3)
res1: Int = 13
scala> x = 5
x: Int = 5
scala> foo(3) //see changes made in x. But how can I make closure not to see changes made on x?
res2: Int = 8
You could do something like this:
val foo = ((x:Int) => (a:Int) => {val y = x; a + y})(x)
In this case, x is bound in foo.
What you are doing is an example of closure.
scala> var x = 10
x: Int = 10
scala> val foo = { val y = x; (a: Int) => a + y }
foo: Int => Int = $$Lambda$1027/1344946518#5416f8db
scala> foo(3)
res0: Int = 13
scala> x = 5
x: Int = 5
scala> foo(3)
res1: Int = 13

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

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

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