Tiny since cleanup (#13286)
This commit is contained in:
parent
be795bbf1a
commit
fb94836bc7
4 changed files with 111 additions and 111 deletions
|
|
@ -2357,7 +2357,7 @@ proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault,
|
||||||
# but nothing else is possible:
|
# but nothing else is possible:
|
||||||
if buf[i] in {'.', ','}: result[i] = decimalSep
|
if buf[i] in {'.', ','}: result[i] = decimalSep
|
||||||
else: result[i] = buf[i]
|
else: result[i] = buf[i]
|
||||||
when (NimMajor, NimMinor) >= (1, 1):
|
since (1, 1):
|
||||||
# remove trailing dot, compatible with Python's formatter and JS backend
|
# remove trailing dot, compatible with Python's formatter and JS backend
|
||||||
if result[^1] == decimalSep:
|
if result[^1] == decimalSep:
|
||||||
result.setLen(len(result)-1)
|
result.setLen(len(result)-1)
|
||||||
|
|
|
||||||
|
|
@ -189,124 +189,124 @@ macro capture*(locals: openArray[typed], body: untyped): untyped {.since: (1, 1)
|
||||||
result.add(newProc(newEmptyNode(), params, body, nnkProcDef))
|
result.add(newProc(newEmptyNode(), params, body, nnkProcDef))
|
||||||
for arg in locals: result.add(arg)
|
for arg in locals: result.add(arg)
|
||||||
|
|
||||||
when (NimMajor, NimMinor) >= (1, 1):
|
macro outplace*[T](arg: T, call: untyped; inplaceArgPosition: static[int] = 1): T {.since: (1, 1).} =
|
||||||
macro outplace*[T](arg: T, call: untyped; inplaceArgPosition: static[int] = 1): T =
|
## Turns an `in-place`:idx: algorithm into one that works on
|
||||||
## Turns an `in-place`:idx: algorithm into one that works on
|
## a copy and returns this copy. The second parameter is the
|
||||||
## a copy and returns this copy. The second parameter is the
|
## index of the calling expression that is replaced by a copy
|
||||||
## index of the calling expression that is replaced by a copy
|
## of this expression.
|
||||||
## of this expression.
|
## **Since**: Version 1.2.
|
||||||
## **Since**: Version 1.2.
|
runnableExamples:
|
||||||
runnableExamples:
|
import algorithm
|
||||||
import algorithm
|
|
||||||
|
|
||||||
var a = @[1, 2, 3, 4, 5, 6, 7, 8, 9]
|
var a = @[1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||||
doAssert a.outplace(sort()) == sorted(a)
|
doAssert a.outplace(sort()) == sorted(a)
|
||||||
#Chaining:
|
#Chaining:
|
||||||
var aCopy = a
|
var aCopy = a
|
||||||
aCopy.insert(10)
|
aCopy.insert(10)
|
||||||
|
|
||||||
doAssert a.outplace(insert(10)).outplace(sort()) == sorted(aCopy)
|
doAssert a.outplace(insert(10)).outplace(sort()) == sorted(aCopy)
|
||||||
|
|
||||||
expectKind call, nnkCallKinds
|
expectKind call, nnkCallKinds
|
||||||
let tmp = genSym(nskVar, "outplaceResult")
|
let tmp = genSym(nskVar, "outplaceResult")
|
||||||
var callsons = call[0..^1]
|
var callsons = call[0..^1]
|
||||||
callsons.insert(tmp, inplaceArgPosition)
|
callsons.insert(tmp, inplaceArgPosition)
|
||||||
result = newTree(nnkStmtListExpr,
|
result = newTree(nnkStmtListExpr,
|
||||||
newVarStmt(tmp, arg),
|
newVarStmt(tmp, arg),
|
||||||
copyNimNode(call).add callsons,
|
copyNimNode(call).add callsons,
|
||||||
tmp)
|
tmp)
|
||||||
|
|
||||||
proc transLastStmt(n, res, bracketExpr: NimNode): (NimNode, NimNode, NimNode) =
|
proc transLastStmt(n, res, bracketExpr: NimNode): (NimNode, NimNode, NimNode) {.since: (1, 1).} =
|
||||||
# Looks for the last statement of the last statement, etc...
|
# Looks for the last statement of the last statement, etc...
|
||||||
case n.kind
|
case n.kind
|
||||||
of nnkIfExpr, nnkIfStmt, nnkTryStmt, nnkCaseStmt:
|
of nnkIfExpr, nnkIfStmt, nnkTryStmt, nnkCaseStmt:
|
||||||
result[0] = copyNimTree(n)
|
result[0] = copyNimTree(n)
|
||||||
result[1] = copyNimTree(n)
|
result[1] = copyNimTree(n)
|
||||||
result[2] = copyNimTree(n)
|
result[2] = copyNimTree(n)
|
||||||
for i in ord(n.kind == nnkCaseStmt)..<n.len:
|
for i in ord(n.kind == nnkCaseStmt)..<n.len:
|
||||||
(result[0][i], result[1][^1], result[2][^1]) = transLastStmt(n[i], res, bracketExpr)
|
(result[0][i], result[1][^1], result[2][^1]) = transLastStmt(n[i], res, bracketExpr)
|
||||||
of nnkStmtList, nnkStmtListExpr, nnkBlockStmt, nnkBlockExpr, nnkWhileStmt,
|
of nnkStmtList, nnkStmtListExpr, nnkBlockStmt, nnkBlockExpr, nnkWhileStmt,
|
||||||
nnkForStmt, nnkElifBranch, nnkElse, nnkElifExpr, nnkOfBranch, nnkExceptBranch:
|
nnkForStmt, nnkElifBranch, nnkElse, nnkElifExpr, nnkOfBranch, nnkExceptBranch:
|
||||||
result[0] = copyNimTree(n)
|
result[0] = copyNimTree(n)
|
||||||
result[1] = copyNimTree(n)
|
result[1] = copyNimTree(n)
|
||||||
result[2] = copyNimTree(n)
|
result[2] = copyNimTree(n)
|
||||||
if n.len >= 1:
|
if n.len >= 1:
|
||||||
(result[0][^1], result[1][^1], result[2][^1]) = transLastStmt(n[^1], res, bracketExpr)
|
(result[0][^1], result[1][^1], result[2][^1]) = transLastStmt(n[^1], res, bracketExpr)
|
||||||
of nnkTableConstr:
|
of nnkTableConstr:
|
||||||
result[1] = n[0][0]
|
result[1] = n[0][0]
|
||||||
result[2] = n[0][1]
|
result[2] = n[0][1]
|
||||||
if bracketExpr.len == 1:
|
if bracketExpr.len == 1:
|
||||||
bracketExpr.add([newCall(bindSym"typeof", newEmptyNode()), newCall(
|
bracketExpr.add([newCall(bindSym"typeof", newEmptyNode()), newCall(
|
||||||
bindSym"typeof", newEmptyNode())])
|
bindSym"typeof", newEmptyNode())])
|
||||||
template adder(res, k, v) = res[k] = v
|
template adder(res, k, v) = res[k] = v
|
||||||
result[0] = getAst(adder(res, n[0][0], n[0][1]))
|
result[0] = getAst(adder(res, n[0][0], n[0][1]))
|
||||||
of nnkCurly:
|
of nnkCurly:
|
||||||
result[2] = n[0]
|
result[2] = n[0]
|
||||||
if bracketExpr.len == 1:
|
if bracketExpr.len == 1:
|
||||||
bracketExpr.add(newCall(bindSym"typeof", newEmptyNode()))
|
bracketExpr.add(newCall(bindSym"typeof", newEmptyNode()))
|
||||||
template adder(res, v) = res.incl(v)
|
template adder(res, v) = res.incl(v)
|
||||||
result[0] = getAst(adder(res, n[0]))
|
result[0] = getAst(adder(res, n[0]))
|
||||||
else:
|
else:
|
||||||
result[2] = n
|
result[2] = n
|
||||||
if bracketExpr.len == 1:
|
if bracketExpr.len == 1:
|
||||||
bracketExpr.add(newCall(bindSym"typeof", newEmptyNode()))
|
bracketExpr.add(newCall(bindSym"typeof", newEmptyNode()))
|
||||||
template adder(res, v) = res.add(v)
|
template adder(res, v) = res.add(v)
|
||||||
result[0] = getAst(adder(res, n))
|
result[0] = getAst(adder(res, n))
|
||||||
|
|
||||||
macro collect*(init, body: untyped): untyped =
|
macro collect*(init, body: untyped): untyped {.since: (1, 1).} =
|
||||||
## Comprehension for seq/set/table collections. ``init`` is
|
## Comprehension for seq/set/table collections. ``init`` is
|
||||||
## the init call, and so custom collections are supported.
|
## the init call, and so custom collections are supported.
|
||||||
##
|
##
|
||||||
## The last statement of ``body`` has special syntax that specifies
|
## The last statement of ``body`` has special syntax that specifies
|
||||||
## the collection's add operation. Use ``{e}`` for set's ``incl``,
|
## the collection's add operation. Use ``{e}`` for set's ``incl``,
|
||||||
## ``{k: v}`` for table's ``[]=`` and ``e`` for seq's ``add``.
|
## ``{k: v}`` for table's ``[]=`` and ``e`` for seq's ``add``.
|
||||||
##
|
##
|
||||||
## The ``init`` proc can be called with any number of arguments,
|
## The ``init`` proc can be called with any number of arguments,
|
||||||
## i.e. ``initTable(initialSize)``.
|
## i.e. ``initTable(initialSize)``.
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
import sets, tables
|
import sets, tables
|
||||||
let data = @["bird", "word"]
|
let data = @["bird", "word"]
|
||||||
## seq:
|
## seq:
|
||||||
let k = collect(newSeq):
|
let k = collect(newSeq):
|
||||||
for i, d in data.pairs:
|
for i, d in data.pairs:
|
||||||
if i mod 2 == 0: d
|
if i mod 2 == 0: d
|
||||||
|
|
||||||
assert k == @["bird"]
|
assert k == @["bird"]
|
||||||
## seq with initialSize:
|
## seq with initialSize:
|
||||||
let x = collect(newSeqOfCap(4)):
|
let x = collect(newSeqOfCap(4)):
|
||||||
for i, d in data.pairs:
|
for i, d in data.pairs:
|
||||||
if i mod 2 == 0: d
|
if i mod 2 == 0: d
|
||||||
|
|
||||||
assert x == @["bird"]
|
assert x == @["bird"]
|
||||||
## HashSet:
|
## HashSet:
|
||||||
let y = initHashSet.collect:
|
let y = initHashSet.collect:
|
||||||
for d in data.items: {d}
|
for d in data.items: {d}
|
||||||
|
|
||||||
assert y == data.toHashSet
|
assert y == data.toHashSet
|
||||||
## Table:
|
## Table:
|
||||||
let z = collect(initTable(2)):
|
let z = collect(initTable(2)):
|
||||||
for i, d in data.pairs: {i: d}
|
for i, d in data.pairs: {i: d}
|
||||||
|
|
||||||
assert z == {1: "word", 0: "bird"}.toTable
|
assert z == {1: "word", 0: "bird"}.toTable
|
||||||
# analyse the body, find the deepest expression 'it' and replace it via
|
# analyse the body, find the deepest expression 'it' and replace it via
|
||||||
# 'result.add it'
|
# 'result.add it'
|
||||||
let res = genSym(nskVar, "collectResult")
|
let res = genSym(nskVar, "collectResult")
|
||||||
expectKind init, {nnkCall, nnkIdent, nnkSym}
|
expectKind init, {nnkCall, nnkIdent, nnkSym}
|
||||||
let bracketExpr = newTree(nnkBracketExpr,
|
let bracketExpr = newTree(nnkBracketExpr,
|
||||||
if init.kind == nnkCall: init[0] else: init)
|
if init.kind == nnkCall: init[0] else: init)
|
||||||
let (resBody, keyType, valueType) = transLastStmt(body, res, bracketExpr)
|
let (resBody, keyType, valueType) = transLastStmt(body, res, bracketExpr)
|
||||||
if bracketExpr.len == 3:
|
if bracketExpr.len == 3:
|
||||||
bracketExpr[1][1] = keyType
|
bracketExpr[1][1] = keyType
|
||||||
bracketExpr[2][1] = valueType
|
bracketExpr[2][1] = valueType
|
||||||
else:
|
else:
|
||||||
bracketExpr[1][1] = valueType
|
bracketExpr[1][1] = valueType
|
||||||
let call = newTree(nnkCall, bracketExpr)
|
let call = newTree(nnkCall, bracketExpr)
|
||||||
if init.kind == nnkCall:
|
if init.kind == nnkCall:
|
||||||
for i in 1 ..< init.len:
|
for i in 1 ..< init.len:
|
||||||
call.add init[i]
|
call.add init[i]
|
||||||
result = newTree(nnkStmtListExpr, newVarStmt(res, call), resBody, res)
|
result = newTree(nnkStmtListExpr, newVarStmt(res, call), resBody, res)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
since (1, 1):
|
||||||
import algorithm
|
import algorithm
|
||||||
|
|
||||||
var a = @[1, 2, 3, 4, 5, 6, 7, 8, 9]
|
var a = @[1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,7 @@ macro lenTuple*(t: typedesc[tuple]): int {.since: (1, 1).} =
|
||||||
## Return number of elements of `T`
|
## Return number of elements of `T`
|
||||||
newLit t.len
|
newLit t.len
|
||||||
|
|
||||||
when (NimMajor, NimMinor) >= (1, 1):
|
since (1, 1):
|
||||||
template get*(T: typedesc[tuple], i: static int): untyped =
|
template get*(T: typedesc[tuple], i: static int): untyped =
|
||||||
## Return `i`th element of `T`
|
## Return `i`th element of `T`
|
||||||
# Note: `[]` currently gives: `Error: no generic parameters allowed for ...`
|
# Note: `[]` currently gives: `Error: no generic parameters allowed for ...`
|
||||||
|
|
|
||||||
|
|
@ -52,5 +52,5 @@ else:
|
||||||
template since(version, body: untyped) {.dirty.} =
|
template since(version, body: untyped) {.dirty.} =
|
||||||
## limitation: can't be used to annotate a template (eg typetraits.get), would
|
## limitation: can't be used to annotate a template (eg typetraits.get), would
|
||||||
## error: cannot attach a custom pragma.
|
## error: cannot attach a custom pragma.
|
||||||
when version <= (NimMajor, NimMinor):
|
when (NimMajor, NimMinor) >= version:
|
||||||
body
|
body
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue