Skip to content

Commit

Permalink
tiny syntax change for js perf; tag 0.8.47
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Apr 5, 2024
1 parent 7513603 commit 55bc987
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit"
version = "0.8.46"
version = "0.8.47"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@calcit/procs",
"version": "0.8.46",
"version": "0.8.47",
"main": "./lib/calcit.procs.mjs",
"devDependencies": {
"@types/node": "^20.11.28",
Expand All @@ -23,6 +23,6 @@
"dependencies": {
"@calcit/ternary-tree": "0.0.23",
"@cirru/parser.ts": "^0.0.6",
"@cirru/writer.ts": "^0.1.4"
"@cirru/writer.ts": "^0.1.5"
}
}
2 changes: 1 addition & 1 deletion src/cirru/calcit-core.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@
&let
size $ &list:count pattern
quasiquote $ if
if (&= ~t ~k) (&= ~size (&tuple:count ~value)) false
if (identical? ~t ~k) (identical? ~size (&tuple:count ~value)) false
let
~ $ map-indexed (&list:rest pattern)
defn %tag-match (idx x)
Expand Down
3 changes: 2 additions & 1 deletion ts-src/custom-formatter.mts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ export let load_console_formatter_$x_ = () => {
if (obj instanceof CalcitList || obj instanceof CalcitSliceList) {
let preview = "";
let hasCollection = false;
for (let idx = 0; idx < obj.len(); idx++) {
let size = obj.len();
for (let idx = 0; idx < size; idx++) {
preview += " ";
if (isLiteral(obj.get(idx))) {
preview += saveString(obj.get(idx));
Expand Down
40 changes: 24 additions & 16 deletions ts-src/js-cirru.mts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ export let to_cirru_edn = (x: CalcitValue): CirruEdnFormat => {
return `|${x}`;
}
if (typeof x === "number") {
return x.toString();
return `${x}`;
}
if (typeof x === "boolean") {
return x.toString();
return `${x}`;
}
if (x instanceof CalcitTag) {
return x.toString();
Expand All @@ -85,8 +85,12 @@ export let to_cirru_edn = (x: CalcitValue): CirruEdnFormat => {
return x.toString();
}
if (x instanceof CalcitList || x instanceof CalcitSliceList) {
// TODO can be faster
return (["[]"] as CirruEdnFormat[]).concat(x.toArray().map(to_cirru_edn));
let ret: CirruEdnFormat[] = ["[]"];
let arr = x.toArray();
for (let idx = 0; idx < arr.length; idx++) {
ret.push(to_cirru_edn(arr[idx]));
}
return ret;
}
if (x instanceof CalcitCirruQuote) {
return ["quote", x.value];
Expand Down Expand Up @@ -126,22 +130,14 @@ export let to_cirru_edn = (x: CalcitValue): CirruEdnFormat => {
return buffer;
}
if (x instanceof CalcitRecord) {
let buffer: [string, CirruEdnFormat] = ["%{}", x.name.toString()];
let buffer: [string, CirruEdnFormat][] = [];
for (let idx = 0; idx < x.fields.length; idx++) {
buffer.push([x.fields[idx].toString(), to_cirru_edn(x.values[idx])]);
}
// placed literals first
buffer.sort((a, b) => {
let a1_literal = isLiteral(a[1] as CalcitValue);
let b1_literal = isLiteral(b[1] as CalcitValue);
if (a1_literal && !b1_literal) {
return -1;
} else if (!a1_literal && b1_literal) {
return 1;
} else {
return _$n_compare(a[0] as CalcitValue, b[0] as CalcitValue);
}
});
buffer.sort(recordFieldOrder);
(buffer as any[]).unshift(x.name.toString());
(buffer as any[]).unshift("%{}");
return buffer;
}
if (x instanceof CalcitSet) {
Expand Down Expand Up @@ -172,6 +168,18 @@ export let to_cirru_edn = (x: CalcitValue): CirruEdnFormat => {
throw new Error("Unexpected data to to-cirru-edn");
};

let recordFieldOrder = (a: [string, CirruEdnFormat], b: [string, CirruEdnFormat]) => {
let a1_literal = isLiteral(a[1] as CalcitValue);
let b1_literal = isLiteral(b[1] as CalcitValue);
if (a1_literal && !b1_literal) {
return -1;
} else if (!a1_literal && b1_literal) {
return 1;
} else {
return _$n_compare(a[0] as CalcitValue, b[0] as CalcitValue);
}
};

/** makes sure we got string */
let extractFieldTag = (x: string) => {
if (x[0] === ":") {
Expand Down
12 changes: 8 additions & 4 deletions ts-src/js-list.mts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export class CalcitList {
return new CalcitList(ternaryTree.reverse(this.value));
}
nestedDataInChildren(): boolean {
for (let idx = 0; idx < this.len(); idx++) {
let size = this.len();
for (let idx = 0; idx < size; idx++) {
if (!isLiteral(this.get(idx))) {
return true;
}
Expand Down Expand Up @@ -262,7 +263,8 @@ export class CalcitSliceList {
return this.turnListMode().reverse();
}
nestedDataInChildren(): boolean {
for (let idx = 0; idx < this.len(); idx++) {
let size = this.len();
for (let idx = 0; idx < size; idx++) {
if (!isLiteral(this.get(idx))) {
return true;
}
Expand Down Expand Up @@ -292,7 +294,8 @@ export let foldl = function (xs: CalcitValue, acc: CalcitValue, f: CalcitFn): Ca
}
if (xs instanceof CalcitSliceList || xs instanceof CalcitList) {
var result = acc;
for (let idx = 0; idx < xs.len(); idx++) {
let size = xs.len();
for (let idx = 0; idx < size; idx++) {
let item = xs.get(idx);
result = f(result, item);
}
Expand Down Expand Up @@ -335,7 +338,8 @@ export let foldl_shortcut = function (xs: CalcitValue, acc: CalcitValue, v0: Cal
}
if (xs instanceof CalcitList || xs instanceof CalcitSliceList) {
var state = acc;
for (let idx = 0; idx < xs.len(); idx++) {
let size = xs.len();
for (let idx = 0; idx < size; idx++) {
let item = xs.get(idx);
let pair = f(state, item);
if (pair instanceof CalcitTuple) {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 55bc987

Please sign in to comment.