Skip to content

Commit

Permalink
docs(defmulti): update readme, add graphviz example
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Nov 2, 2020
1 parent d15a159 commit e6e5fe6
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 1 deletion.
61 changes: 61 additions & 0 deletions assets/defmulti/readme.dot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 43 additions & 1 deletion packages/defmulti/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This project is part of the
- [Usage examples](#usage-examples)
- [Dynamic dispatch: Simple S-expression interpreter](#dynamic-dispatch--simple-s-expression-interpreter)
- [True multiple arg dispatch](#true-multiple-arg-dispatch)
- [Dispatch value graph visualization](#dispatch-value-graph-visualization)
- [Authors](#authors)
- [License](#license)

Expand Down Expand Up @@ -53,7 +54,7 @@ yarn add @thi.ng/defmulti
<script src="https://unpkg.com/@thi.ng/defmulti/lib/index.umd.js" crossorigin></script>
```

Package sizes (gzipped, pre-treeshake): ESM: 750 bytes / CJS: 811 bytes / UMD: 885 bytes
Package sizes (gzipped, pre-treeshake): ESM: 799 bytes / CJS: 862 bytes / UMD: 936 bytes

## Dependencies

Expand Down Expand Up @@ -106,6 +107,7 @@ function, but also exposes the following operations:
- `.rels()` - return all dispatch value relationships
- `.parents(id)` - direct parents of dispatch value `id`
- `.ancestors(id)` - transitive parents of dispatch value `id`
- `.dependencies()` - returns iterator of all dispatch value relationship pairs

#### Dispatch value hierarchies

Expand Down Expand Up @@ -327,6 +329,46 @@ apr({type: "isa", balance: 10000});
// Error: invalid account type: isa
```

#### Dispatch value graph visualization

To facilitate better introspection of dynamically constructed/added `defmulti()`
implementations (with possibly deep hierarchies of dispatch values), we can
utilize the `.dependencies()` method to extract all dispatch value relationships
and use these to build [dependency
graph](https://github.com/thi-ng/umbrella/tree/develop/packages/dgraph), which
then can also be visualized.

```ts
import { defDGraph } from "@thi.ng/dgraph";
import { toDot } from "@thi.ng/dgraph-dot";

const fn = defmulti((x) => x);

// dummy impls
fn.add("a", () => {});
fn.add("d", () => {});

// dispatch value relationships
fn.isa("b", "a");
fn.isa("c", "b");
fn.isa("e", "d");

// build dependency graph and export as Graphviz DOT format
console.log(toDot(defDGraph(fn.dependencies()), { id: (id) => id }));
// digraph g {
// "b"[label="b"];
// "c"[label="c"];
// "e"[label="e"];
// "a"[label="a"];
// "d"[label="d"];
// "b" -> "a";
// "c" -> "b";
// "e" -> "d";
// }
```

![Graphviz output](https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/defmulti/readme.dot.svg)

## Authors

Karsten Schmidt
Expand Down
41 changes: 41 additions & 0 deletions packages/defmulti/tpl.readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function, but also exposes the following operations:
- `.rels()` - return all dispatch value relationships
- `.parents(id)` - direct parents of dispatch value `id`
- `.ancestors(id)` - transitive parents of dispatch value `id`
- `.dependencies()` - returns iterator of all dispatch value relationship pairs

#### Dispatch value hierarchies

Expand Down Expand Up @@ -292,6 +293,46 @@ apr({type: "isa", balance: 10000});
// Error: invalid account type: isa
```

#### Dispatch value graph visualization

To facilitate better introspection of dynamically constructed/added `defmulti()`
implementations (with possibly deep hierarchies of dispatch values), we can
utilize the `.dependencies()` method to extract all dispatch value relationships
and use these to build [dependency
graph](https://github.com/thi-ng/umbrella/tree/develop/packages/dgraph), which
then can also be visualized.

```ts
import { defDGraph } from "@thi.ng/dgraph";
import { toDot } from "@thi.ng/dgraph-dot";

const fn = defmulti((x) => x);

// dummy impls
fn.add("a", () => {});
fn.add("d", () => {});

// dispatch value relationships
fn.isa("b", "a");
fn.isa("c", "b");
fn.isa("e", "d");

// build dependency graph and export as Graphviz DOT format
console.log(toDot(defDGraph(fn.dependencies()), { id: (id) => id }));
// digraph g {
// "b"[label="b"];
// "c"[label="c"];
// "e"[label="e"];
// "a"[label="a"];
// "d"[label="d"];
// "b" -> "a";
// "c" -> "b";
// "e" -> "d";
// }
```

![Graphviz output](https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/defmulti/readme.dot.svg)

## Authors

${authors}
Expand Down

0 comments on commit e6e5fe6

Please sign in to comment.