user701254
user701254

Reputation: 3943

Unsure how this union function on Sets works

I'm trying to understand this def method :

def union(a: Set, b: Set): Set = i => a(i) || b(i)

Which is referred to at question : Scala set function

This is my understanding :

The method takes two parameters of type Set - a & b A Set is returned which the union of the two sets a & b.

Here is where I am particularly confused : Set = i => a(i) || b(i)

The returned Set itself contains the 'or' of Set a & b . Is the Set 'i' being populated by an implicit for loop ?

Since 'i' is a Set why is it possible to or a 'set of sets', is this something like whats being generated in the background :

a(i) || b(i) 
becomes
SetA(Set) || SetB(Set)

Upvotes: 1

Views: 3425

Answers (3)

Viktor Seifert
Viktor Seifert

Reputation: 636

No the set is not populated by a for loop.

The return type of union(a: Set, b: Set): Set is a function. The code of the declaration a(i) || b(i) is not executed when you call union; it will only be executed when you call the result of union.

And i is not a set it is an integer. It is the single argument of the function returned by union.

What happens here is that by using the set and union function you construct a binary tree of functions by combining them with the logical-or-operator (||). The set function lets you build leafs and the union lets you combine them into bigger function trees.

Example:

def set_one = set(1)
def set_two = set(2)
def set_three = set(2)
def set_one_or_two = union(set_one, set_two)
def set_one_two_three = union(set_three, set_one_or_two)

The set_one_two_three will be a function tree which contains two nodes: the left is a function checking if the passed parameter is equal to 3; the right is a node that contains two functions itself, checking if the parameter is equal to 1 and 2 respectively.

Upvotes: 1

Luigi Plinge
Luigi Plinge

Reputation: 51109

If you look carefully, that question defines a type Set = Int => Boolean. So we're not talking about scala.collection.Set here; we're talking Int => Booleans.

To write a function literal, you use the => keyword, e.g.

x => someOp(x)

You don't need to annotate the type if it's already known. So if we know that the r.h.s. is Int => Boolean, we know that x is type Int.

Upvotes: 1

dhg
dhg

Reputation: 52691

Maybe what's confusing you is the syntax. We can rewrite this as:

type Set = (Int => Boolean)

def union(a: Set, b: Set): Set = {
  (i: Int) => a(i) || b(i)
}

So this might be easier to sort out. We are defining a method union that takes to Sets and returns a new Set. In our implementation, Set is just another name for a function from Int to Boolean (ie, a function telling us if the argument is "in the set").

The body of the the union method creates an anonymous function from Int to Boolean (which is a Set as we have defined it). This anonymous function accepts a parameter i, an Int, and returns true if, and only if, i is in set a (a(i)) OR i is in set b (b(i)).

Upvotes: 12

Related Questions