Why does not ; equivalent to the eol? [duplicate] - scala

This question already has answers here:
toList on Range with suffix notation causes type mismatch
(2 answers)
Closed 6 years ago.
Here is the code that compiles as intended
def coarse_grained: Int = {
def fib: Int = List(1,2) sum ;
fib
}
and one which does not
def coarse_grained: Int = {
def fib: Int = List(1,2) sum
fib
}
The only difference is ; after the sum.

As you know, List(2,6,9).drop(1) can also be written as List(2,6,9) drop 1. In fact, it can also be written like this.
List(2,6,9) drop
1
The compiler keeps looking for the final argument, even past a newline. So if you want to do this List(1,2).sum like this List(1,2) sum, you'll need to use the semicolon ; to tell the compiler to stop looking for the final argument. It's not coming.

Related

Placeholder Syntax in Scala Programming [duplicate]

This question already has answers here:
What are all the uses of an underscore in Scala?
(7 answers)
Closed 2 years ago.
I came across the below placeholder syntax in scala. And could not able to under what '_' is doing in the code
def evenDef(n: Int): Int => Boolean = _ => n % 2 == 0
//evenDef: (n: Int)Int => Boolean
val result = evenDef(2)
//res: Int => Boolean = $$Lambda$5397/911325#3aa9352
result(9)
//res18: Boolean = true
I am little confused how the above is returning 'true' for the value 9, even though its a odd number.
Can anyone help me in understand the '_' in the code ?
The code is equivalent to
def evenDef(n: Int): Int => Boolean = (ignored: Int) => n % 2 == 0
Since ignored is not used anywhere it can be replaced with the underscore placeholder.
Admittedly the code is a bit useless the way it stands. For any integer n it returns a constant function that either returns true for even values of n and false otherwise. The argument of the returned function does not matter at all.

'for' loop inside the code snippet not understood [duplicate]

This question already has answers here:
Confused with the for-comprehension to flatMap/Map transformation
(5 answers)
in scala why does for yield return option instead of string
(3 answers)
Is for-yield-getOrElse paradigmatic Scala or is there a better way?
(1 answer)
Closed 3 years ago.
I have below piece of code, which is printing: Some(600) as output.
Its not understood how the addition is happening inside 'for' loop.
In below, its confusing whats happening inside the code block of 'for' loop and how the variable 'y' is being calculated. Can someone please help?
object TestObject extends App
{
def toInt(s: String): Option[Int] = {
try
{
Some(Integer.parseInt(s.trim))
}
catch
{
case e: Exception => None
}
}
val y = for
{
a <- toInt("100")
b <- toInt("200")
c <- toInt("300")
} yield a + b + c
println(y)
}
In Scala this is called a for-comprehension.
toInt wraps the value in an Option which is extracted by <- and assigned to a and so on.
If one of the Option is None the result would be None
yield always returns its last statement, in your case: a + b + c
And so the result is Some(600).
See the documentation here: https://docs.scala-lang.org/tour/for-comprehensions.html
From inside the for loop, toInt method is being called which returns an Option[Int]. Now what for loop does is to simply open the Option container and assign variable a with value of 100. On similar lines, variables b and c are assigned value of 200 and 300.
And in the end the yield statement adds the values a,b and c and puts it back in the Option container.
Please refer to https://docs.scala-lang.org/tutorials/FAQ/yield.html and https://alvinalexander.com/scala/scala-for-loop-yield-examples-yield-tutorial

How can I get all the indexes of a Particular String in String vector in scala [duplicate]

This question already has an answer here:
Find Indexes *Where*
(1 answer)
Closed 6 years ago.
I am new to scala,
How can i get all the indexs for particular string.
for example:
var taluk = List("Hyderabad", "Nampally", "Hyderabad" ,"Khairatabad")
taluk.indexOf("Hyderabad")
output is 0
But I want
output as 0,2
because there are two string match in vector.
One way to do this: zipWithIndex and then collect the indices for values matching yours:
scala> taluk.zipWithIndex.collect { case ("Hyderabad", i) => i }
res0: List[Int] = List(0, 2)

will 'val' produce mutable data? [duplicate]

This question already has answers here:
Why is it possible to declare variable with same name in the REPL?
(2 answers)
Closed 7 years ago.
In REPL when we type the below command
scala> val p = 1 << 1
p: Int = 2
again
scala> val p = 1 << 2
p: Int = 4
my question is , I read that val is immutable . but in this case the value is changing right . Well can someone tell me why . is this really an example of mutatating . Please help
This behaviour appears in REPL only. If you try to define val twice in Scala code you'll get compilation error. In REPL second definition of val just shadows previous value of p
yep, as nyavro said, in the REPL you can override vals. Just think that in IDE if you make a mistake typing a value you can fix, in the REPL how would you fix? you would need to close the session?

Is there a way to use ++ on Int in scala? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
++ operator in Scala
I want to increment an Int variable in scala. But, because Int is immutable, I have to do this
var myInt: Int = 5
....
myInt = myInt + 1
which seems a little too complicated. What I would like to do is
var myInt: Int = 5
....
myInt++
however, since Int is immutable, I can't do this. Is there any solution? Because I can't be first who would want to use ++ on integer variable...
A ++ operator is not a language construct of Scala, and the desired behaviour cannot be achieved with a regular method definition. But Scala offers at least some syntactic help, in that a call a += b will be automatically expanded to a = a + b unless a direct method += exists. Thus:
var myInt = 5
myInt += 1