Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pwpearson/65e2c895fce6c02a9bbd7787ee578a27 to your computer and use it in GitHub Desktop.
Save pwpearson/65e2c895fce6c02a9bbd7787ee578a27 to your computer and use it in GitHub Desktop.
scala> type Set = Int => Boolean
defined type alias Set
scala> Set
res0: scala.collection.immutable.Set.type = scala.collection.immutable.Set$@7a7f0991
scala> def contains(s: Set, elem: Int): Boolean = s(elem)
contains: (s: Int => Boolean, elem: Int)Boolean
scala> contains(x => x%2 == 0, 2)
res1: Boolean = true
scala> contains(x => x%2 == 0, 1)
res2: Boolean = false
scala> contains(x => x>100, 1)
res3: Boolean = false
scala> contains(x => x>100, 2)
res4: Boolean = false
scala> contains(x => x>100, 99)
res5: Boolean = false
scala> contains(x => x>100, 100)
res6: Boolean = false
scala> contains(x => x>100, 101)
res7: Boolean = true
scala> val s1 = singletonSet(1)
<console>:40: error: not found: value singletonSet
val s1 = singletonSet(1)
^
scala> val s1 = Set(1)
s1: scala.collection.immutable.Set[Int] = Set(1)
scala> val s2 = Set(2)
s2: scala.collection.immutable.Set[Int] = Set(2)
scala> union(s1, s2)
<console>:43: error: not found: value union
union(s1, s2)
^
scala> def union(a: Set, b:Set) = a+b
<console>:41: error: type mismatch;
found : Int => Boolean
required: String
def union(a: Set, b:Set) = a+b
^
scala> def union(a: Set, b:Set) = a.concat(b)
<console>:41: error: value concat is not a member of Int => Boolean
def union(a: Set, b:Set) = a.concat(b)
^
scala> s1 || s2
<console>:43: error: value || is not a member of scala.collection.immutable.Set[Int]
s1 || s2
^
scala> s1 | s2
res10: scala.collection.immutable.Set[Int] = Set(1, 2)
scala> s1 & s2
res11: scala.collection.immutable.Set[Int] = Set()
scala> s1 | s2
res12: scala.collection.immutable.Set[Int] = Set(1, 2)
scala> Set(1) | Set(2)
res13: scala.collection.immutable.Set[Int] = Set(1, 2)
scala> Set(1) | Set(2)(1)
<console>:41: error: type mismatch;
found : Boolean
required: scala.collection.GenSet[Int]
Set(1) | Set(2)(1)
^
scala> (Set(1) | Set(2))(1)
res15: Boolean = true
scala> (Set(1) | Set(2))(2)
res16: Boolean = true
scala> (Set(1) | Set(2))(3)
res17: Boolean = false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment