Skip to content

metagn/applicates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.