Move asArray macro back to sequtils
This reverts commit 72f653c2daa5c629ed8a57a4f53dcef56432aa26.
This commit is contained in:
parent
bd2f4d1852
commit
b02ecda5a0
2 changed files with 30 additions and 30 deletions
|
|
@ -20,6 +20,8 @@
|
||||||
|
|
||||||
include "system/inclrtl"
|
include "system/inclrtl"
|
||||||
|
|
||||||
|
import macros
|
||||||
|
|
||||||
when not defined(nimhygiene):
|
when not defined(nimhygiene):
|
||||||
{.pragma: dirty.}
|
{.pragma: dirty.}
|
||||||
|
|
||||||
|
|
@ -704,6 +706,28 @@ template newSeqWith*(len: int, init: untyped): untyped =
|
||||||
result[i] = init
|
result[i] = init
|
||||||
result
|
result
|
||||||
|
|
||||||
|
macro asArray*(targetType: untyped, values: typed): untyped =
|
||||||
|
## applies a type conversion to each of the elements in the specified
|
||||||
|
## array literal. Each element is converted to the ``targetType`` type..
|
||||||
|
##
|
||||||
|
## Example:
|
||||||
|
##
|
||||||
|
## .. code-block::
|
||||||
|
## let x = asArray(int, [0.1, 1.2, 2.3, 3.4])
|
||||||
|
## doAssert x is array[4, int]
|
||||||
|
##
|
||||||
|
## Short notation for:
|
||||||
|
##
|
||||||
|
## .. code-block::
|
||||||
|
## let x = [(0.1).int, (1.2).int, (2.3).int, (3.4).int]
|
||||||
|
values.expectKind(nnkBracket)
|
||||||
|
result = newNimNode(nnkBracket, lineInfoFrom=values)
|
||||||
|
for i in 0 ..< len(values):
|
||||||
|
var call = newNimNode(nnkCall, lineInfoFrom=values[i])
|
||||||
|
call.add targetType
|
||||||
|
call.add values[i]
|
||||||
|
result.add call
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
import strutils
|
import strutils
|
||||||
block: # concat test
|
block: # concat test
|
||||||
|
|
@ -992,5 +1016,11 @@ when isMainModule:
|
||||||
seq2D[0][1] = true
|
seq2D[0][1] = true
|
||||||
doAssert seq2D == @[@[true, true], @[true, false], @[false, false], @[false, false]]
|
doAssert seq2D == @[@[true, true], @[true, false], @[false, false], @[false, false]]
|
||||||
|
|
||||||
|
block: # asArray tests
|
||||||
|
let x = asArray(int, [1.2, 2.3, 3.4, 4.5])
|
||||||
|
doAssert x is array[4, int]
|
||||||
|
let y = asArray(`$`, [1.2, 2.3, 3.4, 4.5])
|
||||||
|
doAssert y is array[4, string]
|
||||||
|
|
||||||
when not defined(testing):
|
when not defined(testing):
|
||||||
echo "Finished doc tests"
|
echo "Finished doc tests"
|
||||||
|
|
|
||||||
|
|
@ -198,33 +198,3 @@ macro dump*(x: typed): untyped =
|
||||||
let r = quote do:
|
let r = quote do:
|
||||||
debugEcho `s`, " = ", `x`
|
debugEcho `s`, " = ", `x`
|
||||||
return r
|
return r
|
||||||
|
|
||||||
macro asArray*(targetType: untyped, values: typed): untyped =
|
|
||||||
## applies a type conversion to each of the elements in the specified
|
|
||||||
## array literal. Each element is converted to the ``targetType`` type..
|
|
||||||
##
|
|
||||||
## Example:
|
|
||||||
##
|
|
||||||
## .. code-block::
|
|
||||||
## let x = asArray(int, [0.1, 1.2, 2.3, 3.4])
|
|
||||||
## doAssert x is array[4, int]
|
|
||||||
##
|
|
||||||
## Short notation for:
|
|
||||||
##
|
|
||||||
## .. code-block::
|
|
||||||
## let x = [(0.1).int, (1.2).int, (2.3).int, (3.4).int]
|
|
||||||
values.expectKind(nnkBracket)
|
|
||||||
result = newNimNode(nnkBracket, lineInfoFrom=values)
|
|
||||||
for i in 0 ..< len(values):
|
|
||||||
var call = newNimNode(nnkCall, lineInfoFrom=values[i])
|
|
||||||
call.add targetType
|
|
||||||
call.add values[i]
|
|
||||||
result.add call
|
|
||||||
echo result.repr()
|
|
||||||
|
|
||||||
when isMainModule:
|
|
||||||
block: # asArray tests
|
|
||||||
let x = asArray(int, [1.2, 2.3, 3.4, 4.5])
|
|
||||||
doAssert x is array[4, int]
|
|
||||||
let y = asArray(`$`, [1.2, 2.3, 3.4, 4.5])
|
|
||||||
doAssert y is array[4, string]
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue