Last active
December 28, 2015 21:09
-
-
Save dbrattli/f52a15a0c6a380c7e9c4 to your computer and use it in GitHub Desktop.
List out of lambda, Python versions
This file contains 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
empty_list = None | |
def prepend(el, lst): | |
def func(selector): | |
return selector(el, lst) | |
return func | |
def head(lst): | |
def selector(h, t): | |
return h | |
return lst(selector) | |
def tail(lst): | |
def selector(h, t): | |
return t | |
return lst(selector) | |
def is_empty(lst): | |
return lst is empty_list | |
a = prepend("a", prepend("b", empty_list)) | |
assert("a" == head(a)) | |
assert("b" == head(tail(a))) | |
assert(tail(tail(a))==empty_list) | |
assert(not is_empty(a)) | |
assert(is_empty(empty_list)) |
This file contains 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
empty_list = None | |
def prepend(el, lst): | |
return lambda selector: selector(el, lst) | |
def head(lst): | |
return lst(lambda h, t: h) | |
def tail(lst): | |
return lst(lambda h, t: t) | |
def is_empty(lst): | |
return (lst == empty_list) | |
a = prepend("a", prepend("b", empty_list)) | |
assert("a" == head(a)) | |
assert("b" == head(tail(a))) | |
assert(tail(tail(a))==empty_list) | |
assert(not is_empty(a)) | |
assert(is_empty(empty_list)) |
This file contains 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
empty_list = None | |
prepend = lambda el, list: lambda selector: selector(el, list) | |
head = lambda list: list(lambda h, t: h) | |
tail = lambda list: list(lambda h, t: t) | |
is_empty = lambda list: list is empty_list | |
a = prepend("a", prepend("b", empty_list)) | |
assert("a" == head(a)) | |
assert("b" == head(tail(a))) | |
assert(tail(tail(a))==empty_list) | |
assert(not is_empty(a)) | |
assert(is_empty(empty_list)) |
This file contains 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
class List(object): | |
def __init__(self, list=None): | |
self.list = list | |
def prepend(self, el): | |
return List(lambda selector: selector(el, self.list)) | |
def head(self): | |
return self.list(lambda h, t: h) | |
def tail(self): | |
return List(self.list(lambda h, t: t)) | |
def is_empty(self): | |
return not self.list | |
a = List().prepend("b").prepend("a") | |
assert("a" == a.head()) | |
assert("b" == a.tail().head()) | |
assert(a.tail().tail().is_empty()) | |
assert(not a.is_empty()) | |
assert(List().is_empty()) |
This file contains 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
class List(object): | |
def __init__(self, list=None): | |
self.prepend = lambda el: List(lambda selector: selector(el, list)) | |
self.head = lambda: list(lambda h, t: h) | |
self.tail = lambda: List(list(lambda h, t: t)) | |
self.is_empty = lambda: not list | |
a = List().prepend("b").prepend("a") | |
assert("a" == a.head()) | |
assert("b" == a.tail().head()) | |
assert(a.tail().tail().is_empty()) | |
assert(not a.is_empty()) | |
assert(List().is_empty()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment