Created
April 4, 2013 11:48
-
-
Save colaru/5309747 to your computer and use it in GitHub Desktop.
Scala FunSets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object FunSets { | |
/** | |
* 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(s: Set, elem: Int): Boolean = s(elem) | |
/** | |
* Returns the set of the one given element. | |
*/ | |
def singletonSet(elem: Int): Set = ((x:Int) => elem == x) | |
/** | |
* Returns the union of the two given sets, | |
* the sets of all elements that are in either `s` or `t`. | |
*/ | |
def union(s: Set, t: Set): Set = ((x:Int) => contains(s, x) || contains(t, x)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment