Skip to content
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

Make interleave (Y) work on infinite lists #654

Merged
merged 2 commits into from Jan 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions tests/test_complex.py
Expand Up @@ -135,3 +135,8 @@ def test_cartesian_product_infinite_lists():
[3, 1],
[1, 4],
]


def test_interleave():
stack = run_vyxal("⁽›1Ḟ ⁽⇧1Ḟ Y")
assert stack[-1][:6] == [1, 1, 2, 3, 3, 5]
26 changes: 17 additions & 9 deletions vyxal/elements.py
Expand Up @@ -1744,17 +1744,25 @@ def interleave(lhs, rhs, ctx):
rhs = iterable(rhs, ctx=ctx)

@lazylist
def f():
for i in range(max(len(lhs), len(rhs))):
if i < len(lhs):
yield lhs[i]
if i < len(rhs):
yield rhs[i]
def gen():
lhs_iter = iter(lhs)
rhs_iter = iter(rhs)
while True:
try:
yield next(lhs_iter)
except StopIteration:
yield from rhs_iter
break
try:
yield next(rhs_iter)
except StopIteration:
yield from lhs_iter
break

if type(lhs) is type(rhs) is str:
return "".join(f())
return "".join(gen())
else:
return f()
return gen()


def into_two(lhs, ctx):
Expand Down Expand Up @@ -4637,7 +4645,7 @@ def zfiller(lhs, rhs, ctx):
"ḣ": (
"top = iterable(pop(stack, 1, ctx), ctx=ctx);"
" stack.append(head(top, ctx));"
" stack.append(index(top, [1, None], ctx))",
" stack.append(top[1:])",
1,
),
"ḭ": process_element(integer_divide, 2),
Expand Down