Skip to content

Commit

Permalink
fix-nim: add patch for Iterable and list
Browse files Browse the repository at this point in the history
  • Loading branch information
litlighilit committed Apr 28, 2024
1 parent 5c117fd commit a0ee450
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/pylib/builtins/list.nim
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func sorted*[T](self: PyList[T], reverse=false): PyList[T] =
## sorted(list, reverse=False)
newPyList self.asSeq.sorted(order=rev2ord(reverse))

func list*[T](x: openArray[T]): PyList[T] = newPyList @x
# Impl end

# the following does nothing with how PyList is implemented.
Expand Down
10 changes: 9 additions & 1 deletion src/pylib/collections_abc.nim
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@

const SupportIteratorHit = (NimMajor, NimMinor, NimPatch) >= (2, 1, 2)

type
Iterable*[T] = concept self ## Mimic Pythons Iterable. But not checks `iter`
#for value in self: value is T
# Above may cause inner error when JS backend on older Nim
T # see below
typeof(self.items, typeOfIter) is T
when SupportIteratorHit:
iterator items(self): T
else:
#typeof(self.items(), typeOfIter) is T
when defined(js):
typeof(self.items(), typeOfIter) is T
else:
for value in self: value is T
Sized* = concept self
len(self) is int

Expand Down
2 changes: 1 addition & 1 deletion tests/tset.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ test "set":
def fun():
s = pyset([2, 3])
check(len(s.union([1]))==3)
check(len(s.intersection([1,2,3]))==2)
check(len(s.intersection(@[1,2,3]))==2)
fun()
def op():
s = pyset([2, 3])
Expand Down

0 comments on commit a0ee450

Please sign in to comment.