-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInternal.py
71 lines (51 loc) · 1.58 KB
/
Internal.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import functools
def fold1Impl(f):
def result(xs):
acc = xs[0]
lxs = len(xs)
for i in range(1, lxs):
acc = f(acc)(xs[i])
return acc
# return functools.reduce(lambda acc, cur: f(acc)(cur), xs)
return result
class Cont:
def __init__(self, fn):
self.fn = fn
def _mkTraverselImpl():
class Cont:
def __init__(self, fn):
self.fn = fn
class ConsCell:
def __init__(self, head, tail):
self.head = head
self.tail = tail
emptyList = None
def finalCell(head):
return ConsCell(head, emptyList)
def consList(x):
return lambda xs: ConsCell(x, xs)
def listToArray(lst):
arr = []
xs = lst
while xs is not emptyList:
arr.append(xs.head)
xs = xs.tail
return arr
def kernel(apply, map_, f):
def buildFrom(x, ys):
return apply(map_(consList)(f(x)))(ys)
def go(acc, currentLen, xs):
if currentLen == 0:
return acc
else:
last = xs[currentLen - 1]
return Cont(lambda: go(buildFrom(last, acc), currentLen - 1, xs))
def result(array):
acc = map_(finalCell)(f(array[-1]))
result = go(acc, len(array) - 1, array)
while isinstance(result, Cont):
result = result.fn()
return map_(listToArray)(result)
return result
return lambda apply: lambda map_: lambda f: kernel(apply, map_, f)
traverse1Impl = _mkTraverselImpl()