Merge branch 'devel' of https://github.com/nim-lang/Nim into devel

This commit is contained in:
Araq
2015-10-13 15:54:33 +02:00
3 changed files with 106 additions and 4 deletions

View File

@@ -397,6 +397,67 @@ template keepItIf*(varSeq: seq, pred: expr) =
inc(pos)
setLen(varSeq, pos)
proc all*[T](seq1: seq[T], pred: proc(item: T): bool {.closure.}): bool =
## Iterates through a sequence and checks if every item fulfills the
## predicate.
##
## Example:
##
## .. code-block::
## let numbers = @[1, 4, 5, 8, 9, 7, 4]
## assert all(numbers, proc (x: int): bool = return x < 10) == true
## assert all(numbers, proc (x: int): bool = return x < 9) == false
for i in seq1:
if not pred(i):
return false
return true
template allIt*(seq1, pred: expr): bool {.immediate.} =
## Checks if every item fulfills the predicate.
##
## Example:
##
## .. code-block::
## let numbers = @[1, 4, 5, 8, 9, 7, 4]
## assert allIt(numbers, it < 10) == true
## assert allIt(numbers, it < 9) == false
var result {.gensym.} = true
for it {.inject.} in items(seq1):
if not pred:
result = false
break
result
proc any*[T](seq1: seq[T], pred: proc(item: T): bool {.closure.}): bool =
## Iterates through a sequence and checks if some item fulfills the
## predicate.
##
## Example:
##
## .. code-block::
## let numbers = @[1, 4, 5, 8, 9, 7, 4]
## assert any(numbers, proc (x: int): bool = return x > 8) == true
## assert any(numbers, proc (x: int): bool = return x > 9) == false
for i in seq1:
if pred(i):
return true
return false
template anyIt*(seq1, pred: expr): bool {.immediate.} =
## Checks if some item fulfills the predicate.
##
## Example:
##
## .. code-block::
## let numbers = @[1, 4, 5, 8, 9, 7, 4]
## assert anyIt(numbers, it > 8) == true
## assert anyIt(numbers, it > 9) == false
var result {.gensym.} = false
for it {.inject.} in items(seq1):
if pred:
result = true
break
result
template toSeq*(iter: expr): expr {.immediate.} =
## Transforms any iterator into a sequence.
@@ -642,6 +703,38 @@ when isMainModule:
keepItIf(candidates, it.len == 3 and it[0] == 'b')
assert candidates == @["bar", "baz"]
block: # any
let
numbers = @[1, 4, 5, 8, 9, 7, 4]
len0seq : seq[int] = @[]
assert any(numbers, proc (x: int): bool = return x > 8) == true
assert any(numbers, proc (x: int): bool = return x > 9) == false
assert any(len0seq, proc (x: int): bool = return true) == false
block: # anyIt
let
numbers = @[1, 4, 5, 8, 9, 7, 4]
len0seq : seq[int] = @[]
assert anyIt(numbers, it > 8) == true
assert anyIt(numbers, it > 9) == false
assert anyIt(len0seq, true) == false
block: # all
let
numbers = @[1, 4, 5, 8, 9, 7, 4]
len0seq : seq[int] = @[]
assert all(numbers, proc (x: int): bool = return x < 10) == true
assert all(numbers, proc (x: int): bool = return x < 9) == false
assert all(len0seq, proc (x: int): bool = return false) == true
block: # allIt
let
numbers = @[1, 4, 5, 8, 9, 7, 4]
len0seq : seq[int] = @[]
assert allIt(numbers, it < 10) == true
assert allIt(numbers, it < 9) == false
assert allIt(len0seq, false) == true
block: # toSeq test
let
numeric = @[1, 2, 3, 4, 5, 6, 7, 8, 9]

View File

@@ -150,6 +150,8 @@ template test*(name: expr, body: stmt): stmt {.immediate, dirty.} =
try:
when declared(testSetupIMPLFlag): testSetupIMPL()
body
when declared(testTeardownIMPLFlag):
defer: testTeardownIMPL()
except:
when not defined(js):
@@ -158,7 +160,6 @@ template test*(name: expr, body: stmt): stmt {.immediate, dirty.} =
fail()
finally:
when declared(testTeardownIMPLFlag): testTeardownIMPL()
testDone name, testStatusIMPL
proc checkpoint*(msg: string) =

View File

@@ -81,19 +81,27 @@ All rights reserved.
| | Linux | Windows | Mac |
| ------ | ----- | ------- | --- |
| x86 | ![linux-x86][linux-x86-img] | ![windows-x86][windows-x86-img] |
| x86_64 | ![linux-x86_64][linux-x86_64-img] | ![windows-x86_64][windows-x86_64-img] | ![mac-x86_64][mac-x86_64-img] |
| arm | ![linux-armv5][linux-arm5-img]<br/> ![linux-armv6][linux-arm6-img]<br/> ![linux-armv7][linux-arm7-img]
| x86 | [![linux-x86][linux-x86-img]][linux-x86] | [![windows-x86][windows-x86-img]][windows-x86] |
| x86_64 | [![linux-x86_64][linux-x86_64-img]][linux-x86_64] | [![windows-x86_64][windows-x86_64-img]][windows-x86_64] | [![mac-x86_64][mac-x86_64-img]][mac-x86_64] |
| arm | [![linux-armv5][linux-arm5-img]][linux-arm5]<br/> [![linux-armv6][linux-arm6-img]][linux-arm6]<br/> [![linux-armv7][linux-arm7-img]][linux-arm7]
[linux-x86]: http://buildbot.nim-lang.org/builders/linux-x32-builder
[linux-x86-img]: http://buildbot.nim-lang.org/buildstatusimage?builder=linux-x32-builder
[linux-x86_64]: http://buildbot.nim-lang.org/builders/linux-x64-builder
[linux-x86_64-img]: http://buildbot.nim-lang.org/buildstatusimage?builder=linux-x64-builder
[linux-arm5]: http://buildbot.nim-lang.org/builders/linux-arm5-builder
[linux-arm5-img]: http://buildbot.nim-lang.org/buildstatusimage?builder=linux-arm5-builder
[linux-arm6]: http://buildbot.nim-lang.org/builders/linux-arm6-builder
[linux-arm6-img]: http://buildbot.nim-lang.org/buildstatusimage?builder=linux-arm6-builder
[linux-arm7]: http://buildbot.nim-lang.org/builders/linux-arm7-builder
[linux-arm7-img]: http://buildbot.nim-lang.org/buildstatusimage?builder=linux-arm7-builder
[windows-x86]: http://buildbot.nim-lang.org/builders/windows-x32-builder
[windows-x86-img]: http://buildbot.nim-lang.org/buildstatusimage?builder=windows-x32-builder
[windows-x86_64]: http://buildbot.nim-lang.org/builders/windows-x64-builder
[windows-x86_64-img]: http://buildbot.nim-lang.org/buildstatusimage?builder=windows-x64-builder
[mac-x86_64]: http://buildbot.nim-lang.org/builders/mac-x64-builder
[mac-x86_64-img]: http://buildbot.nim-lang.org/buildstatusimage?builder=mac-x64-builder
[waterfall]: http://buildbot.nim-lang.org/waterfall