Created
October 20, 2018 09:39
-
-
Save mcstafford-git/e9feba3f43f3338de2e7776a2297e8ec to your computer and use it in GitHub Desktop.
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
""" | |
Python's lambda is an alternative way to create a function, | |
something like anonymous functions in pl/sql, and javascript. | |
""" | |
def input_times_whatever(n): | |
return lambda a: a * n | |
input_times_1 = input_times_whatever(1) | |
input_times_2 = input_times_whatever(2) | |
input_times_3 = input_times_whatever(3) | |
# print 12 several times | |
for twelve in [ | |
input_times_1(12), | |
input_times_3(4), | |
input_times_whatever(2)(6), | |
map(lambda a: int(a * 0.5), [24, input_times_1(24), input_times_2(12)]), | |
]: | |
if not isinstance(twelve, map): | |
print("direct: {}".format(twelve)) | |
else: | |
for t in twelve: | |
print("mapped: {}".format(t)) | |
list_0_to_5 = ["zero", "one", "two", "three", "four", "five"] | |
list_int_each = list(range(len(list_0_to_5))) | |
list_int_even = [0, 2, 4] | |
if list_int_even == [b for b in filter(lambda x: x % 2 == 0, list_int_each)]: | |
print("x % 2 == 0 -> even numbers") | |
dict_0_to_5 = {k: v for v, k in enumerate(list_0_to_5)} | |
print([r for r in filter(lambda i: i in ["one", "four"], dict_0_to_5)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment