diff options
Diffstat (limited to 'Scala/funsets/src/main')
| -rw-r--r-- | Scala/funsets/src/main/scala/common/package.scala | 40 | ||||
| -rw-r--r-- | Scala/funsets/src/main/scala/funsets/FunSets.scala | 90 | ||||
| -rw-r--r-- | Scala/funsets/src/main/scala/funsets/Main.scala | 6 | 
3 files changed, 136 insertions, 0 deletions
| diff --git a/Scala/funsets/src/main/scala/common/package.scala b/Scala/funsets/src/main/scala/common/package.scala new file mode 100644 index 0000000..f1c74c3 --- /dev/null +++ b/Scala/funsets/src/main/scala/common/package.scala @@ -0,0 +1,40 @@ +import java.io.File + +package object common { + +  /** An alias for the `Nothing` type. +   *  Denotes that the type should be filled in. +   */ +  type ??? = Nothing + +  /** An alias for the `Any` type. +   *  Denotes that the type should be filled in. +   */ +  type *** = Any + +   +  /** +   * Get a child of a file. For example, +   *  +   *   subFile(homeDir, "b", "c") +   *  +   * corresponds to ~/b/c +   */ +  def subFile(file: File, children: String*) = { +    children.foldLeft(file)((file, child) => new File(file, child)) +  } + +  /** +   * Get a resource from the `src/main/resources` directory. Eclipse does not copy +   * resources to the output directory, then the class loader cannot find them. +   */ +  def resourceAsStreamFromSrc(resourcePath: List[String]): Option[java.io.InputStream] = { +    val classesDir = new File(getClass.getResource(".").toURI) +    val projectDir = classesDir.getParentFile.getParentFile.getParentFile.getParentFile +    val resourceFile = subFile(projectDir, ("src" :: "main" :: "resources" :: resourcePath): _*) +    if (resourceFile.exists) +      Some(new java.io.FileInputStream(resourceFile)) +    else +      None +  } +} diff --git a/Scala/funsets/src/main/scala/funsets/FunSets.scala b/Scala/funsets/src/main/scala/funsets/FunSets.scala new file mode 100644 index 0000000..594a49d --- /dev/null +++ b/Scala/funsets/src/main/scala/funsets/FunSets.scala @@ -0,0 +1,90 @@ +package funsets + +import common._ + +/** + * 2. Purely Functional Sets. + */ +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 = ??? + +  /** +   * 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 = ??? + +  /** +   * Returns the intersection of the two given sets, +   * the set of all elements that are both in `s` and `t`. +   */ +  def intersect(s: Set, t: Set): Set = ??? + +  /** +   * Returns the difference of the two given sets, +   * the set of all elements of `s` that are not in `t`. +   */ +  def diff(s: Set, t: Set): Set = ??? + +  /** +   * Returns the subset of `s` for which `p` holds. +   */ +  def filter(s: Set, p: Int => Boolean): Set = ??? + +  /** +   * The bounds for `forall` and `exists` are +/- 1000. +   */ +  val bound = 1000 + +  /** +   * Returns whether all bounded integers within `s` satisfy `p`. +   */ +  def forall(s: Set, p: Int => Boolean): Boolean = { +    def iter(a: Int): Boolean = { +      if (???) ??? +      else if (???) ??? +      else iter(???) +    } +    iter(???) +  } + +  /** +   * Returns whether there exists a bounded integer within `s` +   * that satisfies `p`. +   */ +  def exists(s: Set, p: Int => Boolean): Boolean = ??? + +  /** +   * Returns a set transformed by applying `f` to each element of `s`. +   */ +  def map(s: Set, f: Int => Int): Set = ??? + +  /** +   * Displays the contents of a set +   */ +  def toString(s: Set): String = { +    val xs = for (i <- -bound to bound if contains(s, i)) yield i +    xs.mkString("{", ",", "}") +  } + +  /** +   * Prints the contents of a set on the console. +   */ +  def printSet(s: Set) { +    println(toString(s)) +  } +} diff --git a/Scala/funsets/src/main/scala/funsets/Main.scala b/Scala/funsets/src/main/scala/funsets/Main.scala new file mode 100644 index 0000000..6126909 --- /dev/null +++ b/Scala/funsets/src/main/scala/funsets/Main.scala @@ -0,0 +1,6 @@ +package funsets + +object Main extends App { +  import FunSets._ +  println(contains(singletonSet(1), 1)) +} | 
