Skip to content

Instantly share code, notes, and snippets.

@colaru
Created April 4, 2013 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colaru/5309747 to your computer and use it in GitHub Desktop.
Save colaru/5309747 to your computer and use it in GitHub Desktop.
Scala FunSets
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