-
-
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.
direct symbol applicate, typed iterator test
- Loading branch information
Showing
8 changed files
with
295 additions
and
92 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,26 +1,33 @@ | ||
# applicates | ||
|
||
"pointers" to cached AST that instantiate routines when called. caches nodes of anonymous routine definitions then gives their pointer (index in the cache) which you can pass around as a compile time argument and invoke. not a clean solution but should do the job | ||
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. | ||
|
||
Would have preferred not using a cache to do this, but for now it should do the job. | ||
|
||
```nim | ||
import applicates | ||
proc map[T](s: seq[T], f: ApplicateArg): seq[T] = | ||
result.newSeq(s.len) | ||
for i in 0..<s.len: | ||
result[i] = | ||
f.apply(s[i]) # maybe a little long | ||
# or | ||
f | s[i] | ||
s[i] |< f # noisy, also you need to do tuples like ((1, 2)) |< f as (1, 2) |< f becomes f.apply(1, 2) | ||
# or | ||
f(s[i]) # uses experimental callOperator feature, if it breaks your code use `import except` | ||
let x = s[i] | ||
result[i] = f.apply(x) | ||
# supported sugar for the above (best I could come up with, might be too much): | ||
result[i] = f | x | ||
result[i] = x |< f | ||
result[i] = \f(x) | ||
result[i] = \x.f | ||
result[i] = f(x) # when experimental callOperator is enabled | ||
result[i] = x.f # ditto | ||
# `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] | ||
# alternate syntax (doesnt look great but i cant think of anything better): | ||
doAssert @[1, 2, 3, 4, 5].map(fromSymbol(succ)) == @[2, 3, 4, 5, 6] | ||
# sugar for `applicate do` syntax (again, best I could come up with): | ||
doAssert @[1, 2, 3, 4, 5].map(x !=> x * 2) == @[2, 4, 6, 8, 10] | ||
doAssert @[1, 2, 3, 4, 5].map(x \=> x * 2) == @[2, 4, 6, 8, 10] | ||
``` | ||
|
||
tests show some of the possibilities with this construct | ||
See tests for more example uses of this library. | ||
|
||
1 limitation is you can't really annotate these with types. you might think of a way with concepts but it's probably going to be way too complex to work | ||
Note: Since `Applicate` is implemented as `distinct int` or `distinct string` and is also usually used as `static Applicate` (for which `ApplicateArg` is an alias), you might have a fair bit of trouble/bugs with the type system. This is unfortunate as it limits the possibilities for type annotated functional programming using applicates. The messiness of Nim's error system when dealing with macros also does not help in this regard. |
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
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.