Skip to content

Commit 98f0079

Browse files
committed
BREAKING CHANGE - Swap index and at
Fix index when not in list
1 parent 641ef61 commit 98f0079

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

node/index.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ class Index(Node):
77
args = 2
88
results = 1
99

10-
@Node.test_func([1,"tes"], ["e"])
11-
@Node.test_func([-1,[1,2,3]], [3])
12-
def at(self, a:int, b: Node.indexable):
13-
"""b[a]"""
14-
return[b[a]]
10+
@Node.test_func(["tes",1], ["e"])
11+
@Node.test_func([[1,2,3],-1], [3])
12+
def at(self, a: Node.indexable, b:int):
13+
"""a[b]"""
14+
return[a[b]]
1515

1616

1717
@Node.test_func([2, {1:2,2:3}], [3])
@@ -20,16 +20,20 @@ def at(self, a:int, b: Node.indexable):
2020
def dict_at(self, a, b:dict):
2121
return[b[a]]
2222

23-
@Node.test_func([[1,2,3],1], [0])
24-
@Node.test_func([[1,2,3],3], [2])
25-
@Node.test_func(["hello","e"], [1])
26-
@Node.test_func(["hello","?"], [-1])
23+
@Node.test_func([1, [1,2,3]], [0])
24+
@Node.test_func([3, [1,2,3]], [2])
25+
@Node.test_func([4, [1,2,3]], [-1])
26+
@Node.test_func(["e", "hello"], [1])
27+
@Node.test_func(["?", "hello"], [-1])
2728
@Node.prefer
28-
def index(self, a: Node.indexable, b):
29-
"""a.index(b)"""
30-
if isinstance(a, str):
31-
return a.find(b)
32-
return a.index(b)
29+
def index(self, a, b: Node.indexable):
30+
"""b.index(a)"""
31+
if isinstance(b, str):
32+
return b.find(a)
33+
try:
34+
return b.index(a)
35+
except ValueError:
36+
return -1
3337

3438
@Node.test_func([2, 0], [3])
3539
@Node.test_func([1, 2], [5])

0 commit comments

Comments
 (0)