Skip to content

Instantly share code, notes, and snippets.

@gabrieljones
Created August 12, 2024 18:03
Show Gist options
  • Save gabrieljones/35ab32aa85d2e3f72b7038be7bfdb15e to your computer and use it in GitHub Desktop.
Save gabrieljones/35ab32aa85d2e3f72b7038be7bfdb15e to your computer and use it in GitHub Desktop.
package com.gabrieljones
/**
* Represents a set of elements that may or may not exist
* @tparam T the type of the elements in the set
*/
sealed trait ExiSet[T] {
def exists(p: T => Boolean): Boolean
}
object ExiSet {
/**
* Represents a set with no members
* @tparam T the type of the elements in the set
* @return an empty ExiSet
*/
def none[T]: ExiSet[T] = ExiSet.None.asInstanceOf[ExiSet[T]]
/**
* Represents a set with all potential members of type T
* @tparam T the type of the elements in the set
* @return an ExiSet with all potential members
*/
def all[T]: ExiSet[T] = ExiSet.All.asInstanceOf[ExiSet[T]]
/**
* Represents a set with no members
*/
private object None extends ExiSet[Any] {
override def exists(p: Any => Boolean): Boolean = false
}
/**
* Represents a set with some members of type T
* @param elem the set of elements
* @tparam T the type of the elements in the set
*/
case class Some[T](elem: Set[T]) extends ExiSet[T] {
override def exists(p: T => Boolean): Boolean = elem.exists(p)
}
/**
* Represents a set with all possible members
*/
private object All extends ExiSet[Any] {
override def exists(p: Any => Boolean): Boolean = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment