From 7cede93dfcd0d77b18f2d7d8db6296b8a0dfb87c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Sat, 7 Mar 2020 18:34:29 +0100 Subject: [PATCH] add workaround #13417 --- lib/pure/collections/sequtils.nim | 33 +++++++++++++------------------ 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim index 7dfe529030a7d..0901cde2b8634 100644 --- a/lib/pure/collections/sequtils.nim +++ b/lib/pure/collections/sequtils.nim @@ -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`). ## @@ -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 @@ -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. ## @@ -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