summary refs log tree commit diff stats
path: root/lib/pure/collections
diff options
context:
space:
mode:
authorGrzegorz Adam Hankiewicz <gradha@imap.cc>2013-03-16 23:07:59 +0100
committerGrzegorz Adam Hankiewicz <gradha@imap.cc>2013-03-17 12:14:27 +0100
commite4c3515baf392024ba5cd38412277febdb35585f (patch)
tree5f646060d4dcd7a090d6da982f72e23623683cde /lib/pure/collections
parent29d0a774e985fa457da14e288f1e2dc634496c8b (diff)
downloadNim-e4c3515baf392024ba5cd38412277febdb35585f.tar.gz
Fixes the dirtyness of the filterIt template. Refs #351.
Diffstat (limited to 'lib/pure/collections')
-rw-r--r--lib/pure/collections/sequtils.nim9
1 files changed, 7 insertions, 2 deletions
diff --git a/lib/pure/collections/sequtils.nim b/lib/pure/collections/sequtils.nim
index 451a1dc7f..c2d638a8c 100644
--- a/lib/pure/collections/sequtils.nim
+++ b/lib/pure/collections/sequtils.nim
@@ -117,7 +117,7 @@ proc filter*[T](seq1: seq[T], pred: proc(item: T): bool {.closure.}): seq[T] =
   ##   assert f2 == @["yellow"]
   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.
   ##
   ## 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
   ##      temperatures = @[-272.15, -2.0, 24.5, 44.31, 99.9, -113.44]
   ##      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 notAcceptable == @[-272.15, 99.9, -113.44]
   var result {.gensym.}: type(seq1) = @[]
-  for it in items(seq1):
+  for internalit in items(seq1):
+    let it {.inject.} = internalit
     if pred: result.add(it)
   result
 
@@ -272,7 +275,9 @@ when isMainModule:
     let
       temperatures = @[-272.15, -2.0, 24.5, 44.31, 99.9, -113.44]
       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 notAcceptable == @[-272.15, 99.9, -113.44]
 
   block: # toSeq test
     let
a id='n192' href='#n192'>192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435