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)
Maybe what’s confusing you is the syntax. We can rewrite this as:
So this might be easier to sort out. We are defining a method
unionthat takes toSets and returns a newSet. In our implementation,Setis just another name for a function fromInttoBoolean(ie, a function telling us if the argument is “in the set”).The body of the the
unionmethod creates an anonymous function fromInttoBoolean(which is aSetas we have defined it). This anonymous function accepts a parameteri, an Int, and returnstrueif, and only if,iis in seta(a(i)) ORiis in setb(b(i)).