Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
28
Task/Nested-templated-data/Nim/nested-templated-data.nim
Normal file
28
Task/Nested-templated-data/Nim/nested-templated-data.nim
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import macros,sugar,strformat
|
||||
#macro to take a nested tuple and return a type
|
||||
#e.g. (int,(int,int))=>(string,(string,string))
|
||||
proc intstr(n: NimNode): NimNode =
|
||||
if n.kind == nnkSym:
|
||||
return ident("string")
|
||||
result = nnkPar.newNimNode()
|
||||
for i in 1..<n.len:
|
||||
result.add(intstr(n[i]))
|
||||
|
||||
macro tuptype(t: typed): untyped = intstr(t.getType)
|
||||
|
||||
|
||||
proc replace(t: tuple | int, p: openArray[string]): auto =
|
||||
when t is int: (if t in 0..<p.len: p[t] else: "nil")
|
||||
else:
|
||||
var res: tuptype(t)
|
||||
for k, v in t.fieldpairs:
|
||||
#k will be 'Field0', so we convert to an integer index
|
||||
res[k[^1].int - '0'.int] = replace(v, p)
|
||||
return res
|
||||
when isMainModule:
|
||||
let p = collect(for i in 0..5: &"payload{i}")
|
||||
|
||||
let tplt1 = ((1,2),(3,4,1),5)
|
||||
let tplt2 = ((1,2),(3,4,1),6)
|
||||
echo replace(tplt1, p)
|
||||
echo replace(tplt2, p)
|
||||
Loading…
Add table
Add a link
Reference in a new issue