functionalscript 0.5.1 → 0.6.1

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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import { index } from "../module.f.js";
2
+ import node from "../../io/module.js";
3
+ await node(index);
package/dev/module.f.d.ts CHANGED
@@ -1,22 +1,12 @@
1
1
  import type { Io } from '../io/module.f.ts';
2
- import type { Sign } from '../types/function/compare/module.f.ts';
3
2
  export declare const todo: () => never;
4
3
  export type Module = {
5
4
  readonly default?: unknown;
6
5
  };
7
- export type UnknownMap = {
8
- readonly [k in string]: unknown;
9
- };
10
- type Entry<T> = readonly [string, T];
11
- export declare const cmp: ([a]: Entry<unknown>, [b]: Entry<unknown>) => Sign;
12
6
  export type ModuleMap = {
13
7
  readonly [k in string]: Module;
14
8
  };
15
- export declare const remove_tail: (v: readonly string[]) => (dif: number) => string[];
16
- export type MutableModuleMap = {
17
- [k in string]: Module;
18
- };
19
9
  export declare const env: (io: Io) => (v: string) => string | undefined;
20
- export declare const loadModuleMap: ({ fs: { promises: { readdir }, existsSync }, asyncImport }: Io) => Promise<ModuleMap>;
10
+ export declare const allFiles: ({ fs: { promises: { readdir } } }: Io) => Promise<readonly string[]>;
11
+ export declare const loadModuleMap: (io: Io) => Promise<ModuleMap>;
21
12
  export declare const index: (io: Io) => Promise<number>;
22
- export {};
package/dev/module.f.js CHANGED
@@ -1,77 +1,51 @@
1
1
  import { updateVersion } from "./version/module.f.js";
2
2
  export const todo = () => { throw 'not implemented'; };
3
- export const cmp = ([a], [b]) => a < b ? -1 : a > b ? 1 : 0;
4
- export const remove_tail = (v) => (dif) => v.slice(0, v.length - dif);
3
+ const cmp = ([a], [b]) => a < b ? -1 : a > b ? 1 : 0;
5
4
  export const env = ({ process: { env } }) => a => {
6
5
  const r = Object.getOwnPropertyDescriptor(env, a);
7
6
  return r === undefined ? undefined :
8
7
  typeof r.get === 'function' ? r.get() :
9
8
  r.value;
10
9
  };
11
- export const loadModuleMap = async ({ fs: { promises: { readdir }, existsSync }, asyncImport }) => {
12
- const load = async () => {
13
- const map = [];
14
- const f = async (p) => {
15
- for (const i of await readdir(p, { withFileTypes: true })) {
16
- const { name } = i;
17
- if (!name.startsWith('.')) {
18
- const file = `${p}/${name}`;
19
- if (i.isDirectory()) {
20
- await f(file);
21
- continue;
22
- }
23
- if (name.endsWith('.f.mjs') ||
24
- name.endsWith('.f.js') ||
25
- (name.endsWith('.f.ts') && !existsSync(file.substring(0, file.length - 3) + '.js'))) {
26
- const source = await asyncImport(`../${file}`);
27
- map.push([file, source.default]);
28
- }
10
+ export const allFiles = ({ fs: { promises: { readdir } } }) => {
11
+ const load = async (p) => {
12
+ let result = [];
13
+ for (const i of await readdir(p, { withFileTypes: true })) {
14
+ const { name } = i;
15
+ if (!name.startsWith('.')) {
16
+ const file = `${p}/${name}`;
17
+ if (i.isDirectory()) {
18
+ result = [...result, ...await load(file)];
19
+ continue;
29
20
  }
30
- }
31
- };
32
- await f('.');
33
- map.sort(cmp);
34
- return Object.fromEntries(map);
35
- };
36
- const map = await load();
37
- const build = () => {
38
- const d = {};
39
- const getModule = (base) => (k) => {
40
- const relativePath = k.split('/');
41
- const dif = relativePath.filter(v => v === '..').length;
42
- const path = [remove_tail(base)(dif), relativePath.filter(v => !['..', '.'].includes(v))]
43
- .flat();
44
- const pathStr = path.join('/');
45
- {
46
- const module = d[pathStr];
47
- if (module !== void 0) {
48
- return [pathStr, module];
21
+ if (name.endsWith('.js') || name.endsWith('.ts')) {
22
+ result = [...result, file];
49
23
  }
50
24
  }
51
- {
52
- const module = { default: map[pathStr] };
53
- d[pathStr] = module;
54
- return [pathStr, module];
55
- }
56
- };
57
- {
58
- const get = getModule(['.']);
59
- for (const k of Object.keys(map)) {
60
- get(k);
61
- }
62
25
  }
63
- return d;
26
+ return result;
64
27
  };
65
- return build();
28
+ return load('.');
29
+ };
30
+ export const loadModuleMap = async (io) => {
31
+ const { fs: { existsSync }, asyncImport } = io;
32
+ let map = [];
33
+ for (const f of await allFiles(io)) {
34
+ if (f.endsWith('.f.js') ||
35
+ (f.endsWith('.f.ts') && !existsSync(f.substring(0, f.length - 3) + '.js'))) {
36
+ const source = await asyncImport(`../${f}`);
37
+ map = [...map, [f, source]];
38
+ }
39
+ }
40
+ return Object.fromEntries(map.toSorted(cmp));
66
41
  };
67
42
  export const index = async (io) => {
68
43
  updateVersion(io);
69
44
  const jj = './deno.json';
70
- const n = '/module.f.ts';
71
45
  const jsr_json = JSON.parse(await io.fs.promises.readFile(jj, 'utf8'));
72
- const list = Object.keys(await loadModuleMap(io)).filter(v => v.endsWith(n));
46
+ const list = (await allFiles(io)).filter(v => v.endsWith('/module.f.ts') || v.endsWith('/module.ts'));
73
47
  //console.log(list)
74
- const exportsA = list.filter(v => !v.startsWith('./out')).map(v => [v.replace(n, ''), `./${v.substring(2)}`]);
48
+ const exportsA = list.map(v => [v, `./${v.substring(2)}`]);
75
49
  // console.log(exportsA)
76
50
  const exports = Object.fromEntries(exportsA);
77
51
  // console.log(exports)
@@ -0,0 +1 @@
1
+ export {};
@@ -1,11 +1,6 @@
1
1
  import type * as Result from '../../types/result/module.f.ts';
2
2
  import type { Io, Performance } from '../../io/module.f.ts';
3
- type Module = {
4
- readonly default?: unknown;
5
- };
6
- type ModuleMap = {
7
- readonly [k in string]: Module;
8
- };
3
+ import { type ModuleMap } from '../module.f.ts';
9
4
  type Log<T> = (v: string) => (state: T) => T;
10
5
  type Measure<T> = <R>(f: () => R) => (state: T) => readonly [R, number, T];
11
6
  type Input<T> = {
@@ -1,7 +1,7 @@
1
1
  import { entries, fold } from "../../types/list/module.f.js";
2
2
  import { reset, fgGreen, fgRed, bold } from "../../text/sgr/module.f.js";
3
3
  import { env, loadModuleMap } from "../module.f.js";
4
- const isTest = (s) => s.endsWith('test.f.mjs') || s.endsWith('test.f.js') || s.endsWith('test.f.ts');
4
+ const isTest = (s) => s.endsWith('test.f.js') || s.endsWith('test.f.ts');
5
5
  const addPass = (delta) => (ts) => ({ ...ts, time: ts.time + delta, pass: ts.pass + 1 });
6
6
  const addFail = (delta) => (ts) => ({ ...ts, time: ts.time + delta, fail: ts.fail + 1 });
7
7
  const timeFormat = (a) => {
@@ -15,7 +15,7 @@ const timeFormat = (a) => {
15
15
  };
16
16
  export const test = (input) => {
17
17
  let { moduleMap, log, error, measure, tryCatch, env, state } = input;
18
- const isGitHub = env('GITHUB_ACTION') !== void 0;
18
+ const isGitHub = env('GITHUB_ACTION') !== undefined;
19
19
  const f = ([k, v]) => {
20
20
  const test = i => v => ([ts, state]) => {
21
21
  const next = test(`${i}| `);
@@ -91,5 +91,5 @@ export const main = async (io) => test({
91
91
  measure: measure(io.performance),
92
92
  tryCatch: io.tryCatch,
93
93
  env: env(io),
94
- state: void 0,
94
+ state: undefined,
95
95
  })[0];
@@ -0,0 +1,3 @@
1
+ import { main } from "./module.f.js";
2
+ import node from "../../io/module.js";
3
+ await node(main);
@@ -1,12 +1,12 @@
1
1
  import * as result from '../../types/result/module.f.ts';
2
2
  import { type List } from '../../types/list/module.f.ts';
3
3
  import type { DjsToken } from '../tokenizer/module.f.ts';
4
- import { type Map } from '../../types/map/module.f.ts';
4
+ import { type OrderedMap } from '../../types/ordered_map/module.f.ts';
5
5
  import type { Fs } from '../../io/module.f.ts';
6
6
  import type { AstModule } from '../ast/module.f.ts';
7
7
  export type ParseContext = {
8
8
  readonly fs: Fs;
9
- readonly complete: Map<result.Result<AstModule, string>>;
9
+ readonly complete: OrderedMap<result.Result<AstModule, string>>;
10
10
  readonly stack: List<string>;
11
11
  };
12
12
  export declare const parseFromTokens: (tokenList: List<DjsToken>) => result.Result<AstModule, string>;
@@ -1,6 +1,6 @@
1
1
  import * as result from "../../types/result/module.f.js";
2
2
  import { fold, first, drop, toArray, length, concat } from "../../types/list/module.f.js";
3
- import { setReplace, at } from "../../types/map/module.f.js";
3
+ import { setReplace, at } from "../../types/ordered_map/module.f.js";
4
4
  import { fromMap } from "../../types/object/module.f.js";
5
5
  const parseInitialOp = token => state => {
6
6
  switch (token.kind) {
@@ -1,11 +1,11 @@
1
1
  import type * as djs from '../module.f.ts';
2
2
  import { type Result } from '../../types/result/module.f.ts';
3
3
  import { type List } from '../../types/list/module.f.ts';
4
- import { type Map } from '../../types/map/module.f.ts';
4
+ import { type OrderedMap } from '../../types/ordered_map/module.f.ts';
5
5
  import type { Fs } from '../../io/module.f.ts';
6
6
  export type ParseContext = {
7
7
  readonly fs: Fs;
8
- readonly complete: Map<djsResult>;
8
+ readonly complete: OrderedMap<djsResult>;
9
9
  readonly stack: List<string>;
10
10
  readonly error: string | null;
11
11
  };
@@ -1,7 +1,7 @@
1
1
  import { error, ok } from "../../types/result/module.f.js";
2
2
  import { fold, drop, map as listMap, toArray, includes } from "../../types/list/module.f.js";
3
3
  import { tokenize } from "../tokenizer/module.f.js";
4
- import { setReplace, at } from "../../types/map/module.f.js";
4
+ import { setReplace, at } from "../../types/ordered_map/module.f.js";
5
5
  import { stringToList } from "../../text/utf16/module.f.js";
6
6
  import { concat as pathConcat } from "../../path/module.f.js";
7
7
  import { parseFromTokens } from "../parser/module.f.js";
@@ -1,5 +1,5 @@
1
1
  import { sort } from "../../types/object/module.f.js";
2
- import { setReplace } from "../../types/map/module.f.js";
2
+ import { setReplace } from "../../types/ordered_map/module.f.js";
3
3
  import { transpile } from "./module.f.js";
4
4
  import { stringify } from "../serializer/module.f.js";
5
5
  import { createVirtualIo } from "../../io/virtual.f.js";
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/fsc/module.js ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ import node from "../io/module.js";
3
+ import { compile } from "../djs/module.f.js";
4
+ await node(compile);
package/io/module.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { type Io, type Run } from './module.f.ts';
2
+ export declare const io: Io;
3
+ declare const runDefault: Run;
4
+ export default runDefault;
package/io/module.f.d.ts CHANGED
@@ -13,6 +13,13 @@ export type Dirent = {
13
13
  readonly isDirectory: () => boolean;
14
14
  readonly isFile: () => boolean;
15
15
  };
16
+ export type RmOptions = {
17
+ readonly force?: boolean;
18
+ readonly recursive?: boolean;
19
+ };
20
+ export type MakeDirectoryOptions = {
21
+ readonly recursive?: boolean;
22
+ };
16
23
  /**
17
24
  * File system operations interface
18
25
  * @see https://nodejs.org/api/fs.html
@@ -27,6 +34,9 @@ export type Fs = {
27
34
  readonly readdir: (path: string, options: {
28
35
  withFileTypes: true;
29
36
  }) => Promise<Dirent[]>;
37
+ readonly rm: (path: string, options?: RmOptions) => Promise<void>;
38
+ readonly mkdir: (path: string, options?: MakeDirectoryOptions) => Promise<string | undefined>;
39
+ readonly copyFile: (src: string, dest: string) => Promise<void>;
30
40
  };
31
41
  };
32
42
  /**
@@ -77,7 +87,8 @@ export type Process = {
77
87
  export type Env = {
78
88
  readonly [k: string]: string | undefined;
79
89
  };
80
- export type Run = (f: (io: Io) => Promise<number>) => Promise<never>;
90
+ export type App = (io: Io) => Promise<number>;
91
+ export type Run = (f: App) => Promise<never>;
81
92
  /**
82
93
  * Runs a function and exits the process with the returned code
83
94
  * Handles errors by exiting with code 1
package/io/module.js ADDED
@@ -0,0 +1,36 @@
1
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
2
+ if (typeof path === "string" && /^\.\.?\//.test(path)) {
3
+ return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
4
+ return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
5
+ });
6
+ }
7
+ return path;
8
+ };
9
+ import { run } from "./module.f.js";
10
+ import fs from 'node:fs';
11
+ import process from "node:process";
12
+ export const io = {
13
+ console,
14
+ fs,
15
+ process,
16
+ asyncImport: (v) => import(__rewriteRelativeImportExtension(v)),
17
+ performance,
18
+ tryCatch: f => {
19
+ try {
20
+ return ['ok', f()];
21
+ }
22
+ catch (e) {
23
+ return ['error', e];
24
+ }
25
+ },
26
+ asyncTryCatch: async (f) => {
27
+ try {
28
+ return ['ok', await f()];
29
+ }
30
+ catch (e) {
31
+ return ['error', e];
32
+ }
33
+ },
34
+ };
35
+ const runDefault = run(io);
36
+ export default runDefault;
package/io/virtual.f.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import type { Io } from './module.f.ts';
2
- import { type Map } from '../types/map/module.f.ts';
3
- export declare const createVirtualIo: (files: Map<string>) => Io;
2
+ import { type OrderedMap } from '../types/ordered_map/module.f.ts';
3
+ export declare const createVirtualIo: (files: OrderedMap<string>) => Io;
package/io/virtual.f.js CHANGED
@@ -1,4 +1,4 @@
1
- import { at } from "../types/map/module.f.js";
1
+ import { at } from "../types/ordered_map/module.f.js";
2
2
  export const createVirtualIo = (files) => ({
3
3
  console: {
4
4
  log: (..._d) => { },
@@ -12,6 +12,9 @@ export const createVirtualIo = (files) => ({
12
12
  readdir: (_path) => Promise.resolve([]),
13
13
  readFile: (_path, _options) => Promise.resolve(''),
14
14
  writeFile: (_path, _data, _options) => Promise.resolve(),
15
+ rm: (_path, _options) => Promise.resolve(),
16
+ mkdir: (_path, _options) => Promise.resolve(undefined),
17
+ copyFile: (_src, _dest) => Promise.resolve(),
15
18
  }
16
19
  },
17
20
  process: {
@@ -2,7 +2,7 @@ import * as operator from "../../types/function/operator/module.f.js";
2
2
  import * as range_map from "../../types/range_map/module.f.js";
3
3
  const { merge, fromRange, get } = range_map;
4
4
  import * as list from "../../types/list/module.f.js";
5
- import * as map from "../../types/map/module.f.js";
5
+ import * as map from "../../types/ordered_map/module.f.js";
6
6
  const { at } = map;
7
7
  import * as _range from "../../types/range/module.f.js";
8
8
  const { one } = _range;
@@ -1,5 +1,5 @@
1
1
  import * as result from '../../types/result/module.f.ts';
2
- import * as list from '../../types/list/module.f.ts';
2
+ import { type List } from '../../types/list/module.f.ts';
3
3
  import type * as Tokenizer from '../tokenizer/module.f.ts';
4
4
  import type * as Json from '../module.f.ts';
5
- export declare const parse: (tokenList: list.List<Tokenizer.JsonToken>) => result.Result<Json.Unknown, string>;
5
+ export declare const parse: (tokenList: List<Tokenizer.JsonToken>) => result.Result<Json.Unknown, string>;
@@ -1,13 +1,10 @@
1
1
  import * as result from "../../types/result/module.f.js";
2
- import * as list from "../../types/list/module.f.js";
3
- const { fold, first, drop, toArray } = list;
4
- import * as map from "../../types/map/module.f.js";
5
- const { setReplace } = map;
6
- import * as o from "../../types/object/module.f.js";
7
- const { fromMap } = o;
2
+ import { fold, first, drop, toArray, concat } from "../../types/list/module.f.js";
3
+ import { setReplace } from "../../types/ordered_map/module.f.js";
4
+ import { fromMap } from "../../types/object/module.f.js";
8
5
  const addKeyToObject = obj => key => ({ kind: 'object', values: obj.values, key: key });
9
6
  const addValueToObject = obj => value => ({ kind: 'object', values: setReplace(obj.key)(value)(obj.values), key: '' });
10
- const addToArray = array => value => ({ kind: 'array', values: list.concat(array.values)([value]) });
7
+ const addToArray = array => value => ({ kind: 'array', values: concat(array.values)([value]) });
11
8
  const pushKey = state => value => {
12
9
  if (state.top?.kind === 'object') {
13
10
  return { status: '{k', top: addKeyToObject(state.top)(value), stack: state.stack };
package/package.json CHANGED
@@ -1,31 +1,29 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.5.1",
3
+ "version": "0.6.1",
4
4
  "type": "module",
5
5
  "files": [
6
- "fsc.js",
7
- "io/node-io.js",
8
- "**/*.f.d.ts",
9
- "**/*.f.js"
6
+ "**/*.js",
7
+ "**/*.d.ts"
10
8
  ],
11
9
  "description": "FunctionalScript is a functional subset of JavaScript",
12
10
  "scripts": {
13
11
  "tsc-emit": "tsc --NoEmit false",
14
- "n": "node --frozen-intrinsics --trace-uncaught",
12
+ "n": "node --trace-uncaught",
15
13
  "prepack": "npm run tsc-emit",
16
14
  "git-clean": "git clean -xf",
17
- "test20": "npm run tsc-emit && npm run n ./dev/test.js",
18
- "test22": "tsc && npm run n -- --experimental-strip-types ./dev/test.ts",
19
- "test": "tsc && npm run n ./dev/test.ts",
20
- "index": "npm run n ./dev/index.ts",
21
- "fsc": "npm run n ./fsc.ts",
15
+ "test20": "npm run tsc-emit && npm run n ./dev/test/module.js",
16
+ "test22": "tsc && npm run n -- --experimental-strip-types ./dev/test/module.ts",
17
+ "test": "tsc && npm run n ./dev/test/module.ts",
18
+ "index": "npm run n ./dev/index/module.ts",
19
+ "fsc": "npm run n ./fsc/module.ts",
22
20
  "update": "npm run index && npm install"
23
21
  },
24
22
  "engines": {
25
- "node": ">=16"
23
+ "node": ">=20"
26
24
  },
27
25
  "bin": {
28
- "fsc": "fsc.js"
26
+ "fsc": "fsc/module.js"
29
27
  },
30
28
  "repository": {
31
29
  "type": "git",
@@ -47,7 +45,7 @@
47
45
  },
48
46
  "homepage": "https://github.com/functionalscript/functionalscript#readme",
49
47
  "devDependencies": {
50
- "@types/node": "^22.13.14",
51
- "typescript": "^5.8.2"
48
+ "@types/node": "^22.14",
49
+ "typescript": "^5.8.3"
52
50
  }
53
51
  }
@@ -1,12 +1,2 @@
1
- import type { Tree } from '../btree/types/module.f.ts';
2
- import { type List } from '../list/module.f.ts';
3
- import type { Reduce } from '../function/operator/module.f.ts';
4
- export type Entry<T> = readonly [string, T];
5
- export type Map<T> = Tree<Entry<T>>;
6
- export declare const at: (name: string) => <T>(map: Map<T>) => T | null;
7
- export declare const setReduce: <T>(reduce: Reduce<T>) => (name: string) => (value: T) => (map: Map<T>) => Map<T>;
8
- export declare const setReplace: (name: string) => <T>(value: T) => (map: Map<T>) => Map<T>;
9
- export declare const entries: <T>(map: Map<T>) => List<Entry<T>>;
10
- export declare const fromEntries: <T>(entries: List<Entry<T>>) => Map<T>;
11
- export declare const remove: (name: string) => <T>(map: Map<T>) => Map<T>;
12
- export declare const empty: null;
1
+ export declare const mapSet: <K, V>(map: ReadonlyMap<K, V>, k: K, v: V) => ReadonlyMap<K, V>;
2
+ export declare const mapDelete: <K, V>(map: ReadonlyMap<K, V>, k: K) => ReadonlyMap<K, V>;
@@ -1,22 +1,17 @@
1
- import { value, find } from "../btree/find/module.f.js";
2
- import { set } from "../btree/set/module.f.js";
3
- import { remove as btreeRemove } from "../btree/remove/module.f.js";
4
- import { values } from "../btree/module.f.js";
5
- import { cmp } from "../string/module.f.js";
6
- import { fold } from "../list/module.f.js";
7
- const keyCmp = a => ([b]) => cmp(a)(b);
8
- export const at = (name) => (map) => {
9
- if (map === null) {
10
- return null;
1
+ const concat = (x, y) => ({
2
+ *[Symbol.iterator]() {
3
+ yield* x;
4
+ yield* y;
11
5
  }
12
- const result = value(find(keyCmp(name))(map).first);
13
- return result === null ? null : result[1];
14
- };
15
- const setReduceEntry = reduce => entry => set(keyCmp(entry[0]))(old => old === null ? entry : [old[0], reduce(old[1])(entry[1])]);
16
- export const setReduce = reduce => name => value => setReduceEntry(reduce)([name, value]);
17
- const replace = () => b => b;
18
- export const setReplace = name => value => setReduceEntry(replace)([name, value]);
19
- export const entries = values;
20
- export const fromEntries = fold(setReduceEntry(replace))(null);
21
- export const remove = name => btreeRemove(keyCmp(name));
22
- export const empty = null;
6
+ });
7
+ const filter = (i, p) => ({
8
+ *[Symbol.iterator]() {
9
+ for (const x of i) {
10
+ if (p(x)) {
11
+ yield x;
12
+ }
13
+ }
14
+ }
15
+ });
16
+ export const mapSet = (map, k, v) => new Map(concat(map, [[k, v]]));
17
+ export const mapDelete = (map, k) => new Map(filter(map, ([xk]) => xk !== k));
@@ -1,5 +1,5 @@
1
1
  declare const _default: {
2
- main: (() => void)[];
3
- stress: () => void;
2
+ set: () => void;
3
+ delete: () => void;
4
4
  };
5
5
  export default _default;
@@ -1,113 +1,21 @@
1
- import { at, setReplace, setReduce, empty, entries, remove } from "./module.f.js";
2
- import { toArray } from "../list/module.f.js";
1
+ import { mapSet, mapDelete } from "./module.f.js";
3
2
  export default {
4
- main: [
5
- () => {
6
- let m = setReplace('a')(1)(null);
7
- if (at('a')(m) !== 1) {
8
- throw 'error';
9
- }
10
- if (at('b')(m) !== null) {
11
- throw 'error';
12
- }
13
- m = setReplace('b')(2)(m);
14
- if (at('a')(m) !== 1) {
15
- throw 'error';
16
- }
17
- if (at('b')(m) !== 2) {
18
- throw 'error';
19
- }
20
- if (at('c')(m) !== null) {
21
- throw 'error';
22
- }
23
- m = setReplace('z')(3)(m);
24
- if (at('a')(m) !== 1) {
25
- throw 'error';
26
- }
27
- if (at('b')(m) !== 2) {
28
- throw 'error';
29
- }
30
- if (at('z')(m) !== 3) {
31
- throw 'error';
32
- }
33
- if (at('')(m) !== null) {
34
- throw 'error';
35
- }
36
- m = setReplace('')(4)(m);
37
- if (at('a')(m) !== 1) {
38
- throw 'error';
39
- }
40
- if (at('b')(m) !== 2) {
41
- throw 'error';
42
- }
43
- if (at('z')(m) !== 3) {
44
- throw 'error';
45
- }
46
- if (at('')(m) !== 4) {
47
- throw 'error';
48
- }
49
- if (at('Hello world!')(m) !== null) {
50
- throw 'error';
51
- }
52
- m = setReplace('Hello world!')(42)(m);
53
- if (at('a')(m) !== 1) {
54
- throw 'error';
55
- }
56
- if (at('b')(m) !== 2) {
57
- throw 'error';
58
- }
59
- if (at('z')(m) !== 3) {
60
- throw 'error';
61
- }
62
- if (at('')(m) !== 4) {
63
- throw 'error';
64
- }
65
- if (at('Hello world!')(m) !== 42) {
66
- throw 'error';
67
- }
68
- if (at('x')(m) !== null) {
69
- throw 'error';
70
- }
71
- // console.log(Array.from(m.entries()))
72
- m = remove('Hello world!')(m);
73
- if (at('Hello world!')(m) !== null) {
74
- throw m;
75
- }
76
- m = setReduce((a) => (b) => a + b)('a')(43)(m);
77
- if (at('a')(m) !== 44) {
78
- throw 'error';
79
- }
80
- },
81
- () => {
82
- let m = setReplace('x')(12)(null);
83
- m = setReplace('y')(44)(m);
84
- if (at('x')(m) !== 12) {
85
- throw 'error';
86
- }
87
- if (at('y')(m) !== 44) {
88
- throw 'error';
89
- }
90
- if (at('a')(m) !== null) {
91
- throw 'error';
92
- }
93
- const e = toArray(entries(m));
94
- if (e.length !== 2) {
95
- throw 'error';
96
- }
97
- },
98
- ],
99
- stress: () => {
100
- let m = empty;
101
- for (let i = 0; i < 100_000; ++i) {
102
- m = setReplace((i * i).toString())(i)(m);
103
- /*
104
- console.log()
105
- console.log(`# ${i}`)
106
- console.log()
107
- for (const e of m.struct()) {
108
- console.log(e)
109
- }
110
- */
3
+ set: () => {
4
+ const map = mapSet(new Map(), 'a', 'b');
5
+ if (map.get('a') !== 'b') {
6
+ throw 'error';
7
+ }
8
+ if (map.size !== 1) {
9
+ throw 'error';
10
+ }
11
+ },
12
+ delete: () => {
13
+ const map = mapDelete(mapSet(new Map(), 'a', 'b'), 'a');
14
+ if (map.get('a') !== undefined) {
15
+ throw 'error';
16
+ }
17
+ if (map.size !== 0) {
18
+ throw 'error';
111
19
  }
112
20
  }
113
21
  };
@@ -1,5 +1,5 @@
1
1
  import { type List } from '../list/module.f.ts';
2
- import { type Map as BtMap } from '../map/module.f.ts';
2
+ import { type OrderedMap } from '../ordered_map/module.f.ts';
3
3
  export type Map<T> = {
4
4
  readonly [k in string]: T;
5
5
  };
@@ -7,7 +7,7 @@ export type Entry<T> = readonly [string, T];
7
7
  export declare const at: (name: string) => <T>(object: Map<T>) => T | null;
8
8
  export declare const sort: <T>(e: List<Entry<T>>) => List<Entry<T>>;
9
9
  export declare const fromEntries: <T>(e: List<Entry<T>>) => Map<T>;
10
- export declare const fromMap: <T>(m: BtMap<T>) => Map<T>;
10
+ export declare const fromMap: <T>(m: OrderedMap<T>) => Map<T>;
11
11
  /**
12
12
  * A set of objects with a single key.
13
13
  *
@@ -1,5 +1,5 @@
1
1
  import { iterable } from "../list/module.f.js";
2
- import { entries as mapEntries, fromEntries as mapFromEntries } from "../map/module.f.js";
2
+ import { entries as mapEntries, fromEntries as mapFromEntries } from "../ordered_map/module.f.js";
3
3
  const { getOwnPropertyDescriptor, fromEntries: objectFromEntries } = Object;
4
4
  export const at = name => object => {
5
5
  const r = getOwnPropertyDescriptor(object, name);
@@ -0,0 +1,12 @@
1
+ import type { Tree } from '../btree/types/module.f.ts';
2
+ import { type List } from '../list/module.f.ts';
3
+ import type { Reduce } from '../function/operator/module.f.ts';
4
+ export type Entry<T> = readonly [string, T];
5
+ export type OrderedMap<T> = Tree<Entry<T>>;
6
+ export declare const at: (name: string) => <T>(map: OrderedMap<T>) => T | null;
7
+ export declare const setReduce: <T>(reduce: Reduce<T>) => (name: string) => (value: T) => (map: OrderedMap<T>) => OrderedMap<T>;
8
+ export declare const setReplace: (name: string) => <T>(value: T) => (map: OrderedMap<T>) => OrderedMap<T>;
9
+ export declare const entries: <T>(map: OrderedMap<T>) => List<Entry<T>>;
10
+ export declare const fromEntries: <T>(entries: List<Entry<T>>) => OrderedMap<T>;
11
+ export declare const remove: (name: string) => <T>(map: OrderedMap<T>) => OrderedMap<T>;
12
+ export declare const empty: null;
@@ -0,0 +1,22 @@
1
+ import { value, find } from "../btree/find/module.f.js";
2
+ import { set } from "../btree/set/module.f.js";
3
+ import { remove as btreeRemove } from "../btree/remove/module.f.js";
4
+ import { values } from "../btree/module.f.js";
5
+ import { cmp } from "../string/module.f.js";
6
+ import { fold } from "../list/module.f.js";
7
+ const keyCmp = a => ([b]) => cmp(a)(b);
8
+ export const at = (name) => (map) => {
9
+ if (map === null) {
10
+ return null;
11
+ }
12
+ const result = value(find(keyCmp(name))(map).first);
13
+ return result === null ? null : result[1];
14
+ };
15
+ const setReduceEntry = reduce => entry => set(keyCmp(entry[0]))(old => old === null ? entry : [old[0], reduce(old[1])(entry[1])]);
16
+ export const setReduce = reduce => name => value => setReduceEntry(reduce)([name, value]);
17
+ const replace = () => b => b;
18
+ export const setReplace = name => value => setReduceEntry(replace)([name, value]);
19
+ export const entries = values;
20
+ export const fromEntries = fold(setReduceEntry(replace))(null);
21
+ export const remove = name => btreeRemove(keyCmp(name));
22
+ export const empty = null;
@@ -0,0 +1,5 @@
1
+ declare const _default: {
2
+ main: (() => void)[];
3
+ stress: () => void;
4
+ };
5
+ export default _default;
@@ -0,0 +1,113 @@
1
+ import { at, setReplace, setReduce, empty, entries, remove } from "./module.f.js";
2
+ import { toArray } from "../list/module.f.js";
3
+ export default {
4
+ main: [
5
+ () => {
6
+ let m = setReplace('a')(1)(null);
7
+ if (at('a')(m) !== 1) {
8
+ throw 'error';
9
+ }
10
+ if (at('b')(m) !== null) {
11
+ throw 'error';
12
+ }
13
+ m = setReplace('b')(2)(m);
14
+ if (at('a')(m) !== 1) {
15
+ throw 'error';
16
+ }
17
+ if (at('b')(m) !== 2) {
18
+ throw 'error';
19
+ }
20
+ if (at('c')(m) !== null) {
21
+ throw 'error';
22
+ }
23
+ m = setReplace('z')(3)(m);
24
+ if (at('a')(m) !== 1) {
25
+ throw 'error';
26
+ }
27
+ if (at('b')(m) !== 2) {
28
+ throw 'error';
29
+ }
30
+ if (at('z')(m) !== 3) {
31
+ throw 'error';
32
+ }
33
+ if (at('')(m) !== null) {
34
+ throw 'error';
35
+ }
36
+ m = setReplace('')(4)(m);
37
+ if (at('a')(m) !== 1) {
38
+ throw 'error';
39
+ }
40
+ if (at('b')(m) !== 2) {
41
+ throw 'error';
42
+ }
43
+ if (at('z')(m) !== 3) {
44
+ throw 'error';
45
+ }
46
+ if (at('')(m) !== 4) {
47
+ throw 'error';
48
+ }
49
+ if (at('Hello world!')(m) !== null) {
50
+ throw 'error';
51
+ }
52
+ m = setReplace('Hello world!')(42)(m);
53
+ if (at('a')(m) !== 1) {
54
+ throw 'error';
55
+ }
56
+ if (at('b')(m) !== 2) {
57
+ throw 'error';
58
+ }
59
+ if (at('z')(m) !== 3) {
60
+ throw 'error';
61
+ }
62
+ if (at('')(m) !== 4) {
63
+ throw 'error';
64
+ }
65
+ if (at('Hello world!')(m) !== 42) {
66
+ throw 'error';
67
+ }
68
+ if (at('x')(m) !== null) {
69
+ throw 'error';
70
+ }
71
+ // console.log(Array.from(m.entries()))
72
+ m = remove('Hello world!')(m);
73
+ if (at('Hello world!')(m) !== null) {
74
+ throw m;
75
+ }
76
+ m = setReduce((a) => (b) => a + b)('a')(43)(m);
77
+ if (at('a')(m) !== 44) {
78
+ throw 'error';
79
+ }
80
+ },
81
+ () => {
82
+ let m = setReplace('x')(12)(null);
83
+ m = setReplace('y')(44)(m);
84
+ if (at('x')(m) !== 12) {
85
+ throw 'error';
86
+ }
87
+ if (at('y')(m) !== 44) {
88
+ throw 'error';
89
+ }
90
+ if (at('a')(m) !== null) {
91
+ throw 'error';
92
+ }
93
+ const e = toArray(entries(m));
94
+ if (e.length !== 2) {
95
+ throw 'error';
96
+ }
97
+ },
98
+ ],
99
+ stress: () => {
100
+ let m = empty;
101
+ for (let i = 0; i < 100_000; ++i) {
102
+ m = setReplace((i * i).toString())(i)(m);
103
+ /*
104
+ console.log()
105
+ console.log(`# ${i}`)
106
+ console.log()
107
+ for (const e of m.struct()) {
108
+ console.log(e)
109
+ }
110
+ */
111
+ }
112
+ }
113
+ };
@@ -0,0 +1,5 @@
1
+ import { type Result } from './module.f.ts';
2
+ declare const _default: {
3
+ readonly tryCatch: <T>(f: () => T) => Result<T, unknown>;
4
+ };
5
+ export default _default;
@@ -0,0 +1,13 @@
1
+ import { ok, error } from "./module.f.js";
2
+ const tryCatch = f => {
3
+ // Side effect: `try catch` is not allowed in FunctionalScript.
4
+ try {
5
+ return ok(f());
6
+ }
7
+ catch (e) {
8
+ return error(e);
9
+ }
10
+ };
11
+ export default {
12
+ tryCatch,
13
+ };
package/fsc.js DELETED
@@ -1,4 +0,0 @@
1
- #!/usr/bin/env node
2
- import node from "./io/node.js";
3
- import { compile } from "./djs/module.f.js";
4
- await node(compile);