Skip to content

Commit

Permalink
docs: update code snippets in docstrings (various pkgs)
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 21, 2024
1 parent fbc7b1b commit 1922bed
Show file tree
Hide file tree
Showing 30 changed files with 230 additions and 146 deletions.
6 changes: 3 additions & 3 deletions packages/fibers/src/fiber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ export class Fiber<T = any>
* {@link Fiber.childForID}.
*
* @example
* ```ts
* import { fiber } from "@thi.ng/fibers";
* ```ts tangle:../export/fork.ts
* import { fiber, wait } from "@thi.ng/fibers";
*
* fiber(function* (ctx) {
* console.log("main start")
Expand Down Expand Up @@ -416,7 +416,7 @@ export class Fiber<T = any>
* `handler` must only manage a single execution step, not multiple.
*
* @example
* ```ts
* ```ts tangle:../export/run-with.ts
* import { fiber } from "@thi.ng/fibers";
*
* // start with custom higher frequency handler
Expand Down
16 changes: 8 additions & 8 deletions packages/fibers/src/ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ export const timeSlice = (
* given `consume` function in order to process these values further.
*
* @example
* ```ts
* ```ts tangle:../export/timeslice-iterable.ts
* import { timeSliceIterable } from "@thi.ng/fibers";
* import { range } from "@thi.ng/transducers";
*
* // consume & batch process iterable in 16ms time slices
* timeSliceIterable(
* range(1_000_000),
* (chunk) => console.log(chunk),
* (chunk) => console.log("items:", chunk.length),
* 16
* ).run();
* ```
Expand Down Expand Up @@ -329,15 +329,15 @@ export class Shuffle extends Fiber {
* [`SYSTEM`](https://docs.thi.ng/umbrella/random/variables/SYSTEM.html).
*
* @example
* ```ts
* ```ts tangle:../export/shuffle.ts
* import { shuffle } from "@thi.ng/fibers";
* import { repeatedly } from "@thi.ng/transducers";
*
* // create & run fiber with 4 children, executing in random order
* // create & run fiber with 16 children, executing in random order
* shuffle(
* repeatedly(
* (id) => function*() { while(true) { console.log(`worker #{id}`); yield; } },
* 4
* (id) => function*() { while(true) { console.log(`worker #${id}`); yield; } },
* 16
* )
* ).run()
*
Expand Down Expand Up @@ -374,8 +374,8 @@ export const shuffle = (
* `catch` handler indicating the error has been dealt with.
*
* @example
* ```ts
* import { asPromise } from "@thi.ng/fibers";
* ```ts tangle:../export/as-promise.ts
* import { asPromise, wait } from "@thi.ng/fibers";
*
* (async () => {
* // create & spawn task/fiber
Expand Down
6 changes: 3 additions & 3 deletions packages/fuzzy-viz/src/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ import { varToHiccup } from "./var.js";
* `.clear()`.
*
* @example
* ```ts
* import { cogStrategy, gaussian } from "@thi.ng/fuzzy";
* ```ts tangle:../export/instrument-strategy.ts
* import { centroidStrategy, gaussian } from "@thi.ng/fuzzy";
* import { instrumentStrategy, fuzzySetToAscii } from "@thi.ng/fuzzy-viz";
*
* const strategy = instrumentStrategy(
* cogStrategy({ samples: 1000 }),
* centroidStrategy({ samples: 1000 }),
* fuzzySetToAscii({ width: 40, height: 8 })
* );
*
Expand Down
10 changes: 6 additions & 4 deletions packages/fuzzy/src/strategies/bisector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fit } from "@thi.ng/math/fit";
import type { DefuzzStrategy, DefuzzStrategyOpts } from "../api.js";
import { defaultOpts } from "./opts.js";
import { __defaultOpts } from "./opts.js";

/**
* Higher-order function: Bisector-of-Area defuzzification strategy. Returns
Expand All @@ -15,10 +15,12 @@ import { defaultOpts } from "./opts.js";
* Also see {@link DefuzzStrategyOpts}
*
* @example
* ```ts
* ```ts tangle:../../export/bisector-strategy.ts
* import { bisectorStrategy, trapezoid } from "@thi.ng/fuzzy";
*
* bisectorStrategy()(trapezoid(0,1,5,6), [0,6])
* console.log(
* bisectorStrategy()(trapezoid(0,1,5,6), [0,6])
* );
* // 2.97
*
* // ......▁█████████████|█████████████▁.....
Expand All @@ -37,7 +39,7 @@ import { defaultOpts } from "./opts.js";
export const bisectorStrategy = (
opts?: Partial<DefuzzStrategyOpts>
): DefuzzStrategy => {
let { samples } = defaultOpts(opts);
let { samples } = __defaultOpts(opts);
return (fn, [min, max]) => {
const delta = (max - min) / samples;
let sum: number[] = [];
Expand Down
10 changes: 6 additions & 4 deletions packages/fuzzy/src/strategies/centroid.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { DefuzzStrategy, DefuzzStrategyOpts } from "../api.js";
import { defaultOpts } from "./opts.js";
import { __defaultOpts } from "./opts.js";

/**
* Higher-order function: Centre-of-gravity defuzzification strategy, yielding
Expand All @@ -14,10 +14,12 @@ import { defaultOpts } from "./opts.js";
* Also see {@link DefuzzStrategyOpts}
*
* @example
* ```ts
* ```ts tangle:../../export/centroid-strategy.ts
* import { centroidStrategy, trapezoid } from "@thi.ng/fuzzy";
*
* centroidStrategy()(trapezoid(0,1,5,6), [0,6])
* console.log(
* centroidStrategy()(trapezoid(0,1,5,6), [0,6])
* );
* // 3.0000000000000004
*
* // ......▁█████████████|█████████████▁.....
Expand All @@ -36,7 +38,7 @@ import { defaultOpts } from "./opts.js";
export const centroidStrategy = (
opts?: Partial<DefuzzStrategyOpts>
): DefuzzStrategy => {
let { samples } = defaultOpts(opts);
let { samples } = __defaultOpts(opts);
return (fn, [min, max]) => {
const delta = (max - min) / samples;
let num = 0;
Expand Down
24 changes: 15 additions & 9 deletions packages/fuzzy/src/strategies/maxima.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { eqDelta } from "@thi.ng/math/eqdelta";
import type { DefuzzStrategy, DefuzzStrategyOpts } from "../api.js";
import { defaultOpts } from "./opts.js";
import { __defaultOpts } from "./opts.js";

/**
* Higher-order function. Returns Mean-of-Maxima defuzzification strategy,
Expand All @@ -12,10 +12,12 @@ import { defaultOpts } from "./opts.js";
* Also see {@link DefuzzStrategyOpts}
*
* @example
* ```ts
* ```ts tangle:../../export/mean-of-maxima-strategy.ts
* import { meanOfMaximaStrategy, trapezoid } from "@thi.ng/fuzzy";
*
* meanOfMaximaStrategy()(trapezoid(0,1,5,6), [0,6])
* console.log(
* meanOfMaximaStrategy()(trapezoid(0,1,5,6), [0,6])
* );
* // 3
*
* // ......▁█████████████|█████████████▁.....
Expand All @@ -34,7 +36,7 @@ import { defaultOpts } from "./opts.js";
export const meanOfMaximaStrategy = (
opts?: Partial<DefuzzStrategyOpts>
): DefuzzStrategy => {
const { samples, eps } = defaultOpts(opts);
const { samples, eps } = __defaultOpts(opts);
return (fn, [min, max]) => {
const delta = (max - min) / samples;
let peak = -Infinity;
Expand Down Expand Up @@ -67,10 +69,12 @@ export const meanOfMaximaStrategy = (
* Also see {@link DefuzzStrategyOpts}
*
* @example
* ```ts
* ```ts tangle:../../export/first-of-maxima-strategy.ts
* import { firstOfMaximaStrategy, trapezoid } from "@thi.ng/fuzzy";
*
* firstOfMaximaStrategy()(trapezoid(0,1,5,6), [0,6])
* console.log(
* firstOfMaximaStrategy()(trapezoid(0,1,5,6), [0,6])
* );
* // 1.02
*
* // ......▁|██████████████████████████▁.....
Expand All @@ -89,7 +93,7 @@ export const meanOfMaximaStrategy = (
export const firstOfMaximaStrategy = (
opts?: Partial<DefuzzStrategyOpts>
): DefuzzStrategy => {
const { samples } = defaultOpts(opts);
const { samples } = __defaultOpts(opts);
return (fn, [min, max]) => {
const delta = (max - min) / samples;
let peak = -Infinity;
Expand Down Expand Up @@ -117,10 +121,12 @@ export const firstOfMaximaStrategy = (
* Also see {@link DefuzzStrategyOpts}
*
* @example
* ```ts
* ```ts tangle:../../export/last-of-maxima-strategy.ts
* import { lastOfMaximaStrategy, trapezoid } from "@thi.ng/fuzzy";
*
* lastOfMaximaStrategy()(trapezoid(0,1,5,6), [0,6])
* console.log(
* lastOfMaximaStrategy()(trapezoid(0,1,5,6), [0,6])
* );
* // 4.98
*
* // ......▁██████████████████████████|▁.....
Expand Down
4 changes: 3 additions & 1 deletion packages/fuzzy/src/strategies/opts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// thing:no-export
import type { DefuzzStrategyOpts } from "../api.js";

export const defaultOpts = (
/** @internal */
export const __defaultOpts = (
opts?: Partial<DefuzzStrategyOpts>
): DefuzzStrategyOpts => ({
samples: 100,
Expand Down
10 changes: 5 additions & 5 deletions packages/fuzzy/src/var.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const variable = <K extends string>(
* considered. The function returns undefined if classification failed.
*
* @example
* ```ts
* ```ts tangle:../export/classify.ts
* import { classify, variable, invSigmoid, sigmoid, trapezoid } from "@thi.ng/fuzzy";
*
* // temperature sets (in celsius)
Expand All @@ -48,7 +48,7 @@ export const variable = <K extends string>(
* hot: sigmoid(30, 2)
* });
*
* classify(temp, 28)
* console.log(classify(temp, 28));
* // "warm"
* ```
*
Expand All @@ -59,7 +59,7 @@ export const variable = <K extends string>(
export const classify = <K extends string>(
{ terms }: LVar<K>,
x: number,
threshold = 0.5
threshold = 0
) => {
let max = threshold;
let maxID: Maybe<K>;
Expand All @@ -78,7 +78,7 @@ export const classify = <K extends string>(
* `x` and returns object of results.
*
* @example
* ```ts
* ```ts tangle:../export/evaluate.ts
* import { evaluate, variable, invSigmoid, sigmoid, trapezoid } from "@thi.ng/fuzzy";
*
* // temperature sets (in celsius)
Expand All @@ -89,7 +89,7 @@ export const classify = <K extends string>(
* hot: sigmoid(30, 2)
* });
*
* evaluate(temp, 28)
* console.log(evaluate(temp, 28));
* // { freezing: 0, cold: 0, warm: 0.4, hot: 0.01798620996209156 }
* ```
*
Expand Down
18 changes: 11 additions & 7 deletions packages/geom-accel/src/hash-grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export abstract class AHashGrid<T> {
* implementation. Returns neighborhood.
*
* @example
* ```ts
* ```ts tangle:../export/query-neighborhood.ts
* import { knearest2 } from "@thi.ng/distance";
* import { HashGrid2 } from "@thi.ng/geom-accel";
* import { repeatedly } from "@thi.ng/transducers";
Expand All @@ -168,12 +168,16 @@ export abstract class AHashGrid<T> {
* const grid = new HashGrid2<ReadonlyVec>((p) => p, 16, pts.length, pts);
*
* // perform k-nearest neighbor search around origin (k=5, radius=200)
* grid.queryNeighborhood(knearest2([0, 0], 5, 200)).values();
* // [-12.65, 26.19]
* // [1.94, 28.09]
* // [-17.49, -8.76]
* // [-14.55, 8.17]
* // [8.09, 17.47]
* console.log(
* grid.queryNeighborhood(knearest2([0, 0], 5, 200)).values()
* );
* // [
* // [-12.65, 26.19]
* // [1.94, 28.09]
* // [-17.49, -8.76]
* // [-14.55, 8.17]
* // [8.09, 17.47]
* // ]
* ```
*
* @param neighborhood
Expand Down
23 changes: 15 additions & 8 deletions packages/geom-axidraw/src/as-axidraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,24 @@ import { pointsByNearestNeighbor } from "./sort.js";
* - triangle
*
* @example
* ```ts
* ```ts tangle:../export/as-axidraw.ts
* import { circle } from "@thi.ng/geom";
* import { asAxiDraw } from "@thi.ng/geom-axidraw";
*
* [...asAxiDraw(circle(100), { samples: 100 })]
* [
* [ 'm', [ 10, 0 ] ],
* [ 'd' ],
* [ 'm', [ 9.980267284282716, 0.6279051952931337 ], undefined ],
* ...
* ]
* console.log(
* [...asAxiDraw(circle(100), { samples: 6 })]
* );
* // [
* // [ "M", [ 100, 0 ], 1 ],
* // [ "d", undefined, undefined ],
* // [ "M", [ 50.00, 86.60 ], 1 ],
* // [ "M", [ -49.99, 86.60 ], 1 ],
* // [ "M", [ -100, 0 ], 1 ],
* // [ "M", [ -50.00, -86.60 ], 1 ],
* // [ "M", [ 49.99, -86.60 ], 1 ],
* // [ "M", [ 100, 0 ], 1 ],
* // [ "u", undefined, undefined ]
* // ]
* ```
*/
export const asAxiDraw: MultiFn1O<
Expand Down
10 changes: 7 additions & 3 deletions packages/geom-clip-line/src/clip-with.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@ import type { ReadonlyVec } from "@thi.ng/vectors";
* other vertex, that entire chunk will be discarded.
*
* @example
* ```ts
* ```ts tangle:../export/clip-polyline-with.ts
* import { clipPolylineWith } from "@thi.ng/geom-clip-line";
*
* const pts = [[0, 0], [1, 0], [1, 1], [2, 1], [3, 1], [4, 0], [5, 0]];
*
* // isolate horizontal chunks
* clipPolylineWith((a, b) => a[1] == b[1], pts)
* console.log(
* clipPolylineWith((a, b) => a[1] == b[1], pts)
* );
* // [
* // [[0, 0], [1, 0]],
* // [[1, 1], [2, 1], [3, 1]],
* // [[4, 0], [5, 0]]
* // ]
*
* // isolate sloped chunks
* clipPolylineWith((a, b) => a[1] != b[1], pts)
* console.log(
* clipPolylineWith((a, b) => a[1] != b[1], pts)
* );
* // [
* // [[1, 0], [1, 1]],
* // [[3, 1], [4, 0]]
Expand Down
11 changes: 8 additions & 3 deletions packages/geom-poly-utils/src/bounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ import { mulN2, mulN3 } from "@thi.ng/vectors/muln";
* to `+∞` and `vmax` to `-∞` (e.g. use copies of `MIN*` / `MAX*`
* constants defined in thi.ng/vectors).
*
* @remarks
* Also see {@link bounds2}, {@link bounds3}.
*
* @example
* ```ts
* ```ts tangle:../export/bounds.ts
* import { bounds } from "@thi.ng/geom-poly-utils";
* import { MAX2, MIN2 } from "@thi.ng/vectors";
*
* points = [[-1,-2], [5,-3], [0,4]];
* const points = [[-1,-2], [5,-3], [0,4]];
*
* bounds(points, [...MAX2], [...MIN2])
* console.log(
* bounds(points, [...MAX2], [...MIN2])
* );
* // [[-1, -3], [5, 4]]
* ```
*
Expand Down
Loading

0 comments on commit 1922bed

Please sign in to comment.