Skip to content

Commit 35dbeee

Browse files
authored
Merge pull request #1 from purescript-python/fix-list-to-tuple
Fix list to tuple
2 parents cea2035 + b11c535 commit 35dbeee

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

Diff for: python-ffi/Data/Array.py

+24-17
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
def _rangeImpl(start):
77
def ap(end):
88
step = 1 if start <= end else -1
9-
return list(_buitins["range"](start, end + step, step))
9+
return tuple(_buitins["range"](start, end + step, step))
1010

1111
return ap
1212

1313

1414
globals()["range"] = _rangeImpl
1515

1616

17-
replicate = lambda count: lambda value: [value for _ in _buitins["range"](count)]
17+
replicate = lambda count: lambda value: tuple([value for _ in _buitins["range"](count)])
1818

1919

2020
def _mkFromFoldableImpl():
@@ -37,7 +37,7 @@ def listToArray(lst):
3737
while xs is not None:
3838
result.append(xs.head)
3939
xs = xs.tail
40-
return result
40+
return tuple(result)
4141

4242
def result(foldr):
4343
def ap(xs):
@@ -47,14 +47,17 @@ def ap(xs):
4747

4848
return result
4949

50-
50+
# TODO: discuss performance consideration with alternative implementation
51+
# at least should we use 2-tuple instead of class?
52+
# current implementation is aiming to mimic JS implementation as much as possible
53+
# same problem in NonEmpty.Internal.py
5154
fromFoldableImpl = _mkFromFoldableImpl()
5255

5356
length = lambda xs: len(xs)
5457

55-
cons = lambda e: lambda l: [e, *l]
58+
cons = lambda e: lambda l: (e, *l)
5659

57-
snoc = lambda xs: lambda x: [*xs, x]
60+
snoc = lambda xs: lambda x: (*xs, x)
5861

5962

6063
def unconsImpl(empty):
@@ -111,9 +114,11 @@ def findLastIndexImpl(just):
111114
def _insertAtImpl(just, nothing, i, a, l):
112115
if i < 0 or i > len(l):
113116
return nothing
117+
# TODO: discuss performance consideration with alternative implementation
118+
# (*l[i-1], a, *l[i:])
114119
ll = list(l)
115120
ll.insert(i, a)
116-
return just(ll)
121+
return just(tuple(ll))
117122

118123

119124
_insertAt = lambda just: lambda nothing: lambda i: lambda a: lambda l: _insertAtImpl(
@@ -137,24 +142,24 @@ def _updateAtImpl(just, nothing, i, a, l):
137142
return nothing
138143
ll = list(l)
139144
ll[i] = a
140-
return just(ll)
145+
return just(tuple(ll))
141146

142147

143148
_updateAt = lambda just: lambda nothing: lambda i: lambda a: lambda l: _updateAtImpl(
144149
just, nothing, i, a, l
145150
)
146151

147-
reverse = lambda xs: list(reversed(xs))
152+
reverse = lambda xs: tuple(reversed(xs))
148153

149154

150155
def concat(xss):
151156
result = []
152157
for x in xss:
153158
result.extend(x)
154-
return result
159+
return tuple(result)
155160

156161

157-
filter = lambda f: lambda xs: list(_buitins["filter"](f, xs))
162+
filter = lambda f: lambda xs: tuple(_buitins["filter"](f, xs))
158163

159164

160165
def partition(f):
@@ -166,20 +171,22 @@ def result(xs):
166171
yes.append(x)
167172
else:
168173
no.append(x)
169-
return {"yes": yes, "no": no}
174+
return {"yes": tuple(yes), "no": tuple(no)}
170175

171176
return result
172177

173178

174-
sortImpl = lambda f: lambda xs: sorted(
175-
xs, key=functools.cmp_to_key(lambda a, b: f(a)(b))
179+
sortImpl = lambda f: lambda xs: tuple(
180+
sorted(xs, key=functools.cmp_to_key(lambda a, b: f(a)(b)))
176181
)
177182

178183
slice = lambda s: lambda e: lambda xs: xs[s:e]
179184

180-
take = lambda n: lambda xs: xs[:max(n, 0)]
181-
drop = lambda n: lambda xs: xs[max(n, 0):]
185+
take = lambda n: lambda xs: xs[: max(n, 0)]
186+
drop = lambda n: lambda xs: xs[max(n, 0) :]
182187

183-
zipWith = lambda f: lambda xs: lambda ys: list(map(lambda t: f(t[0])(t[1]), zip(xs, ys)))
188+
zipWith = lambda f: lambda xs: lambda ys: tuple(
189+
map(lambda t: f(t[0])(t[1]), zip(xs, ys))
190+
)
184191

185192
unsafeIndexImpl = lambda xs: lambda n: xs[n]

Diff for: python-ffi/Data/Array/NonEmpty/Internal.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def listToArray(lst):
4242
while xs is not emptyList:
4343
arr.append(xs.head)
4444
xs = xs.tail
45-
return arr
45+
return tuple(arr)
4646

4747
def kernel(apply, map_, f):
4848
def buildFrom(x, ys):

0 commit comments

Comments
 (0)