UNPKG

functionalscript

Version:

FunctionalScript is a purely functional subset of JavaScript

72 lines (71 loc) 1.9 kB
import { values } from "./module.f.js"; import * as json from "../../json/module.f.js"; import { sort } from "../object/module.f.js"; import { cmp } from "../string/module.f.js"; import { next, toArray } from "../list/module.f.js"; import * as s from "./set/module.f.js"; import * as f from "./find/module.f.js"; const jsonStr = json.stringify(sort); const stringify = sequence => jsonStr(toArray(sequence)); const set = node => value => s.set(cmp(value))(() => value)(node); const valueTest1 = () => { let _map = ['a']; _map = set(_map)('b'); _map = set(_map)('c'); _map = set(_map)('d'); _map = set(_map)('e'); _map = set(_map)('f'); const result = stringify(values(_map)); if (result !== '["a","b","c","d","e","f"]') { throw result; } }; const valuesTest2 = () => { let _map = ['1']; for (let i = 2; i <= 10; i++) _map = set(_map)((i * i).toString()); const result = stringify(values(_map)); if (result !== '["1","100","16","25","36","4","49","64","81","9"]') { throw result; } }; const findTrue = () => { let _map = ['a']; _map = set(_map)('b'); _map = set(_map)('c'); const result = f.value(f.find(cmp('b'))(_map).first); if (result !== 'b') { throw result; } }; const find = () => { let _map = ['a']; _map = set(_map)('b'); _map = set(_map)('c'); const result = f.value(f.find(cmp('e'))(_map).first); if (result !== null) { throw result; } }; const test = () => { let _map = ['a']; _map = set(_map)('b'); _map = set(_map)('c'); _map = set(_map)('d'); _map = set(_map)('e'); _map = set(_map)('f'); // { let _item = next(values(_map)); while (_item !== null) { _item = next(_item.tail); } } }; export default { valueTest1, valuesTest2, findTrue, find, test, };