Fixes the dirtyness of the filterIt template. Refs #351.
This commit is contained in:
parent
29d0a774e9
commit
e4c3515baf
1 changed files with 7 additions and 2 deletions
|
|
@ -117,7 +117,7 @@ proc filter*[T](seq1: seq[T], pred: proc(item: T): bool {.closure.}): seq[T] =
|
||||||
## assert f2 == @["yellow"]
|
## assert f2 == @["yellow"]
|
||||||
accumulateResult(filter(seq1, pred))
|
accumulateResult(filter(seq1, pred))
|
||||||
|
|
||||||
template filterIt*(seq1, pred: expr): expr {.immediate, dirty.} =
|
template filterIt*(seq1, pred: expr): expr {.immediate.} =
|
||||||
## Returns a new sequence with all the items that fulfilled the predicate.
|
## Returns a new sequence with all the items that fulfilled the predicate.
|
||||||
##
|
##
|
||||||
## Unlike the `proc` version, the predicate needs to be an expression using
|
## Unlike the `proc` version, the predicate needs to be an expression using
|
||||||
|
|
@ -128,9 +128,12 @@ template filterIt*(seq1, pred: expr): expr {.immediate, dirty.} =
|
||||||
## let
|
## let
|
||||||
## temperatures = @[-272.15, -2.0, 24.5, 44.31, 99.9, -113.44]
|
## temperatures = @[-272.15, -2.0, 24.5, 44.31, 99.9, -113.44]
|
||||||
## acceptable = filterIt(temperatures, it < 50 and it > -10)
|
## acceptable = filterIt(temperatures, it < 50 and it > -10)
|
||||||
|
## notAcceptable = filterIt(temperatures, it > 50 or it < -10)
|
||||||
## assert acceptable == @[-2.0, 24.5, 44.31]
|
## assert acceptable == @[-2.0, 24.5, 44.31]
|
||||||
|
## assert notAcceptable == @[-272.15, 99.9, -113.44]
|
||||||
var result {.gensym.}: type(seq1) = @[]
|
var result {.gensym.}: type(seq1) = @[]
|
||||||
for it in items(seq1):
|
for internalit in items(seq1):
|
||||||
|
let it {.inject.} = internalit
|
||||||
if pred: result.add(it)
|
if pred: result.add(it)
|
||||||
result
|
result
|
||||||
|
|
||||||
|
|
@ -272,7 +275,9 @@ when isMainModule:
|
||||||
let
|
let
|
||||||
temperatures = @[-272.15, -2.0, 24.5, 44.31, 99.9, -113.44]
|
temperatures = @[-272.15, -2.0, 24.5, 44.31, 99.9, -113.44]
|
||||||
acceptable = filterIt(temperatures, it < 50 and it > -10)
|
acceptable = filterIt(temperatures, it < 50 and it > -10)
|
||||||
|
notAcceptable = filterIt(temperatures, it > 50 or it < -10)
|
||||||
assert acceptable == @[-2.0, 24.5, 44.31]
|
assert acceptable == @[-2.0, 24.5, 44.31]
|
||||||
|
assert notAcceptable == @[-272.15, 99.9, -113.44]
|
||||||
|
|
||||||
block: # toSeq test
|
block: # toSeq test
|
||||||
let
|
let
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue