Skip to content

Commit

Permalink
feat(rstream-graph): add node2(), update sub/div
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 22, 2019
1 parent 4ebf20e commit 5214f9a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
16 changes: 15 additions & 1 deletion packages/rstream-graph/src/graph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IObjectOf } from "@thi.ng/api";
import { IObjectOf, Tuple } from "@thi.ng/api";
import { IAtom } from "@thi.ng/atom";
import { isFunction, isPlainObject, isString } from "@thi.ng/checks";
import { illegalArgs } from "@thi.ng/errors";
Expand Down Expand Up @@ -270,6 +270,20 @@ export const node1 = (
xform ? src[inputID].subscribe(xform, id) : src[inputID].subscribe({}, id)
);

/**
* Syntax sugar for `node()`, intended for nodes w/ 2 inputs, by default
* named `a` & `b` (but can be overridden).
*
* @param xform
* @param inputIDs
* @param reset
*/
export const node2 = (
xform: Transducer<IObjectOf<any>, any>,
inputIDs: Tuple<string, 2> = ["a", "b"],
reset = false
) => node(xform, inputIDs, reset);

/**
* Helper function to verify given object of inputs has required input IDs.
* Throws error if validation fails.
Expand Down
12 changes: 5 additions & 7 deletions packages/rstream-graph/src/nodes/math.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IObjectOf } from "@thi.ng/api";
import { map } from "@thi.ng/transducers";
import { NodeFactory } from "../api";
import { node } from "../graph";
import { node, node2 } from "../graph";

/**
* Addition node.
Expand Down Expand Up @@ -42,17 +42,15 @@ export const mul: NodeFactory<number> = node(
*
* Inputs: `a`, `b`
*/
export const sub: NodeFactory<number> = node(
map((ports: IObjectOf<number>) => ports.a - ports.b),
["a", "b"]
export const sub: NodeFactory<number> = node2(
map((ports: IObjectOf<number>) => ports.a - ports.b)
);

/**
* Division node.
*
* Inputs: `a`, `b`
*/
export const div: NodeFactory<number> = node(
map((ports: IObjectOf<number>) => ports.a / ports.b),
["a", "b"]
export const div: NodeFactory<number> = node2(
map((ports: IObjectOf<number>) => ports.a / ports.b)
);

0 comments on commit 5214f9a

Please sign in to comment.