-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new outplace operator: doAssert @[2,1,3].>sort() == @[1,2,3]
#13309
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a81bd9b
new outplace operator
timotheecour f2454c0
fixup
timotheecour 78578b1
fix typo
timotheecour 98f74d4
deprecate outplace
timotheecour 601678c
support `_` placeholder
timotheecour 5ab8678
moved to new outplaces module
timotheecour 1666f86
.@ => ./
timotheecour 0db3a38
fixup
timotheecour 72ea119
remove instead of deprecateding sugar/outplace since it never made it…
timotheecour 9b4f645
last bikeshed? ./ => .>
timotheecour c2bd62f
add newline in doc comment
timotheecour File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,47 @@ | ||
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,31 @@ | ||
import std/[outplaces, algorithm, random, os] | ||
|
||
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" | ||
|
||
when defined(posix): | ||
doAssert "foo./bar///".>normalizePathEnd() == "foo./bar" | ||
|
||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Paragraphs! Please add empty line above this, otherwise it'll render poorly in the doc gen.
Also, you need to add this file to the docgen config IIRC.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
I believe I've fixed this in a general way in #13221