Last active
December 15, 2017 03:44
-
-
Save izmailoff/fe1f99d4265edbc1096ee42badeabad7 to your computer and use it in GitHub Desktop.
Example of functional sets in Scala
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
type Set = Int => Boolean | |
val setOf_1: Set = (i: Int) => (i == 1) | |
val empty: Set = (i: Int) => false | |
val union: (Set, Set) => Set = | |
(s1: Set, s2: Set) => (i: Int) => s1(i) || s2(i) | |
val intersect: (Set, Set) => Set = | |
(s1: Set, s2: Set) => (i: Int) => s1(i) && s2(i) | |
val setOf_5_3_2 = union(union(_ == 5, _ == 3), _ == 2) | |
setOf_5_3_2(5) | |
setOf_5_3_2(2) | |
setOf_5_3_2(1) | |
val setOf_5_3_2_1 = union(setOf_1, setOf_5_3_2) | |
setOf_5_3_2_1(3) | |
setOf_5_3_2_1(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output from running the above code in REPL: