No description
Find a file
2024-09-27 11:52:27 +03:00
.github fix using conflicting library sources in subfolder docs 2024-09-27 11:52:27 +03:00
src fix using conflicting library sources in subfolder docs 2024-09-27 11:52:27 +03:00
tests fix tests, test_iterator_typed still broken https://github.com/nim-lang/Nim/pull/24075 2024-09-07 13:43:45 +03:00
.gitignore initial 2020-11-28 04:35:50 +03:00
applicates.nimble disable test_iterator_typed on 2.0 2024-09-27 11:32:04 +03:00
README.md update readme 2022-09-18 13:45:20 +03:00

applicates

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.

This allows for fully inlined lambdas via anonymous templates, which is the construct that the macros in this library mainly focus on.

import applicates

# `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) # inlined at AST level
    # 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] = x |> f

assert @[1, 2, 3, 4, 5].map(applicate do (x: int) -> int: x - 1) == @[0, 1, 2, 3, 4]
assert @[1, 2, 3, 4, 5].map(toApplicate(succ)) == @[2, 3, 4, 5, 6]
const double = x ==> x * 2
assert @[1, 2, 3, 4, 5].map(double) == @[2, 4, 6, 8, 10]

See docs and 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, and errors are likely to be cryptic.