-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
enumerate
; solve warning about Iterable
T
- Loading branch information
1 parent
630b9fd
commit 35b040a
Showing
2 changed files
with
19 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -218,21 +218,32 @@ iterator items*[T](f: Filter[T]): T = | |
for x in f.iter(): | ||
yield x | ||
|
||
iterator enumerate*[T](x: Iterable[T]): (int, T) = | ||
var i = 0 | ||
for v in x: | ||
yield (i, v) | ||
i.inc | ||
|
||
when defined(js): | ||
proc list*[T](iter: Iterable[T]): seq[T]{.error:""" | ||
# XXX: a workaround, at least support some types | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
litlighilit
Author
Member
|
||
func list*[T](arr: openArray[T]): seq[T] = | ||
for i in arr: | ||
result.add i | ||
proc list*[T](iter: not openArray[T] and Iterable[T]): seq[T]{.error:""" | ||
XXX: when for js: | ||
nim compiler will complain about `for i in iter`'s `i` , | ||
saying: internal error: expr(nkBracketExpr, tyUserTypeClassInst) | ||
""".} # TODO: when solved, remove workaround below. | ||
else: | ||
# it has side effects as it calls `items` | ||
proc list*[T](iter: Iterable[T]): seq[T] = | ||
for i in iter: | ||
result.add i | ||
# XXX: a workaround, at least support some types | ||
func list*[T](arr: openArray[T]): seq[T] = | ||
for i in arr: | ||
result.add i | ||
when compiles(iter.len): | ||
result = newSeq[T](iter.len) | ||
for i, v in enumerate(iter): | ||
result[i] = v | ||
else: | ||
for i in iter: | ||
result.add i | ||
|
||
when defined(js): | ||
func filter*[T](comp: NoneType | proc(arg: T): bool, iter: Iterable[T]): Filter[T]{.error: """ | ||
|
@@ -263,7 +274,7 @@ else: | |
let filtered = list(filter(None, values)) | ||
doAssert filtered == @["yes", "no", "why"] | ||
|
||
result = filter[T](pylib.bool, iter) | ||
result = filter[T](toBool, iter) | ||
|
||
|
||
proc input*(prompt = ""): string = | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
It works on the devel branch; probably fixed by nim-lang/Nim#23470