Skip to content

Commit

Permalink
Backport to 1.4.8 (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
rotu committed Sep 7, 2022
1 parent 72d285e commit 3d90e26
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
nim: [ '1.6.0', 'stable', 'devel' ]
nim: [ '1.4.8', 'stable', 'devel' ]
name: Nim ${{ matrix.nim }} sample
steps:
- uses: actions/checkout@v3
Expand Down
4 changes: 2 additions & 2 deletions records.nimble
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import os

# Package
version = "0.5.1"
version = "0.5.2"
author = "Dan Rose"
description = "Operations on tuples as heterogeneous record types a la Relational Algebra"
license = "MIT"
srcDir = "src"
binDir = "build"
requires "nim >= 1.6.0"
requires "nim >= 1.4.8"

task style, "enforce code style":
var paths: seq[string]
Expand Down
11 changes: 6 additions & 5 deletions src/records/tupleops.nim
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,20 @@ template tupleKeys*(t: tuple): TupleKeys =
## Return all the field names of the given tuple
tupleKeys(typeof t)

proc project*(t: tuple, keys: static openArray[string]): tuple =
proc project*[T: tuple](t: T, keys: static openArray[string]): auto =
## Rearrange/select named fields from a named tuple,
## returning a new named tuple

macro projImpl(): tuple =
result = newNimNode(nnkTupleConstr)
macro projImpl(): untyped =
let res = newNimNode(nnkPar)
when keys.len != 0:
for key in @keys:
result.add(newColonExpr(
res.add(newColonExpr(
ident key,
newDotExpr(bindSym "t", ident key)
))
projImpl()
res
return projImpl()

proc reject*(t: tuple, keys: static openArray[string]): tuple =
## Given a tuple and a set of keys, return key-value pairs whose keys
Expand Down
10 changes: 6 additions & 4 deletions tests/testLenientTuple.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ test "testAssignment":
check x[0] != y[0]

let yAsX = y.to(typeof x)
check (typeof yAsX) is (typeof x)
check yAsX[0] == x[0]
check:
((typeof yAsX) is (typeof x))
yAsX[0] == x[0]

let y2AsX = y.to(typeof x)
check (typeof y2AsX) is (typeof x)
check y2AsX[0] == x[0]
check:
((typeof y2AsX) is (typeof x))
y2AsX[0] == x[0]

test "assignfrom":
var dest = (x: 1, y: 2)
Expand Down
6 changes: 3 additions & 3 deletions tests/testRelational.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import std/[sequtils, sugar, tables, unittest]
import records/[relational, lenientTuple]

test "joinSequences":
var squares = collect:
var squares = collect(newSeq):
for i in -3..3:
(x: i, y: (i*i))
check len(squares) == 7
let squares2 = collect:
let squares2 = collect(newSeq):
for i in -3..3:
(y: i*i, z: i)
let foo = join(squares, squares2)
Expand All @@ -19,7 +19,7 @@ test "joinSequences":
check rec["z"]*rec["z"] == rec["y"]

test "project":
let table = collect:
let table = collect(newSeq):
for i in -3..3:
(x: i, y: (i*i))
let xs = project(table, ["x"])
Expand Down

0 comments on commit 3d90e26

Please sign in to comment.