Skip to content

Fix list to tuple #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions python-ffi/Data/Array.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
def _rangeImpl(start):
def ap(end):
step = 1 if start <= end else -1
return list(_buitins["range"](start, end + step, step))
return tuple(_buitins["range"](start, end + step, step))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_builtins["range"] is very slow. please directly load range instead. You can give an alias to range in global context, like
_glob = global.


return ap


globals()["range"] = _rangeImpl


replicate = lambda count: lambda value: [value for _ in _buitins["range"](count)]
replicate = lambda count: lambda value: tuple([value for _ in _buitins["range"](count)])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not bind lambda to variable, use def.
also, avoid _builtins["range"].



def _mkFromFoldableImpl():
Expand All @@ -37,7 +37,7 @@ def listToArray(lst):
while xs is not None:
result.append(xs.head)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

result_app = result.append
for ...:
   result_app(xs.head)

xs = xs.tail
return result
return tuple(result)

def result(foldr):
def ap(xs):
Expand All @@ -47,14 +47,17 @@ def ap(xs):

return result


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

length = lambda xs: len(xs)

cons = lambda e: lambda l: [e, *l]
cons = lambda e: lambda l: (e, *l)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use def


snoc = lambda xs: lambda x: [*xs, x]
snoc = lambda xs: lambda x: (*xs, x)


def unconsImpl(empty):
Expand Down Expand Up @@ -111,9 +114,11 @@ def findLastIndexImpl(just):
def _insertAtImpl(just, nothing, i, a, l):
if i < 0 or i > len(l):
return nothing
# TODO: discuss performance consideration with alternative implementation
# (*l[i-1], a, *l[i:])
ll = list(l)
ll.insert(i, a)
return just(ll)
return just(tuple(ll))


_insertAt = lambda just: lambda nothing: lambda i: lambda a: lambda l: _insertAtImpl(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use def

Expand All @@ -137,24 +142,24 @@ def _updateAtImpl(just, nothing, i, a, l):
return nothing
ll = list(l)
ll[i] = a
return just(ll)
return just(tuple(ll))


_updateAt = lambda just: lambda nothing: lambda i: lambda a: lambda l: _updateAtImpl(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use def

just, nothing, i, a, l
)

reverse = lambda xs: list(reversed(xs))
reverse = lambda xs: tuple(reversed(xs))


def concat(xss):
result = []
for x in xss:
result.extend(x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bind extend = result.extend outside the loop.
There're a lot of same places you'd follow my above review to change

return result
return tuple(result)


filter = lambda f: lambda xs: list(_buitins["filter"](f, xs))
filter = lambda f: lambda xs: tuple(_buitins["filter"](f, xs))


def partition(f):
Expand All @@ -166,20 +171,22 @@ def result(xs):
yes.append(x)
else:
no.append(x)
return {"yes": yes, "no": no}
return {"yes": tuple(yes), "no": tuple(no)}

return result


sortImpl = lambda f: lambda xs: sorted(
xs, key=functools.cmp_to_key(lambda a, b: f(a)(b))
sortImpl = lambda f: lambda xs: tuple(
sorted(xs, key=functools.cmp_to_key(lambda a, b: f(a)(b)))
)

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

take = lambda n: lambda xs: xs[:max(n, 0)]
drop = lambda n: lambda xs: xs[max(n, 0):]
take = lambda n: lambda xs: xs[: max(n, 0)]
drop = lambda n: lambda xs: xs[max(n, 0) :]

zipWith = lambda f: lambda xs: lambda ys: list(map(lambda t: f(t[0])(t[1]), zip(xs, ys)))
zipWith = lambda f: lambda xs: lambda ys: tuple(
map(lambda t: f(t[0])(t[1]), zip(xs, ys))
)

unsafeIndexImpl = lambda xs: lambda n: xs[n]
2 changes: 1 addition & 1 deletion python-ffi/Data/Array/NonEmpty/Internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def listToArray(lst):
while xs is not emptyList:
arr.append(xs.head)
xs = xs.tail
return arr
return tuple(arr)

def kernel(apply, map_, f):
def buildFrom(x, ys):
Expand Down