Skip to content

Commit

Permalink
Merge pull request #1145 from Vyxal/fix-index
Browse files Browse the repository at this point in the history
Fix the mysteriously failing `i`
  • Loading branch information
Steffan153 committed Jun 23, 2022
2 parents 052bc5d + a7bcff7 commit c0c82b4
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 35 deletions.
2 changes: 2 additions & 0 deletions documents/knowledge/elements.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions documents/knowledge/elements.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions documents/knowledge/elements.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1185,21 +1185,23 @@
arity: 2
overloads:
any-num: a[b] (index)
num-any: b[a] (index)
str-str: enclose b in a (b[0:len(b)//2] + a + b[len(b)//2:])
any-[x]: a[:b] (0 to bth item of a)
any-[x,y]: a[x:y] (x to yth item of a)
any-[x,y,m]: a[x:y:m] (x to yth item of a, taking every mth)
vectorise: false
tests:
- '["abc",1] : "b"'
- '["abc",[1,2]] : "b"'
- '["joemama","biden''s"] : "joebiden''smama"'
- "[[1,2,3], 0] : 1"
- "[[1,2,3], -1] : 3"
- "[[1,2,3], -0.1] : 1"
- "[[1,2,3], 1.9] : 2"
- "[[2,3,4,5], [2]] : [2,3]"
- "[[1,3,5,7],[1,3]] : [3,5]"
- "[1357,[1,3]] : 35"
- "[4,[1,3,4]] : 3"
- "[[1,2,3,4,5,6,7,8,9,10],[1,8,2]] : [2,4,6,8]"
- "[12345678910,[1,8,2]] : 2468"

- element: "j"
name: Join
Expand Down
2 changes: 2 additions & 0 deletions static/parsed_yaml.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 19 additions & 19 deletions tests/test_elements.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 8 additions & 13 deletions vyxal/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -2034,11 +2034,11 @@ def increment_until_false(lhs, rhs, ctx):
def index(lhs, rhs, ctx):
"""Element i
(any, num) -> a[b] (Index)
(num, any) -> b[a] (Index)
(str, str) -> enclose b in a # b[0:len(b)//2] + a + b[len(b)//2:]
(any, [x]) -> a[:b] (0 to bth item of a)
(any, [x,y]) -> a[x:y] (x to yth item of a)
(any, [x,y,m]) -> a[x:y:m] (x to yth item of a, taking every mth)
(num, any) -> b[a] (Index)
"""
ts = vy_type(lhs, rhs)
lhs = deep_copy(lhs)
Expand All @@ -2049,29 +2049,24 @@ def index(lhs, rhs, ctx):
elif ts == (LazyList, NUMBER_TYPE):
return lhs[int(rhs)]

elif ts[-1] == NUMBER_TYPE:
if len(iterable(lhs)):
return iterable(lhs, ctx=ctx)[int(rhs) % len(iterable(lhs, ctx))]
elif ts[1] == NUMBER_TYPE:
lhs = iterable(lhs, ctx=ctx)
if lhs:
return lhs[int(rhs) % len(lhs)]
else:
return "" if ts[0] is str else 0

elif ts[0] == NUMBER_TYPE:
return index(rhs, lhs, ctx)

elif ts[-1] == str:
elif ts[1] == str:
return vectorise(index, lhs, rhs, ctx=ctx)

else:
originally_string = False
if isinstance(lhs, str):
lhs = LazyList(list(lhs))
originally_string = True
temp = iterable(lhs, ctx=ctx)[
assert len(rhs) <= 3
return lhs[
slice(*[None if vy_type(v) != NUMBER_TYPE else int(v) for v in rhs])
]
if originally_string:
return "".join(temp)
return temp


def index_indices_or_cycle(lhs, rhs, ctx):
Expand Down

0 comments on commit c0c82b4

Please sign in to comment.