-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
601678c
commit 5ab8678
Showing
4 changed files
with
88 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import std/macros | ||
include system/inclrtl | ||
|
||
proc outplaceImpl(arg, call: NimNode): NimNode = | ||
expectKind call, nnkCallKinds | ||
let tmp = genSym(nskVar, "outplaceResult") | ||
var callsons = call[0..^1] | ||
var found = false | ||
for i in 1..<len(callsons): | ||
if callsons[i].kind == nnkIdent and callsons[i].strVal == "_": | ||
callsons[i] = tmp | ||
found = true | ||
break | ||
if not found: callsons.insert(tmp, 1) | ||
result = newTree(nnkStmtListExpr, | ||
newVarStmt(tmp, arg), | ||
copyNimNode(call).add callsons, | ||
tmp) | ||
|
||
proc replaceOutplace(lhs, n: NimNode): NimNode = | ||
case n.kind | ||
of nnkDotExpr, nnkBracketExpr: | ||
result = copyNimTree(n) | ||
result[0] = replaceOutplace(lhs, result[0]) | ||
of nnkCall: | ||
result = outplaceImpl(lhs, n) | ||
of nnkCommand: | ||
result = outplaceImpl(lhs, n) | ||
else: | ||
doAssert false, "unexpected kind: " & $n.kind | ||
|
||
macro `.@`*(lhs, rhs): untyped {.since: (1, 1).} = | ||
## Outplace operator: turns an `in-place`:idx: algorithm into one that works on | ||
## a copy and returns this copy. A placeholder `_` can optionally be used to | ||
## specify an output parameter of position > 0. | ||
## **Since**: Version 1.2. | ||
runnableExamples: | ||
import algorithm, strutils | ||
doAssert @[2,1,3].@sort() == @[1,2,3] | ||
doAssert "".@addQuoted("foX").toUpper == "\"FOX\"" | ||
doAssert "A".@addQuoted("foo").toUpper[0..1].toLower == "a\"" | ||
proc bar(x: int, ret: var int) = ret += x | ||
doAssert 3.@bar(4, _) == 3 + 4 # use placeholder `_` to specify a position > 0 | ||
doAssert @[2,1,3].@sort(_) == @[1,2,3] # `_` works but unneeded in position 0 | ||
result = copyNimTree(rhs) | ||
result = replaceOutplace(lhs, result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import std/outplaces | ||
import algorithm | ||
import random | ||
|
||
proc main() = | ||
var a = @[1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
doAssert a.@sort() == sorted(a) | ||
#Chaining: | ||
var aCopy = a | ||
aCopy.insert(10) | ||
doAssert a.@insert(10).@sort() == sorted(aCopy) | ||
doAssert @[1,3,2].@sort().@sort(order = SortOrder.Descending) == @[3,2,1] | ||
# using 2 `.@` chained together | ||
|
||
proc bar(x: int, ret: var int) = ret += x | ||
doAssert 3.@bar(4, _) == 3 + 4 | ||
|
||
const b = @[0, 1, 2] | ||
let c = b.@shuffle() | ||
doAssert c[0] == 1 | ||
doAssert c[1] == 0 | ||
|
||
block: | ||
var a = "foo" | ||
var b = "bar" | ||
doAssert "ab".@add("cd") == "abcd" | ||
let ret = "ab".@add "cd" # example with `nnkCommand` | ||
doAssert ret == "abcd" | ||
|
||
main() |