Skip to content

Commit

Permalink
add workaround nim-lang#13417
Browse files Browse the repository at this point in the history
  • Loading branch information
krux02 committed Mar 7, 2020
1 parent 94d4f79 commit 7cede93
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions lib/pure/collections/sequtils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ proc apply*[T](s: var openArray[T], op: proc (x: T): T {.closure.})

for i in 0 ..< s.len: s[i] = op(s[i])

iterator filter*[T](s: openArray[T], pred: proc(x: T): bool {.closure.}): T =
iterator filter*[T](s: seq[T], pred: proc(x: T): bool {.closure.}): T =
## Iterates through a container `s` and yields every item that fulfills the
## predicate `pred` (function that returns a `bool`).
##
Expand All @@ -434,6 +434,12 @@ iterator filter*[T](s: openArray[T], pred: proc(x: T): bool {.closure.}): T =
if pred(s[i]):
yield s[i]

iterator filter*[IX, T](s: array[IX, T], pred: proc(x: T): bool {.closure.}): T =
# does not use `openarray`, because of #13417
for i in 0 ..< s.len:
if pred(s[i]):
yield s[i]

proc filter*[T](s: openArray[T], pred: proc(x: T): bool {.closure.}): seq[T]
{.inline.} =
## Returns a new sequence with all the items of `s` that fulfilled the
Expand Down Expand Up @@ -737,12 +743,7 @@ macro toSeqImpl(arg: typed): untyped =
tmp.add it
tmp

if "identity" in result.repr:
echo result.repr
echo result.treeRepr


macro toSeq(arg: untyped): untyped =
macro toSeq*(arg: untyped): untyped =
## Transforms any iterable (anything that can be iterated over, e.g. with
## a for-loop) into a sequence.
##
Expand Down Expand Up @@ -1365,20 +1366,14 @@ when isMainModule:

block:
# tests https://github.com/nim-lang/Nim/issues/7187
# counter = 0
# let ret = toSeq(@[1, 2, 3].identity().filter(proc (x: int): bool = x < 3))
# doAssert ret == @[1, 2]
# doAssert counter == 1

counter = 0
var tmp: seq[int]
for it in filter(identity(@[1, 2, 3]), proc (x: int): bool = result = x < 3):
tmp.add it
#tmp
echo counter
let ret1 = toSeq(@[1, 2, 3].identity().filter(proc (x: int): bool = x < 3))
doAssert ret1 == @[1, 2]
doAssert counter == 1
counter = 0
let ret2 = toSeq([1, 2, 3].identity().filter(proc (x: int): bool = x < 3))
doAssert ret2 == @[1, 2]
doAssert counter == 1



block: # foldl tests
let
Expand Down

0 comments on commit 7cede93

Please sign in to comment.