Skip to content

Commit

Permalink
Merge pull request #654 from Vyxal/fix-interleave
Browse files Browse the repository at this point in the history
Make interleave (Y) work on infinite lists
  • Loading branch information
lyxal committed Jan 20, 2022
2 parents 8e01ce2 + abd764a commit 1fc676b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
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

0 comments on commit 1fc676b

Please sign in to comment.