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, 8for instanceOne 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:
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.
Now, mySet has type
Int => Boolean, which we can make more readable if we define it as a type alias.Besides readability, defining
Setas an alias ofInt => Booleanis 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 theSettype alias:Now for the last piece of this long answer. Let’s define a characteristic function to describe this Singleton set:
3.Easy:
for a Singleton set containing only 4, it would be:
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:
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 => Boolfunction and theIntand just applies the function to theIntso you can dowhich is like calling
mySet(3)directly.