e0x3
e0x3

Reputation: 131

How to write function to compare two sets?

I have type Set = Int => Boolean; , and function subset / how to get two sets A and B and compare them?
I wrote like

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

compiling is successful, but it shows <function1> in output when I'm running the code.

Upvotes: 1

Views: 1135

Answers (2)

Randall Schulz
Randall Schulz

Reputation: 26486

I gather you're working through some exercises, possibly those from the Coursera course "Functional Programming Principles in Scala" from professor Odersky. If not, you should not be re-defining built-in names such as Set.

In any event, to answer you question, having chosen to represent "a set of integers" as a function that returns true if the Int supplied to it is a member of the set, your set is a function (of one Int argument returning a Boolean).

Scala does not record any source code when it compiles a function to JVM bytecode, so it cannot produce a String representing that function in any meaningful way and instead just produces <functionN> where N is the function's arity (number of arguments).

Upvotes: 4

Edmondo
Edmondo

Reputation: 20080

I can deduct that your Set is not a class but it's a type alias for Int=>Boolean.

Since => is a syntactic sugar for FunctionN, Set is a type alias for Function1[Int,Boolean]

Upvotes: 0

Related Questions