Method parameter in Scala - scala

I am beginning Scala and I don't understand what the s => part is/does. Could someone explain me that?
thrill.remove(s => s.length == 4)

It's a way of specifying a function. A function is something that takes one or more parameters as input and gives back an output. One of the ways you can specify a function is with the => symbol.
s: String => s.length == 4 // a function that takes a String called s as input
// and returns a Boolean (true if the length of s is 4
// false otherwise)
In scala you can use functions like you use integer or strings or any other kind of basic data types.
You can assign them to variables:
scala> val f = (s: String) => s.length==4 // assigning our function to f
f: String => Boolean = <function1>
scala> f("abcd") // ...and using it
res1: Boolean = true
and you can pass them as parameters to other functions or methods:
scala> val thrill = List("foo", "bar", "baz", "bazz")
thrill: List[java.lang.String] = List(foo, bar, baz, bazz)
scala> thrill.remove(s => s.length == 4)
warning: there were 1 deprecation warnings; re-run with -deprecation for detail
res2: List[java.lang.String] = List(foo, bar, baz)
here you're saying to the remove method: "apply this function s => s.length==4 to each element in the list, and remove all elements where that function returns true"
By the way, notice that remove is deprecated. The suggested alternative is filterNot
scala> thrill.filterNot(s => s.length == 4)
res3: List[java.lang.String] = List(foo, bar, baz)

The expression
s => s.length == 4
represents a function which takes a string (I'm guessing) and returns a boolean value based on whether it has length four
val f = s => s.length == 4
println(f("five")) //prints "true"
println(f("six")) //prints "false"
the s => part just declares the parameter names for the function (in this case, just one: s)

This says "run the function s => s.length == 4 over all items in thrill, and remove any where the function returns true." Logically, that function must take a single item of the type contained in thrill as a parameter, and it must return a boolean (true or false).
The scala syntax s => ... indicates a lambda function- a kind of shorthand function where in many cases the function's parameter and return types are inferred. For example in this case the compiler is smart enough to know that if thrill contains Strings, s must be a string. Likewise, it can confirm in a statically typed way that the return value (s.length == 4) is a boolean, and satisfies the requirement for a boolean return value.
In short: think of it as a function defined as boolean f(String s) { return s.length == 4; }

I believe all of the following are the same as each other (although I'm a Scala newb too, feel free to offer corrections)
thrill.remove((s:String) => s.length == 4)
thrill.remove(s => s.length == 4)
thrill.remove(_.length == 4)

Related

scala: anonymous partial function strange syntax

I came across a code similar to this, and it was surprised that it even compiles:
scala> val halfSize: PartialFunction[String, Int] = _.length match {
case even if even % 2 == 0 => even / 2
}
halfSize: PartialFunction[String,Int] = <function1>
scala> List("x", "xx").collect(halfSize)
res1: List[Int] = List(1)
As far as I known, the valid syntax to define a PartialFunction is a case function:
val halfSize: PartialFunction[String, Int] = {
case s if s.length % 2 == 0 => s.length / 2
}
The first code seems more optimized since it calls length only once. But even in the SLS I was not able to find the explanation of the syntax. Is this an undocumented feature of scalac ?
The rules are given in https://www.scala-lang.org/files/archive/spec/2.12/06-expressions.html#anonymous-functions:
The eventual run-time value of an anonymous function is determined by the expected type:
...
PartialFunction[T, U], if the function literal is of the shape x => x match { … }
In this case the literal does have such a shape, as rogue-one's answer explains, so PartialFunction is allowed as the expected type.
(EDIT: actually, it doesn't, since it matches x.length instead of x. This looks like a minor bug, but one which should be fixed by changing the specification.)
A PartialFunction's value receives an additional isDefinedAt member, which is derived from the pattern match in the function literal, with each case's body being replaced by true, and an added default (if none was given) that evaluates to false.
So in this case it ends up with
def isDefinedAt(x: String) = x.length match {
case even if even % 2 == 0 => true
case _ => false
}
val halfSize: PartialFunction[String, Int] = _.length match {
case even if even % 2 == 0 => even / 2
}
The underscore (_) in the above function is just a short hand notation that refers to the single argument of the function. Thus the above snippet is just a short form of
val halfSize: PartialFunction[String, Int] = { (x: String) => x.length match {
case even if even % 2 == 0 => even / 2
}
}

Where does set come from in "val Singleton4 : Set = set => set == 4"?

What's the verbose way for val Singleton4 : Set = set => set == 4? I am unable to understand where the set parameter is coming from.
What maybe confuses you is the fact that Set is not what one would expect from the Scala library. You should read the line as:
val Singleton4: Int => Boolean = someArbitraryName => someArbitraryName == 4
// <identifier> <type> = <argument> => <returnValue>
It's not "coming" from anywhere, Set is defined as a function from an integer to a boolean and the type of Singleton4 is exactly that, it takes an integer and returns a function which needs another integer and returns a boolean:
scala> type Set = Int => Boolean
defined type alias Set
scala> def singletonSet(elem: Int): Set = set => set == elem
singletonSet: (elem: Int)Int => Boolean
scala> val p: Set = singletonSet(3)
p: Int => Boolean = <function1>
So now p is a function which takes an integer:
scala> p(3)
res0: Boolean = true
scala> p(2)
res1: Boolean = false
An alternative way of writing that is this:
def singletonSet(elem: Int) = (i: Int) => i == elem
val p: (Int) => Boolean = singletonSet(3)
Or using a different way but achieving the same result:
def singletonSet(elem: Int)(i: Int): Boolean = i == elem
val p: (Int) => Boolean = singletonSet2(3)
By currying the function and applying only one argument you get back a function which still needs another integer to be fully evaluated.
The other answers are very good, but I thought I'd throw some additional details to help newcomers to Scala understand the notation better (as there are some hidden gems in the notation that were not uncovered/described).
Let's play the Scala compiler game!
The following val says enough about what Set the type represents.
val Singleton4: Set = set => set == 4
The type of val is given explicitly - it's Set. We don't however know much about it...yet. It has however to be given to the compiler before it can be used and given the left-hand side it's a function (mind the => two-letter string) from a type (we'll get to it in a moment) to Boolean since set == 4 will inevitably end as Boolean.
From set == 4 we can deduce that set can only be Int (so you can compare apples to apples) and hence we've got the type of set as Int.
The right-hand side of = of the val could also have been written as:
val Singleton4: Set = (set: Int) => set == 4
So, the Singleton4 val is an instance of a function Int => Boolean that's called Set. Set's declared as such somewhere above (lexicographically):
type Set = Int => Boolean
You could substitute Set to the type alias and end up with:
val Singleton4: Int => Boolean = (set: Int) => set == 4

Magic PartialFunction in Scala

I don't think this code should work, but it does (in Scala 2.10):
scala> ((i: Int) => i.toString match {
| case s if s.length == 2 => "A two digit number"
| case s if s.length == 3 => "A three digit number"
| }): PartialFunction[Int,String]
res0: PartialFunction[Int,String] = <function1>
// other interactions omitted
scala> res0.orElse(PartialFunction((i: Int) => i.toString))
res5: PartialFunction[Int,String] = <function1>
scala> res5(1)
res6: String = 1
How does it work? I would expect a MatchError to be thrown inside res0.
The Scala language specification does not seem to explicitly document how res0 should be interpreted.
The trick is that the compiler is not interpreting your definition as a total function converted to a partial function -- it's actually creating a partial function in the first place. You can verify by noting that res0.isDefinedAt(1) == false.
If you actually convert a total function to a partial function, you will get the behavior you expected:
scala> PartialFunction((i: Int) => i.toString match {
| case s if s.length == 2 => "A two digit number"
| case s if s.length == 3 => "A three digit number"
| })
res0: PartialFunction[Int,String] = <function1>
scala> res0 orElse ({ case i => i.toString }: PartialFunction[Int, String])
res1: PartialFunction[Int,String] = <function1>
scala> res1(1)
scala.MatchError: 1 (of class java.lang.String)
// ...
In this example, PartialFunction.apply treats its argument as a total function, so any information about where it's defined is lost.
orElse is defined on PartialFunction so that the argument is treated as a fallback for the cases when the original is not defined. See the API.
You say that if res0 does not match, you want to try your other pf instead. How this essentially works is:
if (res0.isDefinedAt(1)) {
res0(1)
} else {
other(1)
}
The orElse call creates an instance of OrElse, which inherits from PartialFunction: https://github.com/scala/scala/blob/master/src/library/scala/PartialFunction.scala#L159
When you now call apply on this OrElse, it will call f1.applyOrElse(x, f2): https://github.com/scala/scala/blob/master/src/library/scala/PartialFunction.scala#L162
This will call if (isDefinedAt(x)) apply(x) else f2(x): https://github.com/scala/scala/blob/master/src/library/scala/PartialFunction.scala#L117-L118
Therefore you will only get a MatchError, when neither of the pf's matches.

Generic getter method for tuples in Scala which preserves dynamic type?

I am hoping to write a Scala method which takes in a tuple of any size and type along with an index, and returns the element in the tuple at that index. I know how to do everything but preserve the type. I haven't yet figured out a way to make the return value be of the dynamic type of the tuple item.
Here is the function I have so far:
def subscript_get(tup: Product, index:Int): Any={
return tup.productElement(index)
}
The usage for example would be:
subscript_get((0,1,2,3),0)
--> Int = 0
subscript_get((0,1,"asdf",3),2)
--> java.lang.String = asdf
I know that I can cast the result back afterwards to what I am looking for, but this doesn't work for me because I can't always know what type I should cast to.
Is something like this even possible ? Thanks!
I'm not sure you want a solution that uses macros, but for the record (and since I've written precisely this method before), here's how you can implement this with the macro system in 2.10.
As I note in a comment above, this approach requires index to be an integer literal, and relies on "underspecified but intended" behavior in 2.10. It also raises some tricky questions about documentation.
import scala.language.experimental.macros
import scala.reflect.macros.Context
object ProductIndexer {
def at[T <: Product](t: T)(index: Int) = macro at_impl[T]
def at_impl[T <: Product: c.WeakTypeTag](c: Context)
(t: c.Expr[T])(index: c.Expr[Int]) = {
import c.universe._
index.tree match {
case Literal(Constant(n: Int)) if
n >= 0 &&
weakTypeOf[T].members.exists {
case m: MethodSymbol => m.name.decoded == "_" + (n + 1).toString
case _ => false
} => c.Expr[Any](Select(t.tree, newTermName("_" + (n + 1).toString)))
case Literal(Constant(_: Int)) => c.abort(
c.enclosingPosition,
"There is no element at the specified index!"
)
case _ => c.abort(
c.enclosingPosition,
"You must provide an integer literal!"
)
}
}
}
And then:
scala> import ProductIndexer._
import ProductIndexer._
scala> val triple = (1, 'a, "a")
triple: (Int, Symbol, String) = (1,'a,a)
scala> at(triple)(0)
res0: Int = 1
scala> at(triple)(1)
res1: Symbol = 'a
scala> at(triple)(2)
res2: String = a
All statically typed as expected, and if you give it an index that's out of range (or not a literal), you get a nice compile-time error.
You cannot do that. If you use Product, the (compile-time) type of the values in the tuples is lost. Further, a method cannot adapt its return type based on an value you pass in (not entirely true, see dependent method types, but true for an Int).
If you do not know what type to cast to, you could use pattern matching:
subscript_get(..., 1) match {
case v: Int => // do something with Int
case v: String => // do something with String
// snip
case _ => sys.error("don't know how to handle this")
}

Is my understanding of below scala code correct?

I'm just trying to understand the below code :
Here a new type alias Set is declared which is a function that takes an Int
parameter and returns a boolean
type Set = Int => Boolean
Here a new method 'contains' is declared, which takes two parameters of type Set and Int
which returns a boolean. The boolean is set to the function declared in earlier
('type Set = Int => Boolean')
But what logic is performed to determine if the Int 'elem' is a member of Set 's'
def contains(set: Set, elem: Int): Boolean = set(elem)
Here a method is defined which returns a set which returns a function ?
def singletonSet(elem: Int): Set = set => set == elem
Complete code with comments :
/**
* We represent a set by its characteristic function, i.e.
* its `contains` predicate.
*/
type Set = Int => Boolean
/**
* Indicates whether a set contains a given element.
*/
def contains(set: Set, elem: Int): Boolean = set(elem)
/**
* Returns the set of the one given element.
*/
def singletonSet(elem: Int): Set = set => set == elem
Let's read sort of backwards, in logical order.
Say you have a finite set of integers: 0, 1, 2, 3, 5, 8 for instance
One way to describe this set of integers is through a function (its characteristic or indicator function) that, for each integer, returns true if the integer is in the set, false if it is not.
The signature for this function, as we described it, must always be Int => Boolean ("give me an integer, I will tell you if it's in the set"), while its implementation will vary depending on the specific set.
For the set in my example above you could write this function simply as:
val mySet: Int => Boolean = x => Array(0,1,2,3,5,8) contains x
or recognize that the ints in the set are the first ones of the Fibonacci sequence and define f in a slightly more sophisticated way (which I won't do here...).
Note that the "contains" I've used is defined for all scala collections.
In any case, now you have a function that tells you what is in the set and what is not.
Let's try it in the REPL.
scala> val mySet: Int => Boolean = x => Array(0,1,2,3,5,8) contains x
mySet: Int => Boolean = <function1>
scala> mySet(3)
res0: Boolean = true
scala> mySet(9)
res1: Boolean = false
Now, mySet has type Int => Boolean, which we can make more readable if we define it as a type alias.
scala> type Set = Int => Boolean
defined type alias Set
Besides readability, defining Set as an alias of Int => Boolean is making it explicit that in a way a Set is its characteristic function. We can redefine mySet in a more concise (but otherwise equivalent) way with the Set type alias:
scala> val mySet: Set = x => Array(0,1,2,3,5,8) contains x
mySet: Int => Boolean = <function1>
Now for the last piece of this long answer. Let's define a characteristic function to describe this Singleton set: 3.
Easy:
val Singleton3 : Set = set => set == 3
for a Singleton set containing only 4, it would be:
val Singleton4 : Set = set => set == 4
So, let's generalize the creation of these functions and write a method that returns a Singleton function that, for any integer, describes the set containing only that integer:
def singletonSet(elem: Int): Set = set => set == elem
APPENDIX:
I skipped this part, because it wasn't really needed: def contains(set: Set, elem: Int): Boolean = set(elem)
I think it's sort of pointless and (without more context) it looks just like a contrived example to demonstrate how you can pass a function around as an argument, just like any other type in scala. It takes the Int => Bool function and the Int and just applies the function to the Int so you can do
scala> contains(mySet, 3)
res2: Boolean = true
which is like calling mySet(3) directly.
After watching the lecture video on "Currying", I believe that Paolo's solution expressed in a more verbose manner is :
def singletonSet(elem: Int): Set = {
def innerFunction (givenElement: Int) =
if (elem == givenElement) true
else false
innerFunction
}
Plesae correct me if I am wrong!
To answer your question - But what logic is performed to determine if the Int 'elem' is a member of Set 's'
This is performed when you make the actual function call. Consider the following function call.
contains(singletonSet(1), 1)
Now singletonSet is defined as def singletonSet(elem: Int): Set = x => x == elem (I choose to use the identifier x for clarity sake). The return type of the singletonSet is the function of type Set which takes an Int argument and returns Boolean. So the above calling function's first argument singletonSet(1) equates to the function x => x == 1 as elem here is 1. So we get
contains((x => x == 1),1)
Now considering the definition of contains function def contains(f: Set, elem: Int): Boolean = f(elem). The first argument in the call above is the function x => x == 1 which substitutes formal parameter f and second argument 1 substitutes formal parameter elem. The return value of contains is the function f(elem) which equates to f(1). Since f(x) is defined as (x == 1), f(1) equates to (1 == 1) which returns true.
Going by the same logic, a function call like contains(singletonSet(1), 2) would finally equate to (1 == 2) which will return false.
I'm taking the course now, was also confused, but I think I get the point now.
def singletonSet(elem: Int): Set = (x : Int) => x == elem
here singletonSet is a function that returns a function with the type Set, which is defined as type Set = Int => Boolean
so when you call def contains(s:Set, elem:Int): Boolean = s(elem), for example: contains(singletonSet(1), 2), singletonSet(1) is returning a function with elem(in the definition of singletonSet) set to 1, and the 2(also defined as elem, but is defined in the parameter of contains) is passed in as the x in singletonSet definition, we need to get rid of java set idea, we don't need the singletonSet to persist the value we set.
For better understanding, we can change the parameter name as following:
def singletonSet(elemToStore: Int): Set = (x : Int) => x == elemToStore
def contains(s: Set, elemToCheck: Int): Boolean = s(elemToCheck)