Last active
December 15, 2017 03:37
-
-
Save izmailoff/57154c3dd6bed6e63b3d7d65264f7f36 to your computer and use it in GitHub Desktop.
Example of functional sets in Python
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
set_of_1 = lambda i: i == 1 | |
set_of_1(1) | |
set_of_1(2) | |
empty = lambda i: False | |
empty(1) | |
union = lambda s1, s2: lambda i: s1(i) or s2(i) | |
intersect = lambda s1, s2: lambda i: s1(i) and s2(i) | |
set_of_5_3_2 = union(union(lambda x: x == 5, lambda x: x == 3), lambda x: x == 2) | |
set_of_5_3_2(5) | |
set_of_5_3_2(2) | |
set_of_5_3_2(1) | |
set_of_5_3_2_1 = union(set_of_1, set_of_5_3_2) | |
set_of_5_3_2_1(3) | |
set_of_5_3_2_1(1) | |
intersect(set_of_5_3_2, set_of_5_3_2_1)(1) | |
intersect(set_of_5_3_2, set_of_5_3_2_1)(5) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python REPL output of the code above: