FunctionTree

Wraps a function and a domain as an async tree

Usage

import { FunctionTree } from "@weborigami/origami";

// Wrap an object to create an async tree.
const tree = new FunctionTree({
  (key) => `Hello, ${key}.`,
  ["Alice", "Bob", "Carol"]
});

The ori tool will display the contents of the resulting FunctionTree.

$ ori function.js/
Alice: Hello, Alice.
Bob: Hello, Bob.
Carol: Hello, Carol.

A rough implementation of the core interface methods in FunctionTree is:

// Rough implementation
class FunctionTree {
  constructor(fn, domain = []) {
    this.fn = fn;
    this.domain = domain;
  }

  // Return the value for a given key.
  async get(key) {
    return this.fn(key);
  }

  // Return the function's domain as its keys.
  async keys() {
    return this.domain;
  }
}

Unlike async tree classes like ObjectTree, an FunctionTree can often accept keys which it does not make public in its keys iterator. The sample FunctionTree defined above exposes only three keys (“Alice”, “Bob”, “Carol”), but will actually accept any key.

$ ori @keys function.js/
- Alice
- Bob
- Carol
$ ori function.js/David
Hello, David.

FunctionTree class

FunctionTree(fn, [domain]) constructor

  • fn: function – the key->value function
  • domain: Iterable – optional domain of the function

async get(key)

  • key: any

Returns: unknown

Return the application of the function to the given key.

async keys()

Returns: unknown

Enumerates the function’s domain (if defined) as the tree’s keys. If no domain was defined, this returns an empty iterator.