better setops docs (#16236)
* js module also uses runnableExamples * better setops docs * Update lib/system/setops.nim * better * Update lib/system/setops.nim * Update lib/system/setops.nim * better * better
This commit is contained in:
parent
23447ffdce
commit
b1554cfeb5
1 changed files with 64 additions and 72 deletions
|
|
@ -1,94 +1,86 @@
|
||||||
proc incl*[T](x: var set[T], y: T) {.magic: "Incl", noSideEffect.}
|
func incl*[T](x: var set[T], y: T) {.magic: "Incl".} =
|
||||||
## Includes element ``y`` in the set ``x``.
|
## Includes element `y` in the set `x`.
|
||||||
##
|
##
|
||||||
## This is the same as ``x = x + {y}``, but it might be more efficient.
|
## This is the same as `x = x + {y}`, but it might be more efficient.
|
||||||
##
|
runnableExamples:
|
||||||
## .. code-block:: Nim
|
var a = {1, 3, 5}
|
||||||
## var a = {1, 3, 5}
|
a.incl(2)
|
||||||
## a.incl(2) # a <- {1, 2, 3, 5}
|
assert a == {1, 2, 3, 5}
|
||||||
## a.incl(4) # a <- {1, 2, 3, 4, 5}
|
a.incl(4)
|
||||||
|
assert a == {1, 2, 3, 4, 5}
|
||||||
|
|
||||||
template incl*[T](x: var set[T], y: set[T]) =
|
template incl*[T](x: var set[T], y: set[T]) =
|
||||||
## Includes the set ``y`` in the set ``x``.
|
## Includes the set `y` in the set `x`.
|
||||||
##
|
runnableExamples:
|
||||||
## .. code-block:: Nim
|
var a = {1, 3, 5, 7}
|
||||||
## var a = {1, 3, 5, 7}
|
var b = {4, 5, 6}
|
||||||
## var b = {4, 5, 6}
|
a.incl(b)
|
||||||
## a.incl(b) # a <- {1, 3, 4, 5, 6, 7}
|
assert a == {1, 3, 4, 5, 6, 7}
|
||||||
x = x + y
|
x = x + y
|
||||||
|
|
||||||
proc excl*[T](x: var set[T], y: T) {.magic: "Excl", noSideEffect.}
|
func excl*[T](x: var set[T], y: T) {.magic: "Excl".} =
|
||||||
## Excludes element ``y`` from the set ``x``.
|
## Excludes element `y` from the set `x`.
|
||||||
##
|
##
|
||||||
## This is the same as ``x = x - {y}``, but it might be more efficient.
|
## This is the same as `x = x - {y}`, but it might be more efficient.
|
||||||
##
|
runnableExamples:
|
||||||
## .. code-block:: Nim
|
var b = {2, 3, 5, 6, 12, 545}
|
||||||
## var b = {2, 3, 5, 6, 12, 545}
|
b.excl(5)
|
||||||
## b.excl(5) # b <- {2, 3, 6, 12, 545}
|
assert b == {2, 3, 6, 12, 545}
|
||||||
|
|
||||||
template excl*[T](x: var set[T], y: set[T]) =
|
template excl*[T](x: var set[T], y: set[T]) =
|
||||||
## Excludes the set ``y`` from the set ``x``.
|
## Excludes the set `y` from the set `x`.
|
||||||
##
|
runnableExamples:
|
||||||
## .. code-block:: Nim
|
var a = {1, 3, 5, 7}
|
||||||
## var a = {1, 3, 5, 7}
|
var b = {3, 4, 5}
|
||||||
## var b = {3, 4, 5}
|
a.excl(b)
|
||||||
## a.excl(b) # a <- {1, 7}
|
assert a == {1, 7}
|
||||||
x = x - y
|
x = x - y
|
||||||
|
|
||||||
proc card*[T](x: set[T]): int {.magic: "Card", noSideEffect.}
|
func card*[T](x: set[T]): int {.magic: "Card".} =
|
||||||
## Returns the cardinality of the set ``x``, i.e. the number of elements
|
## Returns the cardinality of the set `x`, i.e. the number of elements
|
||||||
## in the set.
|
## in the set.
|
||||||
##
|
runnableExamples:
|
||||||
## .. code-block:: Nim
|
var a = {1, 3, 5, 7}
|
||||||
## var a = {1, 3, 5, 7}
|
assert card(a) == 4
|
||||||
## echo card(a) # => 4
|
var b = {1, 3, 5, 7, 5}
|
||||||
|
assert card(b) == 4 # repeated 5 doesn't count
|
||||||
|
|
||||||
proc len*[T](x: set[T]): int {.magic: "Card", noSideEffect.}
|
func len*[T](x: set[T]): int {.magic: "Card".}
|
||||||
## An alias for `card(x)`.
|
## An alias for `card(x)`.
|
||||||
|
|
||||||
|
|
||||||
proc `*`*[T](x, y: set[T]): set[T] {.magic: "MulSet", noSideEffect.}
|
func `*`*[T](x, y: set[T]): set[T] {.magic: "MulSet".} =
|
||||||
## This operator computes the intersection of two sets.
|
## This operator computes the intersection of two sets.
|
||||||
##
|
runnableExamples:
|
||||||
## .. code-block:: Nim
|
assert {1, 2, 3} * {2, 3, 4} == {2, 3}
|
||||||
## let
|
|
||||||
## a = {1, 2, 3}
|
|
||||||
## b = {2, 3, 4}
|
|
||||||
## echo a * b # => {2, 3}
|
|
||||||
proc `+`*[T](x, y: set[T]): set[T] {.magic: "PlusSet", noSideEffect.}
|
|
||||||
## This operator computes the union of two sets.
|
|
||||||
##
|
|
||||||
## .. code-block:: Nim
|
|
||||||
## let
|
|
||||||
## a = {1, 2, 3}
|
|
||||||
## b = {2, 3, 4}
|
|
||||||
## echo a + b # => {1, 2, 3, 4}
|
|
||||||
proc `-`*[T](x, y: set[T]): set[T] {.magic: "MinusSet", noSideEffect.}
|
|
||||||
## This operator computes the difference of two sets.
|
|
||||||
##
|
|
||||||
## .. code-block:: Nim
|
|
||||||
## let
|
|
||||||
## a = {1, 2, 3}
|
|
||||||
## b = {2, 3, 4}
|
|
||||||
## echo a - b # => {1}
|
|
||||||
|
|
||||||
proc contains*[T](x: set[T], y: T): bool {.magic: "InSet", noSideEffect.}
|
func `+`*[T](x, y: set[T]): set[T] {.magic: "PlusSet".} =
|
||||||
## One should overload this proc if one wants to overload the ``in`` operator.
|
## This operator computes the union of two sets.
|
||||||
|
runnableExamples:
|
||||||
|
assert {1, 2, 3} + {2, 3, 4} == {1, 2, 3, 4}
|
||||||
|
|
||||||
|
func `-`*[T](x, y: set[T]): set[T] {.magic: "MinusSet".} =
|
||||||
|
## This operator computes the difference of two sets.
|
||||||
|
runnableExamples:
|
||||||
|
assert {1, 2, 3} - {2, 3, 4} == {1}
|
||||||
|
|
||||||
|
func contains*[T](x: set[T], y: T): bool {.magic: "InSet".} =
|
||||||
|
## One should overload this proc if one wants to overload the `in` operator.
|
||||||
##
|
##
|
||||||
## The parameters are in reverse order! ``a in b`` is a template for
|
## The parameters are in reverse order! `a in b` is a template for
|
||||||
## ``contains(b, a)``.
|
## `contains(b, a)`.
|
||||||
## This is because the unification algorithm that Nim uses for overload
|
## This is because the unification algorithm that Nim uses for overload
|
||||||
## resolution works from left to right.
|
## resolution works from left to right.
|
||||||
## But for the ``in`` operator that would be the wrong direction for this
|
## But for the `in` operator that would be the wrong direction for this
|
||||||
## piece of code:
|
## piece of code:
|
||||||
##
|
runnableExamples:
|
||||||
## .. code-block:: Nim
|
var s: set[range['a'..'z']] = {'a'..'c'}
|
||||||
## var s: set[range['a'..'z']] = {'a'..'c'}
|
assert s.contains('c')
|
||||||
## assert s.contains('c')
|
assert 'b' in s
|
||||||
## assert 'b' in s
|
assert 'd' notin s
|
||||||
##
|
assert set['a'..'z'] is set[range['a'..'z']]
|
||||||
## If ``in`` had been declared as ``[T](elem: T, s: set[T])`` then ``T`` would
|
## If `in` had been declared as `[T](elem: T, s: set[T])` then `T` would
|
||||||
## have been bound to ``char``. But ``s`` is not compatible to type
|
## have been bound to `char`. But `s` is not compatible to type
|
||||||
## ``set[char]``! The solution is to bind ``T`` to ``range['a'..'z']``. This
|
## `set[char]`! The solution is to bind `T` to `range['a'..'z']`. This
|
||||||
## is achieved by reversing the parameters for ``contains``; ``in`` then
|
## is achieved by reversing the parameters for `contains`; `in` then
|
||||||
## passes its arguments in reverse order.
|
## passes its arguments in reverse order.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue