Code docs for contains, in, is, and of
This commit is contained in:
parent
af4c0851c1
commit
fb547ef76b
1 changed files with 35 additions and 1 deletions
|
|
@ -749,15 +749,49 @@ proc contains*[T](x: set[T], y: T): bool {.magic: "InSet", noSideEffect.}
|
||||||
## passes its arguments in reverse order.
|
## passes its arguments in reverse order.
|
||||||
|
|
||||||
proc contains*[T](s: TSlice[T], value: T): bool {.noSideEffect, inline.} =
|
proc contains*[T](s: TSlice[T], value: T): bool {.noSideEffect, inline.} =
|
||||||
|
## Checks if `value` is withing the range of `s`; returns true iff
|
||||||
|
## `value >= s.a and value <= s.b`
|
||||||
|
##
|
||||||
|
## .. code-block:: Nimrod
|
||||||
|
## assert((1..3).contains(1) == true)
|
||||||
|
## assert((1..3).contains(2) == true)
|
||||||
|
## assert((1..3).contains(4) == false)
|
||||||
result = s.a <= value and value <= s.b
|
result = s.a <= value and value <= s.b
|
||||||
|
|
||||||
template `in` * (x, y: expr): expr {.immediate.} = contains(y, x)
|
template `in` * (x, y: expr): expr {.immediate.} = contains(y, x)
|
||||||
|
## Suger for contains
|
||||||
|
##
|
||||||
|
## .. code-block:: Nimrod
|
||||||
|
## assert(1 in (1..3) == true)
|
||||||
|
## assert(5 in (1..3) == false)
|
||||||
template `notin` * (x, y: expr): expr {.immediate.} = not contains(y, x)
|
template `notin` * (x, y: expr): expr {.immediate.} = not contains(y, x)
|
||||||
|
## Sugar for not containing
|
||||||
|
##
|
||||||
|
## .. code-block:: Nimrod
|
||||||
|
## assert(1 notin (1..3) == false)
|
||||||
|
## assert(5 notin (1..3) == true)
|
||||||
|
|
||||||
proc `is` *[T, S](x: T, y: S): bool {.magic: "Is", noSideEffect.}
|
proc `is` *[T, S](x: T, y: S): bool {.magic: "Is", noSideEffect.}
|
||||||
|
## Checks if T is of the same type as S
|
||||||
|
##
|
||||||
|
## .. code-block:: Nimrod
|
||||||
|
## proc test[T](a: T): int =
|
||||||
|
## when (T is int):
|
||||||
|
## return a
|
||||||
|
## else:
|
||||||
|
## return 0
|
||||||
|
##
|
||||||
|
## assert(test[int](3) == 3)
|
||||||
|
## assert(test[string]("xyz") == 0)
|
||||||
template `isnot` *(x, y: expr): expr {.immediate.} = not (x is y)
|
template `isnot` *(x, y: expr): expr {.immediate.} = not (x is y)
|
||||||
|
## Negated version of `is`. Equivalent to `not(is(x,y))`
|
||||||
proc `of` *[T, S](x: T, y: S): bool {.magic: "Of", noSideEffect.}
|
proc `of` *[T, S](x: T, y: S): bool {.magic: "Of", noSideEffect.}
|
||||||
|
## Checks if `x` has a type of `y`
|
||||||
|
##
|
||||||
|
## .. code-block:: Nimrod
|
||||||
|
## assert(EFloatingPoint is EBase)
|
||||||
|
## assert(EIO is ESystem)
|
||||||
|
## assert(EDivByZero is EBase)
|
||||||
|
|
||||||
proc cmp*[T](x, y: T): int {.procvar.} =
|
proc cmp*[T](x, y: T): int {.procvar.} =
|
||||||
## Generic compare proc. Returns a value < 0 iff x < y, a value > 0 iff x > y
|
## Generic compare proc. Returns a value < 0 iff x < y, a value > 0 iff x > y
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue