mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-03 03:32:32 +00:00
This commit is contained in:
@@ -1014,27 +1014,27 @@ template applyIt*(varSeq, op: untyped) =
|
||||
|
||||
|
||||
template newSeqWith*(len: int, init: untyped): untyped =
|
||||
## Creates a new sequence of length `len`, calling `init` to initialize
|
||||
## each value of the sequence.
|
||||
##
|
||||
## Useful for creating "2D" sequences - sequences containing other sequences
|
||||
## or to populate fields of the created sequence.
|
||||
## Creates a new `seq` of length `len`, calling `init` to initialize
|
||||
## each value of the seq.
|
||||
##
|
||||
## Useful for creating "2D" seqs - seqs containing other seqs
|
||||
## or to populate fields of the created seq.
|
||||
runnableExamples:
|
||||
## Creates a sequence containing 5 bool sequences, each of length of 3.
|
||||
## Creates a seq containing 5 bool seqs, each of length of 3.
|
||||
var seq2D = newSeqWith(5, newSeq[bool](3))
|
||||
assert seq2D.len == 5
|
||||
assert seq2D[0].len == 3
|
||||
assert seq2D[4][2] == false
|
||||
|
||||
## Creates a sequence of 20 random numbers from 1 to 10
|
||||
import random
|
||||
var seqRand = newSeqWith(20, rand(10))
|
||||
## Creates a seq with random numbers
|
||||
import std/random
|
||||
var seqRand = newSeqWith(20, rand(1.0))
|
||||
assert seqRand[0] != seqRand[1]
|
||||
|
||||
var result = newSeq[typeof(init)](len)
|
||||
for i in 0 ..< len:
|
||||
result[i] = init
|
||||
result
|
||||
move(result) # refs bug #7295
|
||||
|
||||
func mapLitsImpl(constructor: NimNode; op: NimNode; nested: bool;
|
||||
filter = nnkLiterals): NimNode =
|
||||
|
||||
Reference in New Issue
Block a user