[docs minor] better comparisons docs (#16201)

This commit is contained in:
flywind 2020-12-02 16:34:34 +08:00 • committed by GitHub
commit e875f27ee6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,24 +1,24 @@
# comparison operators: # comparison operators:
proc `==`*[Enum: enum](x, y: Enum): bool {.magic: "EqEnum", noSideEffect.} proc `==`*[Enum: enum](x, y: Enum): bool {.magic: "EqEnum", noSideEffect.} =
## Checks whether values within the *same enum* have the same underlying value. ## Checks whether values within the *same enum* have the same underlying value.
## runnableExamples:
## .. code-block:: Nim type
## type Enum1 = enum
## Enum1 = enum Field1 = 3, Field2
## Field1 = 3, Field2 Enum2 = enum
## Enum2 = enum Place1, Place2 = 3
## Place1, Place2 = 3 var
## var e1 = Field1
## e1 = Field1 e2 = Enum1(Place2)
## e2 = Enum1(Place2) assert e1 == e2
## echo (e1 == e2) # true assert not compiles(e1 == Place2) # raises error
## echo (e1 == Place2) # raises error proc `==`*(x, y: pointer): bool {.magic: "EqRef", noSideEffect.} =
proc `==`*(x, y: pointer): bool {.magic: "EqRef", noSideEffect.} ## Checks for equality between two `pointer` variables.
## .. code-block:: Nim runnableExamples:
## var # this is a wildly dangerous example var # this is a wildly dangerous example
## a = cast[pointer](0) a = cast[pointer](0)
## b = cast[pointer](nil) b = cast[pointer](nil)
## echo (a == b) # true due to the special meaning of `nil`/0 as a pointer assert a == b # true due to the special meaning of `nil`/0 as a pointer
proc `==`*(x, y: string): bool {.magic: "EqStr", noSideEffect.} proc `==`*(x, y: string): bool {.magic: "EqStr", noSideEffect.}
## Checks for equality between two `string` variables. ## Checks for equality between two `string` variables.
@ -26,13 +26,11 @@ proc `==`*(x, y: char): bool {.magic: "EqCh", noSideEffect.}
## Checks for equality between two `char` variables. ## Checks for equality between two `char` variables.
proc `==`*(x, y: bool): bool {.magic: "EqB", noSideEffect.} proc `==`*(x, y: bool): bool {.magic: "EqB", noSideEffect.}
## Checks for equality between two `bool` variables. ## Checks for equality between two `bool` variables.
proc `==`*[T](x, y: set[T]): bool {.magic: "EqSet", noSideEffect.} proc `==`*[T](x, y: set[T]): bool {.magic: "EqSet", noSideEffect.} =
## Checks for equality between two variables of type `set`. ## Checks for equality between two variables of type `set`.
## runnableExamples:
## .. code-block:: Nim assert {1, 2, 2, 3} == {1, 2, 3} # duplication in sets is ignored
## var a = {1, 2, 2, 3} # duplication in sets is ignored
## var b = {1, 2, 3}
## echo (a == b) # true
proc `==`*[T](x, y: ref T): bool {.magic: "EqRef", noSideEffect.} proc `==`*[T](x, y: ref T): bool {.magic: "EqRef", noSideEffect.}
## Checks that two `ref` variables refer to the same item. ## Checks that two `ref` variables refer to the same item.
proc `==`*[T](x, y: ptr T): bool {.magic: "EqRef", noSideEffect.} proc `==`*[T](x, y: ptr T): bool {.magic: "EqRef", noSideEffect.}
@ -41,87 +39,87 @@ proc `==`*[T: proc](x, y: T): bool {.magic: "EqProc", noSideEffect.}
## Checks that two `proc` variables refer to the same procedure. ## Checks that two `proc` variables refer to the same procedure.
proc `<=`*[Enum: enum](x, y: Enum): bool {.magic: "LeEnum", noSideEffect.} proc `<=`*[Enum: enum](x, y: Enum): bool {.magic: "LeEnum", noSideEffect.}
proc `<=`*(x, y: string): bool {.magic: "LeStr", noSideEffect.} proc `<=`*(x, y: string): bool {.magic: "LeStr", noSideEffect.} =
## Compares two strings and returns true if `x` is lexicographically ## Compares two strings and returns true if `x` is lexicographically
## before `y` (uppercase letters come before lowercase letters). ## before `y` (uppercase letters come before lowercase letters).
## runnableExamples:
## .. code-block:: Nim let
## let a = "abc"
## a = "abc" b = "abd"
## b = "abd" c = "ZZZ"
## c = "ZZZ" assert a <= b
## assert a <= b assert a <= a
## assert a <= a assert not (a <= c)
## assert (a <= c) == false
proc `<=`*(x, y: char): bool {.magic: "LeCh", noSideEffect.} proc `<=`*(x, y: char): bool {.magic: "LeCh", noSideEffect.} =
## Compares two chars and returns true if `x` is lexicographically ## Compares two chars and returns true if `x` is lexicographically
## before `y` (uppercase letters come before lowercase letters). ## before `y` (uppercase letters come before lowercase letters).
## runnableExamples:
## .. code-block:: Nim let
## let a = 'a'
## a = 'a' b = 'b'
## b = 'b' c = 'Z'
## c = 'Z' assert a <= b
## assert a <= b assert a <= a
## assert a <= a assert not (a <= c)
## assert (a <= c) == false
proc `<=`*[T](x, y: set[T]): bool {.magic: "LeSet", noSideEffect.} proc `<=`*[T](x, y: set[T]): bool {.magic: "LeSet", noSideEffect.} =
## Returns true if `x` is a subset of `y`. ## Returns true if `x` is a subset of `y`.
## ##
## A subset `x` has all of its members in `y` and `y` doesn't necessarily ## A subset `x` has all of its members in `y` and `y` doesn't necessarily
## have more members than `x`. That is, `x` can be equal to `y`. ## have more members than `x`. That is, `x` can be equal to `y`.
## runnableExamples:
## .. code-block:: Nim let
## let a = {3, 5}
## a = {3, 5} b = {1, 3, 5, 7}
## b = {1, 3, 5, 7} c = {2}
## c = {2} assert a <= b
## assert a <= b assert a <= a
## assert a <= a assert not (a <= c)
## assert (a <= c) == false
proc `<=`*(x, y: bool): bool {.magic: "LeB", noSideEffect.} proc `<=`*(x, y: bool): bool {.magic: "LeB", noSideEffect.}
proc `<=`*[T](x, y: ref T): bool {.magic: "LePtr", noSideEffect.} proc `<=`*[T](x, y: ref T): bool {.magic: "LePtr", noSideEffect.}
proc `<=`*(x, y: pointer): bool {.magic: "LePtr", noSideEffect.} proc `<=`*(x, y: pointer): bool {.magic: "LePtr", noSideEffect.}
proc `<`*[Enum: enum](x, y: Enum): bool {.magic: "LtEnum", noSideEffect.} proc `<`*[Enum: enum](x, y: Enum): bool {.magic: "LtEnum", noSideEffect.}
proc `<`*(x, y: string): bool {.magic: "LtStr", noSideEffect.} proc `<`*(x, y: string): bool {.magic: "LtStr", noSideEffect.} =
## Compares two strings and returns true if `x` is lexicographically ## Compares two strings and returns true if `x` is lexicographically
## before `y` (uppercase letters come before lowercase letters). ## before `y` (uppercase letters come before lowercase letters).
## runnableExamples:
## .. code-block:: Nim let
## let a = "abc"
## a = "abc" b = "abd"
## b = "abd" c = "ZZZ"
## c = "ZZZ" assert a < b
## assert a < b assert not (a < a)
## assert (a < a) == false assert not (a < c)
## assert (a < c) == false
proc `<`*(x, y: char): bool {.magic: "LtCh", noSideEffect.} proc `<`*(x, y: char): bool {.magic: "LtCh", noSideEffect.} =
## Compares two chars and returns true if `x` is lexicographically ## Compares two chars and returns true if `x` is lexicographically
## before `y` (uppercase letters come before lowercase letters). ## before `y` (uppercase letters come before lowercase letters).
## runnableExamples:
## .. code-block:: Nim let
## let a = 'a'
## a = 'a' b = 'b'
## b = 'b' c = 'Z'
## c = 'Z' assert a < b
## assert a < b assert not (a < a)
## assert (a < a) == false assert not (a < c)
## assert (a < c) == false
proc `<`*[T](x, y: set[T]): bool {.magic: "LtSet", noSideEffect.} proc `<`*[T](x, y: set[T]): bool {.magic: "LtSet", noSideEffect.} =
## Returns true if `x` is a strict or proper subset of `y`. ## Returns true if `x` is a strict or proper subset of `y`.
## ##
## A strict or proper subset `x` has all of its members in `y` but `y` has ## A strict or proper subset `x` has all of its members in `y` but `y` has
## more elements than `y`. ## more elements than `y`.
## runnableExamples:
## .. code-block:: Nim let
## let a = {3, 5}
## a = {3, 5} b = {1, 3, 5, 7}
## b = {1, 3, 5, 7} c = {2}
## c = {2} assert a < b
## assert a < b assert not (a < a)
## assert (a < a) == false assert not (a < c)
## assert (a < c) == false
proc `<`*(x, y: bool): bool {.magic: "LtB", noSideEffect.} proc `<`*(x, y: bool): bool {.magic: "LtB", noSideEffect.}
proc `<`*[T](x, y: ref T): bool {.magic: "LtPtr", noSideEffect.} proc `<`*[T](x, y: ref T): bool {.magic: "LtPtr", noSideEffect.}
proc `<`*[T](x, y: ptr T): bool {.magic: "LtPtr", noSideEffect.} proc `<`*[T](x, y: ptr T): bool {.magic: "LtPtr", noSideEffect.}
@ -252,11 +250,12 @@ proc max*[T](x: openArray[T]): T =
proc clamp*[T](x, a, b: T): T = proc clamp*[T](x, a, b: T): T =
## Limits the value ``x`` within the interval [a, b]. ## Limits the value `x` within the interval [a, b].
## ## This proc is equivalent to `max(a, min(b, x))`.
## .. code-block:: Nim runnableExamples:
## assert((1.4).clamp(0.0, 1.0) == 1.0) assert (1.4).clamp(0.0, 1.0) == 1.0
## assert((0.5).clamp(0.0, 1.0) == 0.5) assert (0.5).clamp(0.0, 1.0) == 0.5
assert 4.clamp(1, 3) == max(1, min(3, 4))
if x < a: return a if x < a: return a
if x > b: return b if x > b: return b
return x return x