-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
11 changed files
with
228 additions
and
256 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 |
---|---|---|
@@ -1,33 +1,34 @@ | ||
# applicates | ||
|
||
instantiated "pointers" to cached AST. caches nodes of anonymous routine definitions OR symbols then returns their pointer (index/key in the cache) which you can pass around as a compile time argument and instantiate in order to use. this allows for fully inlined lambdas via *"anonymous templates"*, which is the construct that the macros in this library mainly focus on. | ||
Generalized routine and symbol pointers, achieved by instantiating cached | ||
routine definitions or symbols. The cached AST is referenced by a key, | ||
this key is passed around as a compile time value to be instantiated. | ||
|
||
Would have preferred not using a cache to do this, but for now it should do the job. | ||
This allows for fully inlined lambdas via anonymous templates, which is | ||
the construct that the macros in this library mainly focus on. | ||
|
||
```nim | ||
import applicates | ||
# optional operators: | ||
import applicates/operators | ||
# ApplicateArg is static Applicate | ||
proc map[T](s: seq[T], f: ApplicateArg): seq[T] = | ||
result.newSeq(s.len) | ||
for i in 0..<s.len: | ||
let x = s[i] | ||
result[i] = f.apply(x) | ||
# optional operators: | ||
result[i] = x |> f # injects x into right hand side | ||
# with `import applicates/calloperator`: | ||
result[i] = f(x) | ||
result[i] = x.f | ||
# with `import applicates/operators`: | ||
result[i] = \f(x) | ||
result[i] = \x.f | ||
result[i] = f(x) # when experimental callOperator is enabled | ||
result[i] = x.f # ditto | ||
result[i] = x |> f | ||
# `applicate do` here generates an anonymous template, so `x - 1` is inlined at AST level: | ||
doAssert @[1, 2, 3, 4, 5].map(applicate do (x): x - 1) == @[0, 1, 2, 3, 4] | ||
doAssert @[1, 2, 3, 4, 5].map(fromSymbol(succ)) == @[2, 3, 4, 5, 6] | ||
# optional operators: | ||
doAssert @[1, 2, 3, 4, 5].map(applicate do (x: int) -> int: x - 1) == @[0, 1, 2, 3, 4] | ||
doAssert @[1, 2, 3, 4, 5].map(toApplicate(succ)) == @[2, 3, 4, 5, 6] | ||
doAssert @[1, 2, 3, 4, 5].map(x ==> x * 2) == @[2, 4, 6, 8, 10] | ||
``` | ||
|
||
See tests for more example uses of this library. Tests are ran for multiple backends. | ||
|
||
Note: Since `Applicate` is implemented as `distinct ApplicateKey` and is also usually used as `static Applicate` (for which `ApplicateArg` is an alias), this library fairly pushes Nim's type system, so annotating applicates with types can be difficult. Nim macro errors in general are also not great. | ||
Note: Since `Applicate` is implemented as `distinct ApplicateKey` and is also usually used as `static Applicate` (for which `ApplicateArg` is an alias), this library fairly pushes Nim's type system, and errors are likely to be cryptic. |
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
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
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,5 @@ | ||
import ../applicates | ||
|
||
template `()`*(appl: ApplicateArg, args: varargs[untyped]): untyped = | ||
## Call operator alias for `apply`. | ||
appl.apply(args) |
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
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
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
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
Oops, something went wrong.