-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist_ops.py
52 lines (35 loc) · 872 Bytes
/
list_ops.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def append(*lists):
return [e for l in lists for e in l]
def concat(lists):
return append(*lists)
def filter_clone(function, xs):
return [e for e in xs if function(e)]
def length(xs):
count = 0
for _ in xs:
count += 1
return count
def map_clone(function, xs):
return [function(e) for e in xs]
def foldl(function, xs, acc):
print(acc)
result = acc
for value in xs:
acc = function(acc, value)
return acc
def foldr(function, xs, acc):
return foldl(lambda x, y: function(y, x), reverse(xs), acc)
def reverse(xs):
return [e for e in iterate_reverse(xs)]
def iterate_reverse(xs):
i = -1
while True:
try:
yield xs[i]
i -= 1
except IndexError:
return
a = [1,2,3,4]
other = [0]
print(foldl(lambda acc, v: acc+a, a, other))
print(other)