UNPKG

functionalscript

Version:

FunctionalScript is a purely functional subset of JavaScript

38 lines (37 loc) 1.23 kB
/** * A set of strings implemented as a B-Tree. * * @module * * @example * * ```js * import { set, contains, remove, fromValues, values, empty } from './module.d.ts'; * * let mySet = fromValues(['apple', 'banana', 'cherry']); * if (!contains('banana')(mySet)) { throw '1' } * if (contains('date')(mySet)) { throw '2' } * * mySet = set('date')(mySet); * if (!contains('date')(mySet)) { throw '3' } * * mySet = remove('banana')(mySet); * if (contains('banana')(mySet)) { throw '4' } * ``` */ import * as btree from "../btree/module.f.js"; import { find, isFound } from "../btree/find/module.f.js"; import { remove as btreeRemove } from "../btree/remove/module.f.js"; import { set as btreeSet } from "../btree/set/module.f.js"; import { cmp } from "../string/module.f.js"; import { fold } from "../list/module.f.js"; import { compose } from "../function/module.f.js"; export const values = btree.values; export const empty = btree.empty; export const contains = value => { const f = find(cmp(value)); return s => s !== null && isFound(f(s).first); }; export const set = value => btreeSet(cmp(value))(() => value); export const fromValues = fold(set)(null); export const remove = compose(cmp)(btreeRemove);