functionalscript 0.11.6 → 0.11.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.11.6",
3
+ "version": "0.11.7",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "**/*.js",
@@ -169,7 +169,7 @@ export declare const msb: BitOrder;
169
169
  * @param list The list of unsigned 8-bit integers to be converted.
170
170
  * @returns The resulting vector based on the provided bit order.
171
171
  */
172
- export declare const u8ListToVec: (bo: BitOrder) => (list: List<number>) => Vec;
172
+ export declare const u8ListToVec: ({ concat }: BitOrder) => (list: List<number>) => Vec;
173
173
  /**
174
174
  * Converts a bit vector to a list of unsigned 8-bit integers based on the provided bit order.
175
175
  *
@@ -23,7 +23,7 @@
23
23
  */
24
24
  import { bitLength, mask, max, min, xor } from "../bigint/module.f.js";
25
25
  import { flip } from "../function/module.f.js";
26
- import { fold } from "../list/module.f.js";
26
+ import { entries, fold, iterable } from "../list/module.f.js";
27
27
  import { asBase, asNominal } from "../nominal/module.f.js";
28
28
  import { repeat as mRepeat } from "../monoid/module.f.js";
29
29
  import { cmp } from "../function/compare/module.f.js";
@@ -169,7 +169,6 @@ export const msb = {
169
169
  concat: flip(lsb.concat),
170
170
  xor: op(msbNorm)(xor)
171
171
  };
172
- const appendU8 = ({ concat }) => (u8) => (a) => concat(a)(vec8(BigInt(u8)));
173
172
  /**
174
173
  * Converts a list of unsigned 8-bit integers to a bit vector using the provided bit order.
175
174
  *
@@ -177,7 +176,34 @@ const appendU8 = ({ concat }) => (u8) => (a) => concat(a)(vec8(BigInt(u8)));
177
176
  * @param list The list of unsigned 8-bit integers to be converted.
178
177
  * @returns The resulting vector based on the provided bit order.
179
178
  */
180
- export const u8ListToVec = (bo) => fold(appendU8(bo))(empty);
179
+ export const u8ListToVec = ({ concat }) => (list) => {
180
+ // much faster than: `fold(appendU8(bo))(empty)(list)`
181
+ // where `appendU8` is defined as
182
+ // ```
183
+ // const appendU8 = ({ concat }: BitOrder) => (u8: number) => (a: Vec) =>
184
+ // concat(a)(vec8(BigInt(u8)))
185
+ // ```
186
+ let result = [];
187
+ for (const b of iterable(list)) {
188
+ let v = vec8(BigInt(b));
189
+ let i = 0;
190
+ while (true) {
191
+ if (result.length <= i) {
192
+ result = [...result, v];
193
+ break;
194
+ }
195
+ const old = result[i];
196
+ if (old === empty) {
197
+ result = result.toSpliced(i, 1, v);
198
+ break;
199
+ }
200
+ result = result.toSpliced(i, 1, empty);
201
+ v = concat(old)(v);
202
+ i++;
203
+ }
204
+ }
205
+ return result.reduce((p, c) => concat(c)(p), empty);
206
+ };
181
207
  /**
182
208
  * Converts a bit vector to a list of unsigned 8-bit integers based on the provided bit order.
183
209
  *
@@ -38,5 +38,6 @@ declare const _default: {
38
38
  msbXor: () => void;
39
39
  repeat: () => void;
40
40
  msbCmp: () => void;
41
+ u8ListToVec: () => void;
41
42
  };
42
43
  export default _default;
@@ -1,6 +1,7 @@
1
1
  import { mask } from "../bigint/module.f.js";
2
2
  import { asBase, asNominal } from "../nominal/module.f.js";
3
- import { length, empty, uint, vec, lsb, msb, repeat, vec8, msbCmp } from "./module.f.js";
3
+ import { length, empty, uint, vec, lsb, msb, repeat, vec8, msbCmp, u8ListToVec } from "./module.f.js";
4
+ import { repeat as listRepeat } from "../list/module.f.js";
4
5
  const unsafeVec = (a) => asNominal(a);
5
6
  // 0x8 = 0b1000 = 0 + 8
6
7
  // 0x9 = 0b1001 = 1 + 8
@@ -442,5 +443,9 @@ export default {
442
443
  c(vec(4n)(0x5n))(vec(5n)(0x5n))(1); // 0b0101_ < 0b00101
443
444
  c(vec(5n)(0x5n))(vec(4n)(0x5n))(-1); // 0b00101 < 0b0101_
444
445
  c(vec(4n)(0x5n))(vec(5n)(0xan))(-1); // 0b0101_ < 0b01010
446
+ },
447
+ u8ListToVec: () => {
448
+ // 131_072 is too much for Bun
449
+ const x = u8ListToVec(msb)(listRepeat(0x12)(131_071));
445
450
  }
446
451
  };