UNPKG

functionalscript

Version:

FunctionalScript is a purely functional subset of JavaScript

26 lines (25 loc) 1.17 kB
import { compose } from "../function/module.f.js"; import { reverse, countdown, flat, map } from "../list/module.f.js"; export const has = n => s => ((s >> BigInt(n)) & 1n) === 1n; // create a set export const empty = 0n; // 0 1 2 3 4 5 6 7 8 9 A B C D E F export const universe = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn; export const one = n => 1n << BigInt(n); export const range = ([b, e]) => one(e - b + 1) - 1n << BigInt(b); // set operations export const union = a => b => a | b; const intersect = a => b => a & b; export const complement = n => universe ^ n; const difference = compose(intersect)(compose(complement)); // additional operations export const set = compose(one)(union); export const setRange = compose(range)(union); export const unset = n => s => difference(s)(one(n)); const counter = reverse(countdown(256)); const toRangeMapOp = n => s => i => { const current = has(i + 1)(n); const prev = has(i)(n); return current === prev ? null : [[prev ? [s] : [], i]]; }; export const toRangeMap = n => s => flat(map(toRangeMapOp(n)(s))(counter));