Combinator Syntax
What is Combinator Syntax?
Section titled “What is Combinator Syntax?”Combinator syntax allows you to pass functions to import-tree to configure and immediately use it, reducing boilerplate and enabling fluent composition:
# Without combinators (verbose)let configured = import-tree.withLib lib; leafs = configured.leafs;inleafs ./modules
# With combinators (terse)import-tree (it: it.withLib lib) (it: it.leafs) ./modulesHow It Works
Section titled “How It Works”When you call import-tree with a function (a zero-argument function), it receives the current import-tree object and can call any methods on it:
import-tree (self: self.withLib lib)The function is applied, and the result replaces the state. You can chain multiple functions:
import-tree (it: it.withLib lib) # first: add lib (it: it.addPath ./modules) # second: add path (it: it.filter (lib.hasSuffix "mod.nix")) # third: filter ./src # finally: evaluateThis is purely syntactic sugar - it’s equivalent to piping through the operations in order.
Real-World Examples
Section titled “Real-World Examples”Filtered List with Combinator
Section titled “Filtered List with Combinator”Get a filtered file list in one expression:
import-tree (it: it.withLib lib) (it: it.filter (lib.hasSuffix "mod.nix")) (it: it.leafs) ./modulesWith Scoped Imports
Section titled “With Scoped Imports”import-tree (it: it.addScoped { helpers = my-utils; }) ./modulesUsing Custom API
Section titled “Using Custom API”Combinators work with custom API methods:
let custom = import-tree.addAPI { nixFilesOnly = self: self.match ".*\\.nix$"; };incustom (it: it.nixFilesOnly) (it: it.leafs) ./srcWhen to Use
Section titled “When to Use”Combinators shine for:
- One-off file discovery in scripts
- Building configurations inline without intermediate variables
- Chaining multiple small transformations
For complex, reusable patterns, consider instead building domain-specific methods with .addAPI.