Skip to content

Commit

Permalink
Fix tuple concatenation containing lists, tuples, refs
Browse files Browse the repository at this point in the history
Bump to 0.5.3
  • Loading branch information
rotu committed Jul 12, 2023
1 parent 3d90e26 commit eea8cf4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion records.nimble
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

# Package
version = "0.5.2"
version = "0.5.3"
author = "Dan Rose"
description = "Operations on tuples as heterogeneous record types a la Relational Algebra"
license = "MIT"
Expand Down
8 changes: 4 additions & 4 deletions src/records/tupleops.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ proc concat*(t1: tuple, t2: tuple): tuple =
expectKind(arg.getTypeImpl(), {nnkTupleConstr, nnkTupleTy})
for i, d in pairs(arg.getTypeImpl):
case kind(d):
of nnkSym:
# un-named tuple field
result.add newTree(nnkBracketExpr, arg, newLit(i))
of nnkIdentDefs:
# named tuple field
let prop = d[0]
result.add newColonExpr(prop,
newTree(nnkDotExpr, arg, prop)
)
else:
error("Unexpected field kind: `" & $kind(d) & "`")
# un-named tuple field
# usually d is of kind nnkSym
# but can be nnkBracketExpr, nnkTupleConstr, nnkRefTy
result.add newTree(nnkBracketExpr, arg, newLit(i))
concatImpl()

proc `&` *(t1: tuple, t2: tuple): auto = concat(t1, t2)
Expand Down
30 changes: 30 additions & 0 deletions tests/testTuple.nim
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,36 @@ test "concatenation":
let c = concat( (1, 2), (3, "a"))
check c == (1, 2, 3, "a")

check concat((),()) == ()
check concat((),(1,)) == (1,)
check concat((1,),()) == (1,)

test "concatenateWithLists":
let a = ([1],[2,3])
let b = ([4],)
check a & b == ([1],[2,3],[4])

let d = (x:[1],y:[2,3])
let e = (z:[4],)
check d & e == (x:[1],y:[2,3],z:[4])

test "concatenateWithTuples":
let a = ((),)
let b = ((1,2,3),(4))
check a & b == ((),(1,2,3),(4))

test "concatenateWithObjects":
type Foo = object
x:int
let a = Foo(x:1)
let b = Foo(x:2)
check concat((a,),(b,)) == (a,b)
check concat((a,),(b,)) != (b,a)

let ra = (ref Foo)(x:1)

check concat((ra,),(a,)) == (ra,a)

test "join":
let x = (a: 1, b: 2)
check join(x, x) == some(x)
Expand Down

0 comments on commit eea8cf4

Please sign in to comment.