doc(sugar): added description and examples to dup (#15455)

This commit is contained in:
Luis Felipe Manfroni 2020-10-05 07:02:08 -03:00 • committed by GitHub
commit 5967b6f60f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -217,7 +217,10 @@ since (1, 1):
macro dup*[T](arg: T, calls: varargs[untyped]): T = macro dup*[T](arg: T, calls: varargs[untyped]): 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. ## a copy and returns this copy, without modifying its input.
##
## This macro also allows for (otherwise in-place) function chaining.
##
## **Since**: Version 1.2. ## **Since**: Version 1.2.
runnableExamples: runnableExamples:
import algorithm import algorithm
@ -234,6 +237,21 @@ since (1, 1):
var s2 = "xyz" var s2 = "xyz"
doAssert s1 & s2 == s1.dup(&= s2) doAssert s1 & s2 == s1.dup(&= s2)
proc makePalindrome(s: var string) =
for i in countdown(s.len-2, 0):
s.add(s[i])
var c = "xyz"
# An underscore (_) can be used to denote the place of the argument you're passing:
# b = "xyz"
var d = dup c:
makePalindrome # xyzyx
sort(_, SortOrder.Descending) # zyyxx
makePalindrome # zyyxxxyyz
doAssert d == "zyyxxxyyz"
result = newNimNode(nnkStmtListExpr, arg) result = newNimNode(nnkStmtListExpr, arg)
let tmp = genSym(nskVar, "dupResult") let tmp = genSym(nskVar, "dupResult")
result.add newVarStmt(tmp, arg) result.add newVarStmt(tmp, arg)