UNPKG

functionalscript

Version:

FunctionalScript is a purely functional subset of JavaScript

140 lines (139 loc) 5.04 kB
import { sum, min, max, cmp, countOnes } from "./module.f.js"; export default { sum: () => { const result = sum([2, 3, 4, 5]); if (result !== 14) { throw result; } }, min: { empty: () => { const result = min([]); if (result !== null) { throw result; } }, multi: () => { const result = min([1, 2, 12, -4, 8]); if (result !== -4) { throw result; } } }, max: () => { const result = max([1, 2, 12, -4, 8]); if (result !== 12) { throw result; } }, cmp: () => { const result = cmp(4)(5); if (result !== -1) { throw result; } }, standard: () => { const check = a => b => { if (BigInt(Number(a)) !== b) { throw [a, b]; } }; const eq = v => check(v)(v); // 53, 0x35 bits. // 3 2 1 0 // 4_3210_FEDC_BA98_7654_3210_FEDC_BA98_7654_3210_FEDC_BA98_7654_3210 eq(4503599627370496n); eq(4503599627370497n); eq(4503599627370498n); eq(4503599627370499n); eq(4503599627370500n); // eq(8205552883602248n); eq(6023495077481713n); eq(7655550916643882n); eq(7408615831253283n); eq(5372046171422892n); // eq(9007199254740991n); eq(9007199254740990n); eq(9007199254740989n); eq(9007199254740988n); eq(9007199254740987n); eq(9007199254740986n); eq(9007199254740985n); eq(9007199254740984n); // 54, 0x35+1 bits. // 3 2 1 0 // 4_3210_FEDC_BA98_7654_3210_FEDC_BA98_7654_3210_FEDC_BA98_7654_3210_1 check(9007199254740992n)(9007199254740992n); check(16585805050637558n)(16585805050637558n); check(18014398509481982n)(18014398509481982n); // round down check(9007199254740993n)(9007199254740992n); check(9007199254740997n)(9007199254740996n); check(11727370849309157n)(11727370849309156n); check(18014398509481981n)(18014398509481980n); // round up check(9007199254740995n)(9007199254740996n); check(15679542343753527n)(15679542343753528n); check(18014398509481983n)(18014398509481984n); // 55, 0x35+2 bits. // 3 2 1 0 // 4_3210_FEDC_BA98_7654_3210_FEDC_BA98_7654_3210_FEDC_BA98_7654_3210_12 // 0_xx: down, down, up // 0_00 check(18014398509481984n)(18014398509481984n); check(31009640954244032n)(31009640954244032n); check(36028797018963960n)(36028797018963960n); // 0_01 round down check(18014398509481985n)(18014398509481984n); check(35865859090492321n)(35865859090492320n); check(36028797018963961n)(36028797018963960n); // 0_10 round down check(18014398509481986n)(18014398509481984n); check(20537830900813786n)(20537830900813784n); check(36028797018963962n)(36028797018963960n); // 0_11 round up check(18014398509481987n)(18014398509481988n); check(20762549594556867n)(20762549594556868n); check(36028797018963963n)(36028797018963964n); // 1_xx: down, up, up // 1_00 check(18014398509481988n)(18014398509481988n); check(27158415062912468n)(27158415062912468n); check(36028797018963963n)(36028797018963964n); // 1_01 round down check(18014398509481989n)(18014398509481988n); check(23136524663958789n)(23136524663958788n); check(36028797018963965n)(36028797018963964n); // 1_10 round up check(18014398509481990n)(18014398509481992n); check(35732739218314830n)(35732739218314832n); check(36028797018963966n)(36028797018963968n); // 1_11 round up check(18014398509481991n)(18014398509481992n); check(35015291789494295n)(35015291789494296n); check(36028797018963967n)(36028797018963968n); // 57, 0x35+4 bits. // 3 2 1 0 // 4_3210_FEDC_BA98_7654_3210_FEDC_BA98_7654_3210_FEDC_BA98_7654_3210_1234 check(104002482718319358n)(104002482718319360n); check(142693895881191444n)(142693895881191440n); }, countOnes: [ () => { // 1121 2231 = 5 + 8 = 13 const r = countOnes(0x1234_5678); if (r !== 13) { throw r; } }, () => { // 2232 3340 = 9 + 10 = 19 const r = countOnes(0x9ABC_DEF0); if (r !== 19) { throw r; } } ] };