iterators

イテレータ

iterator items[T](a: openArray[T]): T {...}{.inline.}
Iterates over each item of a.   ソース 編集
iterator mitems[T](a: var openArray[T]): var T {...}{.inline.}
Iterates over each item of a so that you can modify the yielded value.   ソース 編集
iterator items[IX, T](a: array[IX, T]): T {...}{.inline.}
Iterates over each item of a.   ソース 編集
iterator mitems[IX, T](a: var array[IX, T]): var T {...}{.inline.}
Iterates over each item of a so that you can modify the yielded value.   ソース 編集
iterator items[T](a: set[T]): T {...}{.inline.}
Iterates over each element of a. items iterates only over the elements that are really in the set (and not over the ones the set is able to hold).   ソース 編集
iterator items(a: cstring): char {...}{.inline, raises: [], tags: [].}
Iterates over each item of a.   ソース 編集
iterator mitems(a: var cstring): var char {...}{.inline, raises: [], tags: [].}
Iterates over each item of a so that you can modify the yielded value.   ソース 編集
iterator items(E: typedesc[enum]): E:type
Iterates over the values of the enum E.   ソース 編集
iterator items[T](s: HSlice[T, T]): T
Iterates over the slice s, yielding each value between s.a and s.b (inclusively).   ソース 編集
iterator pairs[T](a: openArray[T]): tuple[key: int, val: T] {...}{.inline.}
Iterates over each item of a. Yields (index, a[index]) pairs.   ソース 編集
iterator mpairs[T](a: var openArray[T]): tuple[key: int, val: var T] {...}{.inline.}
Iterates over each item of a. Yields (index, a[index]) pairs. a[index] can be modified.   ソース 編集
iterator pairs[IX, T](a: array[IX, T]): tuple[key: IX, val: T] {...}{.inline.}
Iterates over each item of a. Yields (index, a[index]) pairs.   ソース 編集
iterator mpairs[IX, T](a: var array[IX, T]): tuple[key: IX, val: var T] {...}{.inline.}
Iterates over each item of a. Yields (index, a[index]) pairs. a[index] can be modified.   ソース 編集
iterator pairs[T](a: seq[T]): tuple[key: int, val: T] {...}{.inline.}
Iterates over each item of a. Yields (index, a[index]) pairs.   ソース 編集
iterator mpairs[T](a: var seq[T]): tuple[key: int, val: var T] {...}{.inline.}
Iterates over each item of a. Yields (index, a[index]) pairs. a[index] can be modified.   ソース 編集
iterator pairs(a: string): tuple[key: int, val: char] {...}{.inline, raises: [], tags: [].}
Iterates over each item of a. Yields (index, a[index]) pairs.   ソース 編集
iterator mpairs(a: var string): tuple[key: int, val: var char] {...}{.inline, raises: [],
    tags: [].}
Iterates over each item of a. Yields (index, a[index]) pairs. a[index] can be modified.   ソース 編集
iterator pairs(a: cstring): tuple[key: int, val: char] {...}{.inline, raises: [], tags: [].}
Iterates over each item of a. Yields (index, a[index]) pairs.   ソース 編集
iterator mpairs(a: var cstring): tuple[key: int, val: var char] {...}{.inline, raises: [],
    tags: [].}
Iterates over each item of a. Yields (index, a[index]) pairs. a[index] can be modified.   ソース 編集
iterator items[T](a: seq[T]): T {...}{.inline.}
Iterates over each item of a.   ソース 編集
iterator mitems[T](a: var seq[T]): var T {...}{.inline.}
Iterates over each item of a so that you can modify the yielded value.   ソース 編集
iterator items(a: string): char {...}{.inline, raises: [], tags: [].}
Iterates over each item of a.   ソース 編集
iterator mitems(a: var string): var char {...}{.inline, raises: [], tags: [].}
Iterates over each item of a so that you can modify the yielded value.   ソース 編集
iterator fields[T: tuple |
    object](x: T): RootObj {...}{.magic: "Fields", noSideEffect.}

Iterates over every field of x.

Warning: This really transforms the 'for' and unrolls the loop. The current implementation also has a bug that affects symbol binding in the loop body.

  ソース 編集
iterator fields[S: tuple |
    object; T: tuple |
    object](x: S; y: T): tuple[a, b: RootObj] {...}{.magic: "Fields", noSideEffect.}

Iterates over every field of x and y.

Warning: This really transforms the 'for' and unrolls the loop. The current implementation also has a bug that affects symbol binding in the loop body.

  ソース 編集
iterator fieldPairs[T: tuple |
    object](x: T): RootObj {...}{.magic: "FieldPairs", noSideEffect.}

Iterates over every field of x returning their name and value.

When you iterate over objects with different field types you have to use the compile time when instead of a runtime if to select the code you want to run for each type. To perform the comparison use the is operator. 用例:

type
  Custom = object
    foo: string
    bar: bool

proc `$`(x: Custom): string =
  result = "Custom:"
  for name, value in x.fieldPairs:
    when value is bool:
      result.add("\n\t" & name & " is " & $value)
    else:
      if value.isNil:
        result.add("\n\t" & name & " (nil)")
      else:
        result.add("\n\t" & name & " '" & value & "'")

Another way to do the same without when is to leave the task of picking the appropriate code to a secondary proc which you overload for each field type and pass the value to.

Warning: This really transforms the 'for' and unrolls the loop. The current implementation also has a bug that affects symbol binding in the loop body.

  ソース 編集
iterator fieldPairs[S: tuple |
    object; T: tuple |
    object](x: S; y: T): tuple[a, b: RootObj] {...}{.magic: "FieldPairs", noSideEffect.}

Iterates over every field of x and y.

Warning: This really transforms the 'for' and unrolls the loop. The current implementation also has a bug that affects symbol binding in the loop body.

  ソース 編集