-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0c766bb
Showing
5 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | ||
Version 2, December 2004 | ||
|
||
Copyright (C) 2004 Sam Hocevar <[email protected]> | ||
|
||
Everyone is permitted to copy and distribute verbatim or modified | ||
copies of this license document, and changing it is allowed as long | ||
as the name is changed. | ||
|
||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | ||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | ||
|
||
0. You just DO WHAT THE FUCK YOU WANT TO. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# funcblock | ||
|
||
A `funcBlock` is a a block that returns a value and has no side effects. | ||
Also implemented is a `procBlock`: a block that returns a value and may have side effects. | ||
|
||
### Prerequisites | ||
|
||
Nim compiler. Minimum version untested. | ||
Nimble package manager. Minimum version also untested. | ||
|
||
### Installing | ||
|
||
Type `nimble install funcblock` into your cli and follow the on-screen instructions. | ||
|
||
### Usage | ||
|
||
```nim | ||
import funcblock | ||
proc main: string = | ||
let a = funcblock(int): | ||
#[A `result` var is initialized. | ||
It can be differentiated from another `result` variable in a higher scope | ||
easily by its type. | ||
However, you lose the ability to access a higher scope `result` of the | ||
same type.]# | ||
result = 0 | ||
for i in 0..10: | ||
result.inc i | ||
assert result == 55 | ||
assert a == 55 | ||
result = $a | ||
assert main() == "55" | ||
``` | ||
|
||
## License | ||
|
||
This project is licensed under the WTFPL License - see the [LICENSE.txt](LICENSE.txt) file for details. | ||
|
||
## Authors | ||
|
||
* **LilKeet** - *Initial work* - [LilKeet](https://github.com/lilkeet) | ||
|
||
## Contributing | ||
|
||
Idk just submit an issue or PR or sum. Or make ur own. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Package | ||
|
||
version = "0.1.0" | ||
author = "lilkeet" | ||
description = "Implements a 'funcblock'; that is, a block that returns a value with no side effects." | ||
license = "WTFPL" | ||
srcDir = "src" | ||
|
||
|
||
# Dependencies | ||
|
||
#requires "nim >= 2.0.0" | ||
|
||
|
||
task test, "Runs the tests via Testament.": | ||
exec "testament pattern tests/*.nim" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
##[Implements a `block` that acts like a `func`. That is, a `block` that | ||
returns a value and has no side effects.]## | ||
|
||
template funcBlock*(returning, body: untyped): untyped = | ||
## A specialization of the `block` keyword. | ||
## Returns a value of type `returning`, and has no side effects. | ||
runnableExamples: | ||
proc myProc(): string = | ||
let a = funcBlock(int): | ||
# Each funcBlock has its own internal result variable. | ||
result = 0 | ||
for c in '0' .. '9': | ||
result += 1 | ||
assert result == 10 | ||
|
||
# After exiting the funcBlock's scope, the old result variable is | ||
# forgotten and stored into the declared variable. | ||
assert result == "" | ||
assert a == 10 | ||
assert myProc() == "" | ||
|
||
block: | ||
func insideFuncBlock(): returning = | ||
body | ||
insideFuncBlock() | ||
|
||
template procBlock*(returning, body: untyped): untyped = | ||
## A specialization of the `block` keyword. | ||
## Returns a value of type `returning`. | ||
runnableExamples: | ||
proc myProc(): string = | ||
let a = procBlock(int): | ||
# Each procBlock has its own internal result variable. | ||
result = 0 | ||
for c in '0' .. '9': | ||
result += 1 | ||
assert result == 10 | ||
|
||
# After exiting the procBlock's scope, the old result variable is | ||
# forgotten and stored into the declared variable. | ||
assert result == "" | ||
assert a == 10 | ||
assert myProc() == "" | ||
|
||
block: | ||
proc insideFuncBlock(): returning = | ||
body | ||
insideFuncBlock() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# To run these tests, simply execute `nimble test`. | ||
|
||
discard """ | ||
action: "run" | ||
exitcode: 0 | ||
batchable: true | ||
joinable: true | ||
valgrind: true | ||
timeout: 3.0 | ||
targets: "c cpp objc js" | ||
""" | ||
|
||
import ../src/funcblock | ||
|
||
proc main: string = | ||
let a = funcblock(int): | ||
#[A `result` var is initialized. | ||
It can be differentiated from another `result` variable in a higher scope | ||
easily by its type. | ||
However, you lose the ability to access a higher scope `result` of the | ||
same type.]# | ||
result = 0 | ||
for i in 0..10: | ||
result.inc i | ||
assert result == 55 | ||
|
||
assert a == 55 | ||
result = $a | ||
|
||
assert main() == "55" |