Skip to content

Commit 574c4a3

Browse files
committed
initial commit
1 parent 200aa36 commit 574c4a3

File tree

5 files changed

+198
-1
lines changed

5 files changed

+198
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
.vscode/

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
# purescript-arrays.py
1+
# A Python FFI provider for purescript-arrays.py
2+
Providing FFI files for https://github.com/purescript/purescript-arrays
3+
4+
Available mirrors:
5+
6+
https://github.com/purescript-python/purescript-python-ffi-index
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import functools
2+
def fold1Impl(f):
3+
def result(xs):
4+
acc = xs[0]
5+
lxs = len(xs)
6+
for i in range(1, lxs):
7+
acc = f(acc)(xs[i])
8+
return acc
9+
return result
10+
11+
class Cont:
12+
def __init__(self, fn):
13+
self.fn = fn
14+
15+
class ConsCell:
16+
def __init__(self, head, tail):
17+
self.head = head
18+
self.tail = tail
19+
20+
def traverse1Impl():
21+
22+
emptyList = None
23+
24+
def finalCell(head):
25+
return ConsCell(head, emptyList)
26+
27+
def consList(x):
28+
return lambda xs: ConsCell(x, xs)
29+
30+
def listToArray(lst):
31+
arr = []
32+
xs = lst
33+
while xs != emptyList:
34+
arr.append(xs.head)
35+
xs = xs.tail
36+
return arr
37+
38+
39+
40+
def _result(apply):
41+
def l0(map_):
42+
def l1(f):
43+
def buildFrom(x, ys):
44+
return apply(map(consList)(f(x)))(ys)
45+
def go(acc, currentLen, xs):
46+
if currentLen == 0:
47+
return acc
48+
else:
49+
last = xs[currentLen - 1]
50+
return Cont(lambda: go(buildFrom(last, acc), currentLen - 1, xs))
51+
def _result2(array):
52+
acc = map_(finalCell)(f(array[array.length - 1]))
53+
result = go(acc, array.length - 1, array)
54+
while isinstance(result, Cont):
55+
result = result.fn()
56+
57+
return map_(listToArray)(result)
58+
return _result2
59+
return l1
60+
return l0
61+
return _result(None)
62+

python-ffi/Data/Array/ST.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
empty = lambda: []
2+
3+
4+
5+
def peekImpl(just):
6+
def _peekImpl(nothing, i, xs):
7+
if i >= 0 and (i < len(xs)):
8+
return just(xs[i])
9+
else:
10+
return nothing
11+
12+
return lambda nothing: lambda i: lambda xs: lambda: _peekImpl(nothing, i, xs)
13+
14+
15+
def _pokeImpl(i, a, xs, unit):
16+
if i >= 0 and i < len(xs):
17+
xs[i] = a
18+
19+
20+
poke = lambda i: lambda a: lambda xs: lambda: _pokeImpl(i, a, xs)
21+
22+
23+
popImpl = (
24+
lambda just: lambda nothing: lambda xs: lambda: just(xs.pop())
25+
if len(xs) > 0
26+
else nothing
27+
)
28+
29+
30+
def _pushAllImpl(as_, xs):
31+
xs.extends(as_)
32+
return len(xs)
33+
34+
35+
pushAll = lambda as_: lambda xs: lambda: _pushAllImpl(as_, xs)
36+
37+
38+
# exports.pushAll = function (as) {
39+
# return function (xs) {
40+
# return function () {
41+
# return xs.push.apply(xs, as);
42+
# };
43+
# };
44+
# };
45+
46+
47+
def _shiftImpl(just, nothing, xs):
48+
if len(xs) > 0:
49+
return just(xs[1:])
50+
else:
51+
return nothing
52+
53+
54+
shiftImpl = (
55+
lambda just: lambda nothing: lambda xs: lambda: just(xs.pop(0))
56+
if len(xs) > 0
57+
else nothing
58+
)
59+
60+
61+
def _unshiftAllImpl(as_, xs):
62+
xs[len(as_) :] = xs[:]
63+
xs[: len(as_)] = as_
64+
return len(xs)
65+
66+
67+
unshiftAll = lambda as_: lambda xs: lambda: _unshiftAllImpl(as_, xs)
68+
69+
70+
# exports.unshiftAll = function (as) {
71+
# return function (xs) {
72+
# return function () {
73+
# return xs.unshift.apply(xs, as);
74+
# };
75+
# };
76+
# };
77+
78+
79+
def _spliceImpl(i, howMany, bs, xs):
80+
s = i if i >= 0 else len(xs) + i
81+
removed = xs[s : s + howMany]
82+
xs[s + len(bs) :] = xs[s + len(removed) :]
83+
xs[s : s + len(bs)] = bs[:]
84+
return removed
85+
86+
87+
splice = lambda i: lambda howMany: lambda bs: lambda xs: lambda: _spliceImpl(
88+
i, howMany, bs, xs
89+
)
90+
91+
# exports.splice = function (i) {
92+
# return function (howMany) {
93+
# return function (bs) {
94+
# return function (xs) {
95+
# return function () {
96+
# return xs.splice.apply(xs, [i, howMany].concat(bs));
97+
# };
98+
# };
99+
# };
100+
# };
101+
# };
102+
103+
104+
unsafeFreeze = lambda xs: lambda: xs
105+
106+
unsafeThaw = lambda xs: lambda: xs
107+
108+
copyImpl = lambda xs: lambda: xs[:]
109+
110+
freeze = copyImpl
111+
thaw = copyImpl
112+
113+
114+
sortByImpl = lambda comp: lambda xs: lambda: sorted(xs, cmp=comp)
115+
116+
117+
def toAssocArray(xs):
118+
return [{"value": x, "index": i} for i, x in enumerate(xs)]

python-ffi/Data/Array/ST/Partial.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def peekImpl(i):
2+
return lambda xs: lambda: xs[i]
3+
4+
5+
def _pokeImpl(xs, i, a):
6+
xs[i] = a
7+
8+
9+
def pokeImpl(i):
10+
return lambda a: lambda xs: lambda: _pokeImpl(xs, i, a)

0 commit comments

Comments
 (0)