Skip to content

Commit 26fd815

Browse files
committed
touch up, operator version, method calls work
1 parent d2393d0 commit 26fd815

File tree

6 files changed

+182
-41
lines changed

6 files changed

+182
-41
lines changed

.github/workflows/main.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,39 @@ jobs:
2424

2525
- name: run tests
2626
run: nimble tests
27+
28+
docs:
29+
runs-on: ubuntu-latest
30+
31+
steps:
32+
- uses: actions/checkout@v3
33+
- uses: jiro4989/setup-nim-action@v1
34+
35+
- name: install nimbleutils
36+
run: nimble install -y https://github.com/metagn/nimbleutils@#HEAD
37+
38+
- name: install dependencies
39+
run: nimble install -y
40+
41+
- name: compile docs
42+
run: nimble docs
43+
44+
- name: move to subfolder
45+
run: |
46+
mkdir pages
47+
mv docs pages/
48+
cd pages
49+
git init
50+
git add -A
51+
git config --local user.email "[email protected]"
52+
git config --local user.name "GitHub Action"
53+
git commit -m 'deploy'
54+
55+
- name: push to branch
56+
uses: ad-m/github-push-action@master
57+
with:
58+
github_token: ${{ secrets.GITHUB_TOKEN }}
59+
branch: gh-pages
60+
force: true
61+
directory: ./pages/
62+
if: github.event_name != 'pull_request'

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,56 @@ proc foo(a, b, c: int) =
1010
echo "b: ", b
1111
echo "c: ", c
1212
13+
# regular call:
1314
foo.spread:
1415
1
1516
c = 3
1617
b = 2
1718
18-
spread foo(1): # limitation: can't call with dot syntax for calls
19+
# operator version:
20+
...foo:
21+
1
1922
c = 3
2023
b = 2
2124
25+
# operator allows inline version with `do`:
26+
let a = 1 + (...min do:
27+
2
28+
3)
29+
30+
assert a == 3
31+
32+
# method call syntax:
33+
spread 1.foo:
34+
c = 3
35+
b = 2
36+
37+
spread 1.foo(2):
38+
c = 3
39+
40+
# arrays:
2241
const arr = [].spread:
2342
1
2443
2
2544
3
2645
2746
assert arr == [1, 2, 3]
2847
48+
# table constructors:
2949
let tab = {:}.spread:
3050
"a" = 1 # constructors convert = in a statement to :
3151
_("b": 2, "c": 3) # all arguments of _ are spread directly
3252
3353
assert tab == {"a": 1, "b": 2, "c": 3}
3454
55+
# object or tuple constructors need a single `_: _``:
3556
type Foo = object
3657
a, b, c: int
3758
38-
# to spread object or tuple constructors without any arguments, include a single _: _
3959
let obj = spread Foo(_: _):
4060
a = 1
4161
c = 3
4262
b = 2
4363
44-
assert obj = Foo(a: 1, b: 2, c: 3)
64+
assert obj == Foo(a: 1, b: 2, c: 3)
4565
```

spread.nimble

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Package
22

3-
version = "0.1.0"
3+
version = "0.2.0"
44
author = "metagn"
55
description = "macro for spreading blocks into call parameters/collections"
66
license = "MIT"
@@ -25,7 +25,7 @@ task docs, "build docs for all modules":
2525
task tests, "run tests for multiple backends and defines":
2626
when declared(runTests):
2727
runTests(
28-
backends = {c, #[js]#},
28+
backends = {c, js, nims},
2929
)
3030
else:
3131
echo "tests task not implemented, need nimbleutils"

src/spread.nim

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,53 @@
11
import macros
22

33
macro spread*(call: untyped, args: varargs[untyped]): untyped =
4+
## macro for providing call arguments or collection literal elements with
5+
## block syntax
6+
runnableExamples:
7+
proc foo(a, b, c: int) =
8+
echo "a: ", a
9+
echo "b: ", b
10+
echo "c: ", c
11+
12+
# regular call:
13+
foo.spread:
14+
1
15+
c = 3
16+
b = 2
17+
18+
# method call syntax:
19+
spread 1.foo:
20+
c = 3
21+
b = 2
22+
23+
spread 1.foo(2):
24+
c = 3
25+
26+
# arrays:
27+
const arr = [].spread:
28+
1
29+
2
30+
3
31+
32+
assert arr == [1, 2, 3]
33+
34+
# table constructors:
35+
let tab = {:}.spread:
36+
"a" = 1 # constructors convert = in a statement to :
37+
_("b": 2, "c": 3) # all arguments of _ are spread directly
38+
39+
assert tab == {"a": 1, "b": 2, "c": 3}
40+
41+
# object or tuple constructors need a single `_: _``:
42+
type Foo = object
43+
a, b, c: int
44+
45+
let obj = spread Foo(_: _):
46+
a = 1
47+
c = 3
48+
b = 2
49+
50+
assert obj == Foo(a: 1, b: 2, c: 3)
451
let colonKinds = {nnkBracket, nnkPar, nnkCurly,
552
nnkTupleConstr, nnkTableConstr, nnkObjConstr}
653
result =
@@ -24,3 +71,18 @@ macro spread*(call: untyped, args: varargs[untyped]): untyped =
2471
result.add(arg[1..^1])
2572
else:
2673
result.add(arg)
74+
75+
template `...`*(call: untyped, args: varargs[untyped]): untyped =
76+
## operator version of `spread`
77+
runnableExamples:
78+
...foo:
79+
1
80+
c = 3
81+
b = 2
82+
83+
# allows inline version with `do`:
84+
let a = 1 + (...min do:
85+
2
86+
3)
87+
assert a == 3
88+
spread(call, args)

tests/test1.nim

Lines changed: 0 additions & 36 deletions
This file was deleted.

tests/test_spread.nim

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import spread
2+
3+
block:
4+
proc foo(a, b, c: int): string = $(a, b, c)
5+
6+
block:
7+
# regular call:
8+
let x = foo.spread:
9+
a = 1
10+
c = 3
11+
b = 2
12+
doAssert x == "(1, 2, 3)"
13+
14+
# method call syntax:
15+
let y = spread 1.foo:
16+
c = 3
17+
b = 2
18+
let z = spread 1.foo(2):
19+
c = 3
20+
doAssert x == y and y == z
21+
22+
block:
23+
# operator version:
24+
let a = ...foo:
25+
1
26+
c = 3
27+
b = 2
28+
doAssert a == "(1, 2, 3)"
29+
30+
# operator allows inline version with `do`:
31+
let b = 1 + (...min do:
32+
2
33+
3)
34+
doAssert b == 3
35+
36+
block:
37+
# array constructor:
38+
const arr = [].spread:
39+
1
40+
2
41+
3
42+
doAssert arr == [1, 2, 3]
43+
44+
# table constructor
45+
let tab = spread {:}:
46+
"a" = 1 # constructors convert = in a statement to :
47+
_("b": 2, "c": 3) # all arguments of _ are spread directly
48+
doAssert tab == {"a": 1, "b": 2, "c": 3}
49+
50+
block:
51+
# object or tuple constructors
52+
type Foo = object
53+
a, b, c: int
54+
let obj = spread Foo(_: _):
55+
a = 1
56+
c = 3
57+
b = 2
58+
doAssert obj == Foo(a: 1, b: 2, c: 3)
59+

0 commit comments

Comments
 (0)