Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
lilkeet committed Nov 4, 2023
0 parents commit 0c766bb
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
14 changes: 14 additions & 0 deletions LICENSE.txt
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.

48 changes: 48 additions & 0 deletions README.md
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.
16 changes: 16 additions & 0 deletions funcblock.nimble
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"
49 changes: 49 additions & 0 deletions src/funcblock.nim
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()
30 changes: 30 additions & 0 deletions tests/test1.nim
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"

0 comments on commit 0c766bb

Please sign in to comment.