@mojir/lits
Version:
Lits is a pure functional programming language implemented in TypeScript
1 lines • 582 kB
JavaScript
import e from"node:fs";import t from"node:path";function r(e){if(!e.position||!e.code)return"";const t=e.position.column-1,r=e.code.length-t-1;return`${" ".repeat(Math.max(t,0))}^${" ".repeat(Math.max(r,0))}`}class n extends Error{params;constructor(e){super(`recur, params: ${e}`),Object.setPrototypeOf(this,n.prototype),this.name="RecurSignal",this.params=e}}class o extends Error{sourceCodeInfo;shortMessage;constructor(e,t){const n=e instanceof Error?e.message:`${e}`;super(function(e,t){if(!t)return e;const n=`${t.position.line}:${t.position.column}`;return`${e}${t.filePath?`\n${t.filePath}:${n}`:`\nLocation ${n}`}\n${t.code}\n${r(t)}`}(n,t)),this.shortMessage=n,this.sourceCodeInfo=t,Object.setPrototypeOf(this,o.prototype),this.name="LitsError"}getCodeMarker(){return this.sourceCodeInfo&&r(this.sourceCodeInfo)}}class a extends o{userMessage;constructor(e,t){super(e,t),this.userMessage=e,Object.setPrototypeOf(this,a.prototype),this.name="UserDefinedError"}}class i extends o{constructor(e,t){super(e,t),Object.setPrototypeOf(this,i.prototype),this.name="AssertionError"}}class s extends o{symbol;constructor(e,t){super(`Undefined symbol '${e}'.`,t),this.symbol=e,Object.setPrototypeOf(this,s.prototype),this.name="UndefinedSymbolError"}}const c={"??":0,"&&":1,"||":2,array:3,cond:4,"defined?":5,block:6,doseq:7,"0_lambda":8,for:9,if:10,let:11,loop:12,object:13,recur:14,match:15,throw:16,try:17,unless:18,import:19},l={Number:1,String:2,NormalExpression:3,SpecialExpression:4,UserDefinedSymbol:5,NormalBuiltinSymbol:6,SpecialBuiltinSymbol:7,ReservedSymbol:8,Binding:9,Spread:10},m=new Set(Object.values(l));function u(e){return Object.keys(l).find(t=>l[t]===e)}const p=new Set(["UserDefined","Partial","Comp","Constantly","Juxt","Complement","EveryPred","SomePred","Fnull","Builtin","SpecialBuiltin","NativeJsFunction","Module"]);const g="^^fn^^",h="^^re^^";function d(e){return null!==e&&"object"==typeof e&&(g in e&&"functionType"in e&&function(e){return"string"==typeof e&&p.has(e)}(e.functionType))}function y(e){return!(!Array.isArray(e)||e.length<2)&&function(e){return"number"==typeof e&&m.has(e)}(e[0])}function f(e){return d(e)?`<function ${e.name||"λ"}>`:y(e)?`${u(e[0])}-node`:null===e?"null":"object"==typeof e&&e instanceof RegExp?`${e}`:"object"==typeof e&&e instanceof Error?e.toString():JSON.stringify(e)}function v(e,t){return e?.sourceCodeInfo??t}function b(e,t,r){return new o(`Expected ${e}, got ${f(t)}.`,v(t,r))}function x(e){const t=e[0];return l.UserDefinedSymbol===t||l.NormalBuiltinSymbol===t||l.SpecialBuiltinSymbol===t}function w(e){return l.UserDefinedSymbol===e[0]}function k(e,t){return function(e,t){if(!w(e))throw b("UserDefinedSymbolNode",e,t)}(e,t),e}function q(e){return l.NormalBuiltinSymbol===e[0]}function A(e){return l.SpecialBuiltinSymbol===e[0]}function N(e){return!!function(e){return e[0]===l.NormalExpression}(e)&&x(e[1][0])}function $(e){return e[0]===l.Spread}const T=(e,t,r,n)=>{const o=Array.isArray(e)?e:[[l.SpecialExpression,[c.block,e.body]]],a=new Set;for(const e of o)S(e,t,r,n)?.forEach(e=>a.add(e));return a};function S(e,t,r,n){const a=e[0];switch(a){case l.UserDefinedSymbol:{const r=e;return null===t.lookUp(r)?new Set([r[1]]):null}case l.NormalBuiltinSymbol:case l.SpecialBuiltinSymbol:case l.String:case l.Number:case l.ReservedSymbol:case l.Binding:return null;case l.NormalExpression:{const o=e,a=new Set;if(N(o)){const[,[e]]=o;if(w(e)){null===t.lookUp(e)&&a.add(e[1])}}else{const[,[e]]=o;S(e,t,r,n)?.forEach(e=>a.add(e))}for(const e of o[1][1])S(e,t,r,n)?.forEach(e=>a.add(e));return a}case l.SpecialExpression:{const o=e,a=o[1][0];return(0,r.specialExpressions[a].getUndefinedSymbols)(o,t,{getUndefinedSymbols:T,builtin:r,evaluateNode:n})}case l.Spread:return S(e[1],t,r,n);default:throw new o(`Unhandled node type: ${a}`,e[2])}}function z(e,t){return E(e,t),e}function E(e,t){if(!function(e){return void 0!==e}(e))throw new o("Unexpected undefined",v(e,t))}function M(e){return null!==e&&"object"==typeof e&&!Array.isArray(e)}function j(e){return null!==e&&"object"==typeof e&&!!e[g]}function P(e){return void 0!==e}function C(e,t){return R(e,t),e}function R(e,t){if(!P(e))throw b("not undefined",e,t)}function I(e){return Array.isArray(e)||"string"==typeof e}function O(e,t){return F(e,t),e}function F(e,t){if(!I(e))throw b("string or array",e,t)}function U(e){return!(null===e||"object"!=typeof e||Array.isArray(e)||e instanceof RegExp||j(e)||B(e))}function D(e,t){if(!U(e))throw b("object",e,t)}function W(e){return I(e)||U(e)}function G(e,t){return V(e,t),e}function V(e,t){if(!W(e))throw b("string, array or object",e,t)}function B(e){return null!==e&&"object"==typeof e&&!!e[h]}function L(e,t){if(!function(e){return B(e)||"string"==typeof e}(e))throw b("string or RegularExpression",e,t)}function K(e,t){return _(e,t),e}function _(e,t){if(!function(e){return"number"==typeof e||!!W(e)||!!j(e)}(e))throw b("FunctionLike",e,t)}function J(e,t={}){return"string"==typeof e&&((!t.nonEmpty||0!==e.length)&&(!t.char||1===e.length))}function Y(e,t,r={}){if(!J(e,r))throw b(""+(r.nonEmpty?"non empty string":r.char?"character":"string"),e,t)}function H(e,t,r={}){return Y(e,t,r),e}function X(e){return"string"==typeof e||"number"==typeof e}function Z(e,t){return Q(e,t),e}function Q(e,t){if(!X(e))throw b("string or number",e,t)}const ee={assert:{evaluate:(e,t)=>{const r=e[0],n=2===e.length?e[1]:`${r}`;if(Y(n,t),!r)throw new i(n,t);return C(r,t)},arity:{min:1,max:2},docs:{category:"assertion",description:"If $value is falsy it throws `AssertionError` with $message. If no $message is provided, message is set to $value.",returns:{type:"any"},args:{value:{type:"any"},message:{type:"string"}},variants:[{argumentNames:["value"]},{argumentNames:["value","message"]}],examples:['try assert(0, "Expected a positive value") catch (e) e.message end'],seeAlso:["assertion.assert-truthy","assertion.assert-true"],hideOperatorForm:!0}}};function te(e,t={}){return"number"==typeof e&&(!Number.isNaN(e)&&(!(t.integer&&!Number.isInteger(e))&&(!(t.finite&&!Number.isFinite(e))&&((!t.zero||0===e)&&((!t.nonZero||0!==e)&&(!(t.positive&&e<=0)&&(!(t.negative&&e>=0)&&(!(t.nonPositive&&e>0)&&(!(t.nonNegative&&e<0)&&(!("number"==typeof t.gt&&e<=t.gt)&&(!("number"==typeof t.gte&&e<t.gte)&&(!("number"==typeof t.lt&&e>=t.lt)&&!("number"==typeof t.lte&&e>t.lte)))))))))))))}function re(e,t,r={}){if(!te(e,r))throw new o(`Expected ${function(e){if(e.zero)return"zero";const t=function(e){return e.positive?"positive":e.negative?"negative":e.nonNegative?"non negative":e.nonPositive?"non positive":e.nonZero?"non zero":""}(e),r=e.integer?"integer":"number",n=e.finite?"finite":"",o=function(e){return"number"!=typeof e.gt&&"number"!=typeof e.gte||"number"!=typeof e.lt&&"number"!=typeof e.lte?"number"==typeof e.gt||"number"==typeof e.gte?"number"==typeof e.gt?`n > ${e.gt}`:`n >= ${e.gte}`:"number"==typeof e.lt||"number"==typeof e.lte?"number"==typeof e.lt?`n < ${e.lt}`:`n <= ${e.lte}`:"":`${"number"==typeof e.gt?`${e.gt} < n `:`${e.gte} <= n `}${"number"==typeof e.lt?`< ${e.lt}`:`<= ${e.lte}`}`}(e);return[t,n,r,o].filter(e=>!!e).join(" ")}(r)}, got ${f(e)}.`,v(e,t))}function ne(e,t,r={}){return re(e,t,r),e}function oe(e){return"number"==typeof e||W(e)?ie(1):e.arity}function ae(e,t,r){const{min:n,max:a}=e;if("number"==typeof n&&t<n)throw new o(`Wrong number of arguments, expected at least ${n}, got ${f(t)}.`,r);if("number"==typeof a&&t>a)throw new o(`Wrong number of arguments, expected at most ${a}, got ${f(t)}.`,r)}function ie(e){return{min:e,max:e}}function se(e,t){return{a:{type:e},b:{type:t}}}const ce={"<<":{evaluate:([e,t],r)=>(re(e,r,{integer:!0}),re(t,r,{integer:!0,nonNegative:!0}),e<<t),arity:ie(2),docs:{category:"bitwise",returns:{type:"integer"},args:{...se("integer","integer")},variants:[{argumentNames:["a","b"]}],description:"Shifts $a arithmetically left by $b bit positions.",seeAlso:[">>",">>>"],examples:["1 << 10","<<(1, 10)","<<(-4, 2)"]}},">>":{evaluate:([e,t],r)=>(re(e,r,{integer:!0}),re(t,r,{integer:!0,nonNegative:!0}),e>>t),arity:ie(2),docs:{category:"bitwise",returns:{type:"integer"},args:{...se("integer","integer")},variants:[{argumentNames:["a","b"]}],description:"Shifts $a arithmetically right by $b bit positions.",seeAlso:["<<",">>>"],examples:["2048 >> 10",">>(2048, 10)",">>>(-16, 2)",">>(4, 10)"]}},">>>":{evaluate:([e,t],r)=>(re(e,r,{integer:!0}),re(t,r,{integer:!0,nonNegative:!0}),e>>>t),arity:ie(2),docs:{category:"bitwise",returns:{type:"integer"},args:{...se("integer","integer")},variants:[{argumentNames:["a","b"]}],description:"Shifts $a arithmetically right by $b bit positions without sign extension.",seeAlso:["<<",">>"],examples:["-16 >>> 2",">>>(2048, 10)",">>>(-16, 2)",">>>(4, 10)",">>>(-1, 10)"]}},"&":{evaluate:([e,...t],r)=>(re(e,r,{integer:!0}),t.reduce((e,t)=>(re(t,r,{integer:!0}),e&t),e)),arity:{min:2},docs:{category:"bitwise",returns:{type:"integer"},args:{...se("integer","integer"),c:{type:"integer",rest:!0}},variants:[{argumentNames:["a","b"]},{argumentNames:["a","b","c"]}],description:"Returns bitwise `and` of all arguments.",seeAlso:["|","xor","bitwise.bit-not","bitwise.bit-and-not"],examples:["0b0011 & 0b0110","&(0b0011, 0b0110)","&(0b0011, 0b0110, 0b1001)"]}},"|":{evaluate:([e,...t],r)=>(re(e,r,{integer:!0}),t.reduce((e,t)=>(re(t,r,{integer:!0}),e|t),e)),arity:{min:2},docs:{category:"bitwise",returns:{type:"integer"},args:{...se("integer","integer"),c:{type:"integer",rest:!0}},variants:[{argumentNames:["a","b"]},{argumentNames:["a","b","c"]}],description:"Returns bitwise `or` of all arguments.",seeAlso:["&","xor","bitwise.bit-not","bitwise.bit-and-not"],examples:["0b0011 | 0b0110","|(0b0011, 0b0110)","|(0b1000, 0b0100, 0b0010)"]}},xor:{evaluate:([e,...t],r)=>(re(e,r,{integer:!0}),t.reduce((e,t)=>(re(t,r,{integer:!0}),e^t),e)),arity:{min:2},docs:{category:"bitwise",returns:{type:"integer"},args:{...se("integer","integer"),c:{type:"integer",rest:!0}},variants:[{argumentNames:["a","b"]},{argumentNames:["a","b","c"]}],description:"Returns bitwise `xor` of all arguments.",seeAlso:["&","|","bitwise.bit-not","bitwise.bit-and-not"],examples:["0b0011 xor 0b0110","xor(0b0011, 0b0110)","xor(0b11110000, 0b00111100, 0b10101010)"]}}};function le(e,t){return!!W(e)&&("string"==typeof e||Array.isArray(e)?!!te(t,{integer:!0})&&(t>=0&&t<e.length):!!Object.getOwnPropertyDescriptor(e,t))}function me(e,t,r){if(Q(e,r),Q(t,r),"string"==typeof e&&"string"==typeof t)return e<t?-1:e>t?1:0;if("number"==typeof e&&"number"==typeof t)return Math.sign(e-t);throw new o(`Cannot compare values of different types: ${typeof e} and ${typeof t}`,r)}function ue(e,t,r){if(e===t)return!0;if("number"==typeof e&&"number"==typeof t)return be(e,t);if(Array.isArray(e)&&Array.isArray(t)){if(e.length!==t.length)return!1;for(let n=0;n<e.length;n+=1)if(!ue(C(e[n],r),C(t[n],r),r))return!1;return!0}if(B(e)&&B(t))return e.s===t.s&&e.f===t.f;if(M(e)&&M(t)){const n=Object.keys(e),o=Object.keys(t);if(n.length!==o.length)return!1;for(let o=0;o<n.length;o+=1){const a=H(n[o],r);if(!ue(e[a],t[a],r))return!1}return!0}return!1}function pe(e){return Math.max(0,Math.ceil(e))}function ge(e){return e??null}function he(e){return U(e)?Object.entries(e).reduce((e,t)=>{const[r,n]=t;return e[r]=he(n),e},{}):Array.isArray(e)?e.map(e=>he(e)):e}function de(e){return he(e)}function ye(...e){const t=new Set;for(const r of e)r.forEach(e=>t.add(e));return t}function fe(e,t){t.forEach(t=>e.add(t))}const ve=1e-10;function be(e,t,r=ve){if(e===t)return!0;const n=Math.abs(e-t);if(0===e||0===t||n<r)return n<r;return n/(Math.abs(e)+Math.abs(t))<r}function xe(e){return Math.abs(e)<ve}function we(e,t=0){const r=e.split("\n");for(;r[0]?.match(/^\s*$/);)r.shift();for(;r[r.length-1]?.match(/^\s*$/);)r.pop();const n=r.reduce((e,t)=>{if(t.match(/^\s*$/))return e;const r=t.match(/^\s*/)[0].length;return Math.min(e,r)},1/0);return r.map(e=>" ".repeat(t)+e.slice(n)).join("\n").trimEnd()}function ke(e,t){if(!Array.isArray(e))throw b("array",e,t)}function qe(e,t){if(!function(e){return Array.isArray(e)&&e.every(e=>"string"==typeof e)}(e))throw b("array of strings",e,t)}function Ae(e,t){if(!function(e){return Array.isArray(e)&&e.every(e=>"string"==typeof e&&1===e.length)}(e))throw b("array of strings",e,t)}function Ne(e,t){return e instanceof Promise?e.then(t):t(e)}function $e(e,t){const r=[];for(let n=0;n<e.length;n++){const o=t(e[n],n);if(o instanceof Promise)return Te(o,r,e,t,n);r.push(o)}return r}async function Te(e,t,r,n,o){t.push(await e);for(let e=o+1;e<r.length;e++)t.push(await n(r[e],e));return t}function Se(e,t,r){let n=r;for(let r=0;r<e.length;r++){const o=t(n,e[r],r);if(o instanceof Promise)return ze(o,e,t,r);n=o}return n}async function ze(e,t,r,n){let o=await e;for(let e=n+1;e<t.length;e++)o=await r(o,t[e],e);return o}function Ee(e,t){for(let r=0;r<e.length;r++){const n=t(e[r],r);if(n instanceof Promise)return Me(n,e,t,r)}}async function Me(e,t,r,n){await e;for(let e=n+1;e<t.length;e++)await r(t[e],e)}function je(e,t){try{const r=e();return r instanceof Promise?r.catch(t):r}catch(e){return t(e)}}function Pe(e,t){for(let r=0;r<e.length;r++){const n=t(e[r],r);if(n instanceof Promise)return Ce(n,e,t,r);if(n)return!0}return!1}async function Ce(e,t,r,n){if(await e)return!0;for(let e=n+1;e<t.length;e++)if(await r(t[e],e))return!0;return!1}function Re(e,t){for(let r=0;r<e.length;r++){const n=t(e[r],r);if(n instanceof Promise)return Ie(n,e,t,r);if(!n)return!1}return!0}async function Ie(e,t,r,n){if(!await e)return!1;for(let e=n+1;e<t.length;e++)if(!await r(t[e],e))return!1;return!0}function Oe(e,t){const r=[];for(let n=0;n<e.length;n++){const o=t(e[n],n);if(o instanceof Promise)return Fe(o,r,e,t,n);o&&r.push(e[n])}return r}async function Fe(e,t,r,n,o){await e&&t.push(r[o]);for(let e=o+1;e<r.length;e++)await n(r[e],e)&&t.push(r[e]);return t}function Ue(e,t){for(let r=0;r<e.length;r++){const n=t(e[r],r);if(n instanceof Promise)return De(n,e,t,r);if(n)return r}return-1}async function De(e,t,r,n){if(await e)return n;for(let e=n+1;e<t.length;e++)if(await r(t[e],e))return e;return-1}const We={filter:{evaluate:([e,t],r,n,{executeFunction:o})=>{if(V(e,r),_(t,r),Array.isArray(e))return Se(e,(e,a)=>Ne(o(t,[a],n,r),t=>(t&&e.push(a),e)),[]);if(J(e)){return Ne(Se(e.split(""),(e,a)=>Ne(o(t,[a],n,r),t=>(t&&e.push(a),e)),[]),e=>e.join(""))}return Se(Object.entries(e),(e,[a,i])=>Ne(o(t,[i],n,r),t=>(t&&(e[a]=i),e)),{})},arity:ie(2),docs:{category:"collection",returns:{type:"collection"},args:{a:{type:"collection"},b:{type:"function"},coll:{type:"collection"},fun:{type:"function"}},variants:[{argumentNames:["coll","fun"]}],description:"Creates a new collection with all elements that pass the test implemented by $fun.",seeAlso:["collection.filteri","map","sequence.remove"],examples:['\nfilter(\n ["Albert", "Mojir", 160, [1, 2]],\n string?\n)',"\nfilter(\n [5, 10, 15, 20],\n -> $ > 10\n)","\nfilter(\n { a: 1, b: 2 },\n odd?\n)"]}},map:{evaluate:(e,t,r,{executeFunction:n})=>{const a=K(e.at(-1),t);if(U(e[0]))return function({colls:e,contextStack:t,executeFunction:r,fn:n,sourceCodeInfo:a}){D(e[0],a);const i=Object.keys(e[0]),s={};return e.forEach(e=>{D(e,a);const t=Object.keys(e);if(t.length!==i.length)throw new o(`All objects must have the same keys. Expected: ${i.join(", ")}. Found: ${t.join(", ")}`,a);if(!t.every(e=>i.includes(e)))throw new o(`All objects must have the same keys. Expected: ${i.join(", ")}. Found: ${t.join(", ")}`,a);Object.entries(e).forEach(([e,t])=>{s[e]||(s[e]=[]),s[e].push(t)})}),Se(i,(e,o)=>Ne(r(n,s[o],t,a),t=>(e[o]=t,e)),{})}({colls:e.slice(0,-1),fn:a,sourceCodeInfo:t,contextStack:r,executeFunction:n});const i=e.slice(0,-1);F(i[0],t);const s="string"==typeof i[0];let c=i[0].length;i.slice(1).forEach(e=>{s?Y(e,t):ke(e,t),c=Math.min(c,e.length)});const l=[];for(let e=0;e<c;e++)l.push(i.map(t=>t[e]));const m=$e(l,e=>n(a,e,r,t));return s?Ne(m,e=>(e.forEach(e=>Y(e,t)),e.join(""))):m},arity:{min:2},docs:{category:"collection",returns:{type:"collection"},args:{a:{type:"collection"},b:{type:"function"},colls:{type:"collection",rest:!0,description:"At least one."},fun:{type:"function"}},variants:[{argumentNames:["colls","fun"]}],description:"Creates a new collection populated with the results of calling $fun on every element in $colls.",seeAlso:["collection.mapi","filter","reduce","mapcat","grid.cell-map","grid.cell-mapi"],examples:["[1, 2, 3] map -","[1, 2, 3] map -> -($)",'map(["Albert", "Mojir", 42], str)',"map([1, 2, 3], inc)","map([1, 2, 3], [1, 10, 100], *)","map({ a: 1, b: 2 }, inc)","map({ a: 1, b: 2 }, { a: 10, b: 20 }, +)"]}},reduce:{evaluate:([e,t,r],n,o,{executeFunction:a})=>(V(e,n),_(t,n),R(r,n),"string"==typeof e?(Y(r,n),0===e.length?r:Se(e.split(""),(e,r)=>a(t,[e,r],o,n),r)):Array.isArray(e)?0===e.length?r:Se(e,(e,r)=>a(t,[e,r],o,n),r):0===Object.keys(e).length?r:Se(Object.entries(e),(e,[,r])=>a(t,[e,r],o,n),r)),arity:ie(3),docs:{category:"collection",returns:{type:"any"},args:{fun:{type:"function"},coll:{type:"collection"},initial:{type:"any"}},variants:[{argumentNames:["coll","fun","initial"]}],description:"Runs $fun function on each element of the $coll, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the $coll is a single value.",seeAlso:["collection.reduce-right","collection.reducei","collection.reductions","map","grid.cell-reduce","grid.cell-reducei"],examples:["reduce([1, 2, 3], +, 0)","reduce([], +, 0)","reduce({ a: 1, b: 2 }, +, 0)","\nreduce(\n [1, 2, 3, 4, 5, 6, 7, 8, 9],\n (result, value) -> result + (even?(value) ? value : 0),\n 0)"]}},get:{evaluate:(e,t)=>{const[r,n]=e,o=ge(e[2]);if(Q(n,t),null===r)return o;V(r,t);const a=function(e,t){if(U(e)){if("string"==typeof t&&le(e,t))return ge(e[t])}else if(te(t,{nonNegative:!0,integer:!0})&&t>=0&&t<e.length)return ge(e[t])}(r,n);return void 0===a?o:a},arity:{min:2,max:3},docs:{category:"collection",returns:{type:"any"},args:{a:{type:"collection"},b:{type:["string","integer"]},"not-found":{type:"any",description:"Default value to return if $b is not found."}},variants:[{argumentNames:["a","b"]},{argumentNames:["a","b","not-found"]}],description:"Returns value in $a mapped at $b.",seeAlso:["collection.get-in","contains?","find","nth"],examples:["[1, 2, 3] get 1",'{ a: 1 } get "a"','"Albert" get "3"',"\nget(\n [1, 2, 3],\n 1, // Optional comma after last argument\n)","\nget(\n [],\n 1\n)",'\nget(\n [],\n 1,\n "default"\n)','\nget(\n { a: 1 },\n "a"\n)','\nget(\n { a: 1 },\n "b"\n)','\nget(\n { a: 1 },\n "b",\n "default"\n)','\nget(\n null,\n "a"\n)','\nget(\n null,\n "b",\n "default"\n)']}},count:{evaluate:([e],t)=>null===e?0:"string"==typeof e?e.length:(V(e,t),Array.isArray(e)?e.length:Object.keys(e).length),arity:ie(1),docs:{category:"collection",returns:{type:"number"},args:{coll:{type:["collection","null"]}},variants:[{argumentNames:["coll"]}],description:"Returns number of elements in $coll.",seeAlso:["empty?"],examples:["count([1, 2, 3])","count([])","count({ a: 1 })",'count("")','count("Albert")',"count(null)"]}},"contains?":{evaluate:([e,t],r)=>null!==e&&(V(e,r),J(e)?(Y(t,r),e.includes(t)):I(e)?(R(t,r),!!e.find(e=>ue(C(e),t,r))):(Y(t,r),t in e)),arity:ie(2),docs:{category:"collection",returns:{type:"boolean"},args:{a:{type:["collection","null"]},b:{type:["string","integer"]}},variants:[{argumentNames:["a","b"]}],description:"Returns `true` if $a contains $b, otherwise returns `false`. For strings, it checks if substring is included.",seeAlso:["get","find","index-of"],examples:["[1, 2, 3] contains? 1","null contains? 1",'{ a: 1, b: 2 } contains? "a"',"\ncontains?(\n [],\n 1\n)","\ncontains?(\n [1],\n 1\n)","\ncontains?(\n [1, 2, 3],\n 1\n)",'\ncontains?(\n {},\n "a"\n)','\ncontains?(\n { a: 1, b: 2 },\n "a"\n)']}},assoc:{evaluate:([e,t,r],n)=>(V(e,n),Q(t,n),R(r,n),function(e,t,r,n){if(V(e,n),Q(t,n),Array.isArray(e)||"string"==typeof e){if(re(t,n,{integer:!0}),re(t,n,{gte:0}),re(t,n,{lte:e.length}),"string"==typeof e)return Y(r,n,{char:!0}),`${e.slice(0,t)}${r}${e.slice(t+1)}`;const o=[...e];return o[t]=r,o}Y(t,n);const o={...e};return o[t]=r,o}(e,t,r,n)),arity:ie(3),docs:{category:"collection",returns:{type:"collection"},args:{coll:{type:"collection"},key:{type:["string","number"]},value:{type:"any"},kvs:{type:"any",description:"Key-value pairs to associate.",rest:!0}},variants:[{argumentNames:["coll","key","value"]},{argumentNames:["coll","key","value","kvs"]}],description:"\nAdd or replace the value of element $key to $value in $coll. Repeated for all key-value pairs in $kvs.\nIf $coll is an 'array', $key must be `number` satisfying `0 <=` $key `<= length`.",seeAlso:["collection.assoc-in","dissoc","merge","collection.update"],examples:['\nassoc(\n [1, 2, 3],\n 1,\n "Two"\n)','\nassoc(\n [1, 2, 3],\n 3,\n "Four"\n)','\nassoc(\n { a: 1, b: 2 },\n "a",\n "One")','\nassoc(\n { a: 1, b: 2 },\n "c",\n "Three")','\nassoc(\n "Albert",\n 6,\n "a")']}},"++":{evaluate:(e,t)=>(te(e[0])||V(e[0],t),Array.isArray(e[0])?e.reduce((e,r)=>(ke(r,t),e.concat(r)),[]):X(e[0])?e.reduce((e,r)=>(Q(r,t),`${e}${r}`),""):e.reduce((e,r)=>(D(r,t),Object.assign(e,r)),{})),arity:{min:1},docs:{category:"collection",returns:{type:"collection"},args:{a:{type:"collection"},b:{type:"collection"},colls:{type:"collection",rest:!0}},variants:[{argumentNames:["a"]},{argumentNames:["a","colls"]}],description:"Concatenates collections into one collection.",seeAlso:["mapcat","str","join","push","sequence.unshift"],examples:['"Albert" ++ " " ++ "Mojir"','"Albert" ++ "Mojir"','++("Albert", "-", "Mojir")','++("Albert")','++("A", "l", "b", "e", "r", "t")',"++([1, 2], [3, 4])","++([], [3, 4])","++([1, 2], [])","++([1, 2], [3, 4], [5, 6])","++([])","++({ a: 1, b: 2 }, { b: 1, c: 2 })","++({}, { a: 1 })"]}}},Ge={range:{evaluate:(e,t)=>{const[r,n,o]=e;let a,i,s;re(r,t,{finite:!0}),1===e.length?(a=0,i=r,s=i>=0?1:-1):2===e.length?(re(n,t,{finite:!0}),a=r,i=n,s=i>=a?1:-1):(re(n,t,{finite:!0}),re(o,t,{finite:!0}),a=r,i=n,s=o,re(s,t,i>a?{positive:!0}:i<a?{negative:!0}:{nonZero:!0}));const c=[];for(let e=a;s<0?e>i:e<i;e+=s)c.push(e);return c},arity:{min:1,max:3},docs:{category:"array",returns:{type:"number",array:!0},args:{a:{type:"number"},b:{type:"number"},step:{type:"number"}},variants:[{argumentNames:["b"]},{argumentNames:["a","b"]},{argumentNames:["a","b","step"]}],description:"$range creates an array with a range of numbers from $a to $b (exclusive), by $step.\n\n$a defaults to 0.\n$step defaults to 1.",seeAlso:["repeat","vector.linspace"],examples:["range(4)","range(1, 4)","1 range 10","range(0.4, 4.9)","\nrange(\n 0.25, // start value\n 1, // end value (exclusive)\n 0.25, // step value\n)"]}},repeat:{evaluate:([e,t],r)=>{re(t,r,{integer:!0,nonNegative:!0});const n=[];for(let r=0;r<t;r+=1)n.push(e);return n},arity:ie(2),docs:{category:"array",returns:{type:"any",array:!0},args:{a:{type:"any"},b:{type:"integer"}},variants:[{argumentNames:["a","b"]}],description:"Returns an array with $a repeated $b times.",seeAlso:["range","string.string-repeat"],examples:["repeat(10, 3)","repeat(10, 0)",'"Albert" repeat 5']}},flatten:{evaluate:([e,t],r)=>{ke(e,r);const n=void 0===t||t===Number.POSITIVE_INFINITY?Number.POSITIVE_INFINITY:ne(t,r,{integer:!0,nonNegative:!0});return e.flat(n)},arity:{min:1,max:2},docs:{category:"array",returns:{type:"any",array:!0},args:{x:{type:["array","any"],description:"If $x is not an array, `[ ]` is returned."}},variants:[{argumentNames:["x"]}],description:"Takes a nested array $x and flattens it.",seeAlso:["mapcat"],examples:["flatten([1, 2, [3, 4], 5])",'\nlet foo = "bar";\nflatten([\n 1,\n " 2 A ",\n [foo, [4, ["ABC"]]],\n 6,\n])'],hideOperatorForm:!0}},mapcat:{evaluate:([e,t],r,n,{executeFunction:o})=>(ke(e,r),_(t,r),Ne($e(e,e=>o(t,[e],n,r)),e=>e.flat(1))),arity:ie(2),docs:{category:"array",returns:{type:"collection"},args:{a:{type:"collection"},b:{type:"function"},colls:{type:"collection",array:!0},fun:{type:"function"}},variants:[{argumentNames:["colls","fun"]}],description:"Returns the result of applying concat to the result of applying map to $fun and $colls.",seeAlso:["flatten","map","++"],examples:["[[3, 2, 1, 0], [6, 5, 4], [9, 8, 7]] mapcat reverse","mapcat([[3, 2, 1, 0], [6, 5, 4], [9, 8, 7]], reverse)","[[3, 2, 1, 0,], [6, 5, 4,], [9, 8, 7]] mapcat reverse","\nlet foo = (n) -> do\n [n - 1, n, n + 1]\nend;\n[1, 2, 3] mapcat foo","\nmapcat(\n [[1, 2], [2, 2], [2, 3]],\n -> $ filter odd?\n)"]}},"moving-fn":{evaluate:([e,t,r],n,o,{executeFunction:a})=>{ke(e,n),re(t,n,{integer:!0,lte:e.length}),_(r,n);const i=[];for(let r=0;r<=e.length-t;r++)i.push(e.slice(r,r+t));return $e(i,e=>a(r,[e],o,n))},arity:ie(3),docs:{category:"array",returns:{type:"array"},args:{arr:{type:"array"},windowSize:{type:"number",description:"The size of the moving window."},fn:{type:"function"}},variants:[{argumentNames:["arr","windowSize","fn"]}],description:"Returns the result of applying $fn to each moving window of size $windowSize in $arr.",seeAlso:["running-fn","vector.moving-mean"],examples:["moving-fn([1, 2, 3], 2, sum)","moving-fn([1, 2, 3], 1, sum)","moving-fn([1, 2, 3], 3, sum)"]}},"running-fn":{evaluate:([e,t],r,n,{executeFunction:o})=>{ke(e,r),_(t,r);const a=[];for(let t=0;t<e.length;t+=1)a.push(e.slice(0,t+1));return $e(a,e=>o(t,[e],n,r))},arity:ie(2),docs:{category:"array",returns:{type:"array"},args:{a:{type:"array"},b:{type:"function"}},variants:[{argumentNames:["a","b"]}],description:"Returns the result of applying $b to each element of $a.",seeAlso:["moving-fn","vector.running-mean"],examples:["running-fn([1, 2, 3], sum)","running-fn([1, 2, 3], max)","running-fn([1, 2, 3], min)"]}}},Ve={nth:{evaluate:(e,t)=>{const[r,n]=e,o=ge(e[2]);if(re(n,t,{integer:!0}),null===r)return o;if(F(r,t),n>=0&&n<r.length){return ge(r[n])}return o},arity:{min:2,max:3},docs:{category:"sequence",returns:{type:"any"},args:{a:{type:"sequence"},b:{type:"integer"},seq:{type:["sequence","null"]},n:{type:"integer"},"not-found":{type:"any"}},variants:[{argumentNames:["seq","n"]},{argumentNames:["seq","n","not-found"]}],description:"Accesses element $n of $seq. Accessing out-of-bounds indices returns $not-found, if present, else `null`.",seeAlso:["first","second","last","get","slice"],examples:["[1, 2, 3] nth 1",'"A string" nth 3',"nth([1, 2, 3], 1)","nth([1, 2, 3], 3)","nth([1, 2, 3], -1)","nth([1, 2, 3], 3, 99)",'nth("A string", 1)','nth("A string", 3)','nth("A string", -3)','nth("A string", 30, "X")',"nth(null, 1)",'nth(null, 1, "Default value")']}},first:{evaluate:([e],t)=>{if(null===e)return null;F(e,t);return ge(e[0])},arity:ie(1),docs:{category:"sequence",returns:{type:"any"},args:{seq:{type:["sequence","null"]}},variants:[{argumentNames:["seq"]}],description:"Returns the first element of $seq. If $seq is empty or `null`, `null` is returned.",seeAlso:["second","last","nth","rest","next"],examples:['first(["Albert", "Mojir", 160, [1, 2]])',"first([])","first(null)"]}},last:{evaluate:([e],t)=>{if(null===e)return null;F(e,t);return ge(e.at(-1))},arity:ie(1),docs:{category:"sequence",returns:{type:"any"},args:{seq:{type:["sequence","null"]}},variants:[{argumentNames:["seq"]}],description:"Returns the last element of $seq. If $seq is empty, `null` is returned.",seeAlso:["first","second","nth","pop"],examples:['last(["Albert", "Mojir", 160, [1, 2]])',"last([1, 2])","last([1])","last([])","last(null)"]}},pop:{evaluate:([e],t)=>(F(e,t),"string"==typeof e?e.substring(0,e.length-1):e.slice(0,e.length-1)),arity:ie(1),docs:{category:"sequence",returns:{type:["sequence","null"],rest:!0},args:{seq:{type:"sequence"}},variants:[{argumentNames:["seq"]}],description:"Returns a copy of $seq with last element removed. If $seq is empty `null` is returned.",seeAlso:["push","last"],examples:["pop([1, 2, 3])","pop([])"]}},"index-of":{evaluate:([e,t],r)=>{if(R(t,r),null===e)return null;if(F(e,r),"string"==typeof e){Y(t,r);const n=e.indexOf(t);return-1!==n?n:null}{const n=e.findIndex(e=>ue(C(e,r),t),r);return-1!==n?n:null}},arity:ie(2),docs:{category:"sequence",returns:{type:["number","null"]},args:{a:{type:"sequence"},b:{type:"any"},seq:{type:["sequence","null"]},x:{type:"any"}},variants:[{argumentNames:["seq","x"]}],description:"Returns the index of $x in $seq. If element is not present in $seq `null` is returned.",seeAlso:["sequence.last-index-of","sequence.position","contains?"],examples:["[[1], [2], [1], [2]] index-of [1]",'index-of(["Albert", "Mojir", 160, [1, 2]], "Mojir")',"index-of([5, 10, 15, 20], 15)","index-of([5, 10, 15, 20], 1)","index-of(null, 1)"]}},push:{evaluate:([e,...t],r)=>(F(e,r),"string"==typeof e?(Ae(t,r),[e,...t].join("")):[...e,...t]),arity:{min:2},docs:{category:"sequence",returns:{type:"sequence"},args:{a:{type:"sequence"},b:{type:"any"},seq:{type:"sequence"},values:{type:"any",rest:!0,description:"At least one."}},variants:[{argumentNames:["seq","values"]}],description:"Returns copy of $seq with $values added to the end of it.",seeAlso:["sequence.unshift","pop","++"],examples:["[1, 2, 3] push 4",'"Albert" push "!"',"push([1, 2, 3], 4)","push([1, 2, 3], 4, 5, 6)","\nlet l = [1, 2, 3];\npush(l, 4);\nl"]}},rest:{evaluate:([e],t)=>(F(e,t),Array.isArray(e)?e.length<=1?[]:e.slice(1):e.substring(1)),arity:ie(1),docs:{category:"sequence",returns:{type:["sequence","null"]},args:{seq:{type:"sequence"}},variants:[{argumentNames:["seq"]}],description:"If $seq is an array, returns a new array with all but the first element from $seq.\nIf $seq has less than two elements, an empty array is returned.\nFor string $seq returns all but the first characters in $seq.",seeAlso:["next","first"],examples:['rest(["Albert", "Mojir", 160, [1, 2]])','rest(["Albert"])',"rest([])",'rest("Albert")','rest("A",)','rest("")']}},next:{evaluate:([e],t)=>(F(e,t),Array.isArray(e)?e.length<=1?null:e.slice(1):e.length<=1?null:e.substring(1)),arity:ie(1),docs:{category:"sequence",returns:{type:["sequence","null"]},args:{seq:{type:"sequence"}},variants:[{argumentNames:["seq"]}],description:"If $seq is an array, returns a new array with all but the first element from $seq. If $seq has less than two elements, `null` is returned. For string $seq returns all but the first characters in $seq. If length of string $seq is less than two, `null` is returned.",seeAlso:["rest","first"],examples:['next(["Albert", "Mojir", 160, [1, 2]])','next(["Albert"])',"next([])",'next("Albert")','next("A",)','next("")']}},reverse:{evaluate:([e],t)=>null===e?null:(F(e,t),Array.isArray(e)?[...e].reverse():e.split("").reverse().join("")),arity:ie(1),docs:{category:"sequence",returns:{type:["sequence","null"]},args:{seq:{type:["sequence","null"]}},variants:[{argumentNames:["seq"]}],description:"If $seq is an array, creates a new array with the elements from $seq in reversed order. If $seq is a string, returns new reversed string.",seeAlso:["sort"],examples:['reverse(["Albert", "Mojir", 160, [1, 2]])',"reverse([])",'reverse("Albert")',"reverse(null)"]}},second:{evaluate:([e],t)=>null===e?null:(F(e,t),ge(e[1])),arity:ie(1),docs:{category:"sequence",returns:{type:"any"},args:{seq:{type:["sequence","null"]}},variants:[{argumentNames:["seq"]}],description:"Returns the second element of $seq. If $seq has less than two elements or is `null`, `null` is returned.",seeAlso:["first","last","nth"],examples:['second(["Albert", "Mojir", 160, [1, 2]])',"second([1])","second([])","second(null)"]}},slice:{evaluate:(e,t)=>{const[r,n,o]=e;return F(r,t),re(n,t,{integer:!0}),2===e.length?(Array.isArray(r),r.slice(n)):(re(o,t,{integer:!0}),Array.isArray(r),r.slice(n,o))},arity:{min:2,max:3},docs:{category:"sequence",returns:{type:"sequence"},args:{a:{type:"sequence"},b:{type:"integer"},seq:{type:"sequence",rest:!0},start:{type:"integer",description:"Defaults to `0`."},stop:{type:"integer",description:"Defaults lenght of sequence + 1."}},variants:[{argumentNames:["seq"]},{argumentNames:["seq","start"]},{argumentNames:["seq","start","stop"]}],description:"Returns a copy of a portion of $seq from index $start (inclusive) to $stop (exclusive).",seeAlso:["take","drop","sequence.splice","nth"],examples:["[1, 2, 3, 4, 5] slice 2","slice([1, 2, 3, 4, 5], 2, 4)","slice([1, 2, 3, 4, 5], 2)"]}},some:{evaluate:([e,t],r,n,{executeFunction:o})=>{if(_(t,r),null===e)return null;if(F(e,r),0===e.length)return null;return Se("string"==typeof e?e.split(""):e,(e,a)=>null!==e?e:Ne(o(t,[a],n,r),e=>e?ge(a):null),null)},arity:ie(2),docs:{category:"sequence",returns:{type:"any"},args:{a:{type:"sequence"},b:{type:"function"},seq:{type:["sequence","null"]},fun:{type:"function"}},variants:[{argumentNames:["seq","fun"]}],description:"Returns the first element that passes the test implemented by $fun. I no element was found, `null` is returned.",seeAlso:["sequence.position","collection.any?","find"],examples:['\nsome(\n ["Albert", "Mojir", 160, [1, 2]],\n string?\n)',"\nsome(\n [5, 10, 15, 20],\n -> $ > 10\n)","\nsome(\n [1, 2, 3, 4],\n -> $ > 10\n)","\nsome(\n [],\n -> $ > 10\n)","\nsome(\n null,\n -> $ > 10\n)"]}},sort:{evaluate:(e,t,r,{executeFunction:n})=>{const[o]=e,a=1===e.length,i=a?null:e[1];if(F(o,t),"string"==typeof o){const e=o.split("");return a?e.sort((e,r)=>me(e,r,t)):(_(i,t),e.sort((e,o)=>{const a=n(i,[e,o],r,t);if(a instanceof Promise)throw new TypeError("Async functions cannot be used as sort comparators");return re(a,t,{finite:!0}),a})),e.join("")}const s=[...o];return a?s.sort((e,r)=>(Q(e,t),Q(r,t),me(e,r,t))):s.sort((e,o)=>{_(i,t);const a=n(i,[e,o],r,t);if(a instanceof Promise)throw new TypeError("Async functions cannot be used as sort comparators");return re(a,t,{finite:!0}),a}),s},arity:{min:1,max:2},docs:{category:"sequence",returns:{type:"any",rest:!0},args:{a:{type:"sequence"},b:{type:"function"},seq:{type:"sequence"},fun:{type:"function"}},variants:[{argumentNames:["seq"]},{argumentNames:["seq","fun"]}],description:"Returns a new sequence with the elements from $seq sorted according to $fun. If no $fun is supplied, builtin `compare` will be used.",seeAlso:["sequence.sort-by","compare","reverse","vector.sort-indices"],examples:["[3, 1, 2] sort (a, b) -> b - a","sort([3, 1, 2])","\nsort(\n [3, 1, 2],\n (a, b) -> cond case a < b then -1 case a > b then 1 case true then -1 end\n)","\nsort(\n [3, 1, 2],\n (a, b) -> cond case a > b then -1 case a < b then 1 case true then -1 end\n)"]}},take:{evaluate:([e,t],r)=>{re(t,r),F(e,r);const n=Math.max(Math.ceil(t),0);return e.slice(0,n)},arity:ie(2),docs:{category:"sequence",returns:{type:"sequence"},args:{a:{type:"sequence"},b:{type:"integer"},n:{type:"integer"},seq:{type:"sequence"}},variants:[{argumentNames:["seq","n"]}],description:"Constructs a new array/string with the $n first elements from $seq.",seeAlso:["take-last","take-while","drop","slice","sequence.split-at"],examples:["take([1, 2, 3, 4, 5], 3)","[1, 2, 3, 4, 5] take 3","take([1, 2, 3, 4, 5], 0)",'take("Albert", 2)','take("Albert", 50)']}},"take-last":{evaluate:([e,t],r)=>{F(e,r),re(t,r);const n=Math.max(Math.ceil(t),0),o=e.length-n;return e.slice(o)},arity:ie(2),docs:{category:"sequence",returns:{type:"sequence"},args:{a:{type:"sequence"},b:{type:"integer"},n:{type:"integer"},seq:{type:"sequence"}},variants:[{argumentNames:["seq","n"]}],description:"Constructs a new array with the $n last elements from $seq.",seeAlso:["take","drop-last"],examples:["take-last([1, 2, 3, 4, 5], 3)","[1, 2, 3, 4, 5] take-last 3","take-last([1, 2, 3, 4, 5], 0)"]}},drop:{evaluate:([e,t],r)=>{re(t,r);const n=Math.max(Math.ceil(t),0);return F(e,r),e.slice(n)},arity:ie(2),docs:{category:"sequence",returns:{type:"sequence"},args:{a:{type:"sequence"},b:{type:"integer"},seq:{type:"sequence"},n:{type:"integer"}},variants:[{argumentNames:["seq","n"]}],description:"Constructs a new array/string with the $n first elements dropped from $seq.",seeAlso:["drop-last","drop-while","take","slice","sequence.split-at"],examples:["drop([1, 2, 3, 4, 5], 3)","[1, 2, 3, 4, 5] drop 0",'drop("Albert", 2)','drop("Albert", 50)']}},"drop-last":{evaluate:([e,t],r)=>{F(e,r),re(t,r);const n=Math.max(Math.ceil(t),0),o=e.length-n;return e.slice(0,o)},arity:ie(2),docs:{category:"sequence",returns:{type:"sequence"},args:{a:{type:"sequence"},b:{type:"integer"},seq:{type:"sequence"},n:{type:"integer"}},variants:[{argumentNames:["seq","n"]}],description:"Constructs a new array with the $n last elements dropped from $seq.",seeAlso:["drop","take-last"],examples:["drop-last([1, 2, 3, 4, 5], 3)","[1, 2, 3, 4, 5] drop-last 3","drop-last([1, 2, 3, 4, 5], 0)"]}},"take-while":{evaluate:([e,t],r,n,{executeFunction:o})=>{F(e,r),_(t,r);const a="string"==typeof e?e.split(""):Array.from(e);return Ne(Ue(a,e=>Ne(o(t,[e],n,r),e=>!e)),t=>{const r=-1===t?a:a.slice(0,t);return"string"==typeof e?r.join(""):r})},arity:ie(2),docs:{category:"sequence",returns:{type:"sequence"},args:{a:{type:"sequence"},b:{type:"function"},seq:{type:"sequence"},fun:{type:"function"}},variants:[{argumentNames:["seq","fun"]}],description:"Returns the members of $seq in order, stopping before the first one for which `predicate` returns a falsy value.",seeAlso:["take","drop-while","sequence.split-with"],examples:["take-while(\n [1, 2, 3, 2, 1],\n -> $ < 3\n)","take-while(\n [1, 2, 3, 2, 1],\n -> $ > 3\n)"]}},"drop-while":{evaluate:([e,t],r,n,{executeFunction:o})=>{F(e,r),_(t,r);const a=Array.isArray(e)?e:e.split("");return Ne(Ue(a,e=>Ne(o(t,[e],n,r),e=>!e)),t=>-1===t?"string"==typeof e?"":[]:"string"==typeof e?a.slice(t).join(""):e.slice(t))},arity:ie(2),docs:{category:"sequence",returns:{type:"sequence"},args:{a:{type:"sequence"},b:{type:"function"},seq:{type:"sequence"},fun:{type:"function"}},variants:[{argumentNames:["seq","fun"]}],description:"Returns the members of $seq in order, skipping the fist elements for witch the `predicate` returns a truethy value.",seeAlso:["drop","take-while","sequence.split-with"],examples:["drop-while(\n [1, 2, 3, 2, 1],\n -> $ < 3\n)","drop-while(\n [1, 2, 3, 2, 1],\n -> $ > 3\n)"]}}},Be=new WeakSet,Le=new WeakSet,Ke=new WeakSet,_e=new WeakSet,Je=new WeakSet,Ye=new WeakSet,He=new WeakSet;function Xe(e){return Array.isArray(e)?(Be.has(e)||(Ze(e),at(e)||nt(e)),e):e}function Ze(e){return!!Array.isArray(e)&&(!!Le.has(e)||!Ke.has(e)&&(e.every(e=>te(e))?(Be.add(e),Le.add(e),!0):(Ke.add(e),!1)))}function Qe(e,t){if(!Ze(e))throw new o(`Expected a vector, but got ${e}`,t)}function et(e,t){if(!function(e){return!!Ze(e)&&2===e.length}(e))throw new o(`Expected a 2d vector, but got ${e}`,t)}function tt(e,t){if(!function(e){return!!Ze(e)&&3===e.length}(e))throw new o(`Expected a 3d vector, but got ${e}`,t)}function rt(e,t){if(Qe(e,t),0===e.length)throw new o(`Expected a non empty vector, but got ${e}`,t)}function nt(e){if(!Array.isArray(e))return!1;if(Ye.has(e))return!0;if(He.has(e))return!1;if(0===e.length)return He.add(e),!1;if(!Array.isArray(e[0]))return He.add(e),!1;const t=e[0].length;for(const r of e.slice(1)){if(!Array.isArray(r))return He.add(e),!1;if(r.length!==t)return He.add(e),!1}return Be.add(e),Ye.add(e),!0}function ot(e,t){if(!nt(e))throw new o(`Expected a grid, but got ${e}`,t)}function at(e){if(!Array.isArray(e))return!1;if(_e.has(e))return!0;if(Je.has(e))return!1;if(0===e.length)return Je.add(e),!1;if(!Array.isArray(e[0])||0===e[0].length)return Je.add(e),!1;const t=e[0].length;for(const r of e)if(!Array.isArray(r)||r.length!==t||r.some(e=>!te(e)))return Je.add(e),!1;return Be.add(e),Ye.add(e),_e.add(e),!0}function it(e,t){if(!at(e))throw new o(`Expected a matrix, but got ${e}`,t)}function st(e,t){if(!at(e))throw new o(`Expected a matrix, but got ${e}`,t);if(e.length!==e[0].length)throw new o(`Expected square matrix, but got ${e.length} and ${e[0].length}`,t)}function ct(e,t){let r=!1,n=!1;for(const a of e)if(Ze(a))r=!0;else if(at(a))n=!0;else if(!te(a))throw new o("Invalid parameter type: "+typeof a,t);if(n){if(r)throw new o("Cannot mix vector and matrix types",t);let n=null,a=null;for(const r of e)if(at(r))if(null===n)n=r.length,a=r[0].length;else if(r.length!==n||r[0].length!==a)throw new o("Matrix dimensions do not match",t);return["matrix",e.map(e=>at(e)?e:Array.from({length:n},()=>Array.from({length:a},()=>e)))]}if(r){let r=null;for(const n of e)if(Ze(n))if(null===r)r=n.length;else if(n.length!==r)throw new o("Vector lengths do not match",t);return["vector",e.map(e=>Ze(e)?e:Array.from({length:r},()=>e))]}return["number",e]}function lt(e){return(t,r)=>{const[n,o]=ct(t,r);return"number"===n?e(o[0]):"vector"===n?o[0].map(t=>e(t)):o[0].map(t=>t.map(t=>e(t)))}}function mt(e){return(t,r)=>{const[n,o]=ct(t,r);return"number"===n?e(o[0],o[1]):"vector"===n?o[0].map((t,r)=>e(t,o[1][r])):o[0].map((t,r)=>t.map((t,n)=>e(t,o[1][r][n])))}}function ut(e,t){return(r,n)=>{if(0===r.length)return e;const[o,a]=ct(r,n);if("number"===o)return a.reduce((e,r)=>t(e,r),e);if("vector"===o){const[e,...r]=a;return r.reduce((e,r)=>e.map((e,n)=>t(e,r[n])),e)}{const[e,...r]=a;return r.reduce((e,r)=>e.map((e,n)=>e.map((e,o)=>t(e,r[n][o]))),e)}}}const pt={inc:{evaluate:lt(e=>e+1),arity:ie(1),docs:{category:"math",returns:{type:"number"},args:{x:{type:["number","vector","matrix"]}},variants:[{argumentNames:["x"]}],description:"The `inc` function increments its argument by 1, working on `numbers` and element-wise on `vectors` and `matrices`. When applied to collections, it increases each element by 1 while preserving the original structure.",seeAlso:["dec","+"],examples:["inc(0)","inc(1)","inc(100.1)","inc([1, 2, 3])","inc([[1, 2], [3, 4]])"]}},dec:{evaluate:lt(e=>e-1),arity:ie(1),docs:{category:"math",returns:{type:["number","vector","matrix"]},args:{x:{type:["number","vector","matrix"]}},variants:[{argumentNames:["x"]}],description:"The `dec` function decrements its argument by 1, working on `numbers` and element-wise on `vectors` and `matrices`. When applied to collections, it decreases each element by 1 while preserving the original structure.",seeAlso:["inc","-"],examples:["dec(0)","dec(1)","dec(100.1)","dec([1, 2, 3])","dec([[1, 2], [3, 4]])"]}},"+":{evaluate:ut(0,(e,t)=>e+t),arity:{},docs:{category:"math",returns:{type:["number","vector","matrix"]},args:{a:{type:["number","vector","matrix"]},b:{type:["number","vector","matrix"]},xs:{type:["number","vector","matrix"],rest:!0}},variants:[{argumentNames:["xs"]}],description:"The `+` function performs addition of numbers and element-wise addition of `vectors` and `matrices` of compatible dimensions, returning the same type as its inputs. When used with mixed types, it adds the scalar to each element of the collection.",seeAlso:["-","*","/","inc"],examples:["1 + 2","1 + 20 + 30","+(1, 2, 3, 4)","+()","+(1)","[1, 2, 3] + 2","[1, 2, 3] + [4, 5, 6]","[[1, 2, 3], [4, 5, 6]] + [[7, 8, 9], [10, 11, 12]]","[[1, 2, 3], [4, 5, 6]] + 2"]}},"*":{evaluate:ut(1,(e,t)=>e*t),arity:{},docs:{category:"math",returns:{type:["number","vector","matrix"]},args:{a:{type:["number","vector","matrix"]},b:{type:["number","vector","matrix"]},xs:{type:["number","vector","matrix"],rest:!0}},variants:[{argumentNames:["xs"]}],description:"The `*` function performs multiplication of `numbers` and element-wise multiplication of `vectors` and `matrices` of compatible dimensions, returning the same type as its inputs. When used with mixed types, it multiplies each element of the collection by the scalar.",seeAlso:["/","+","-","^"],examples:["6 * 7","-1 * 4","*(4, 7)","*(1, 2, 3, 4, 5)","*()","*(8)","[1, 2, 3] * 2","[1, 2, 3] * [4, 5, 6]","[[1, 2, 3], [4, 5, 6]] * [[7, 8, 9], [10, 11, 12]]","[[1, 2, 3], [4, 5, 6]] * 2"]}},"/":{evaluate:(e,t)=>{if(0===e.length)return 1;const[r,n]=ct(e,t);if("number"===r){const[e,...t]=n;return 0===t.length?1/e:t.reduce((e,t)=>e/t,e)}if("vector"===r){const e=n[0];return n.slice(1).reduce((e,t)=>e.map((e,r)=>e/t[r]),e)}{const e=n[0];return n.slice(1).reduce((e,t)=>e.map((e,r)=>e.map((e,n)=>e/t[r][n])),e)}},arity:{},docs:{category:"math",returns:{type:["number","vector","matrix"]},args:{a:{type:["number","vector","matrix"]},b:{type:["number","vector","matrix"]},xs:{type:["number","vector","matrix"],rest:!0}},variants:[{argumentNames:["xs"]}],description:"The `/` function performs division of `numbers` and element-wise division of `vectors` and `matrices` of compatible dimensions, returning the same type as its inputs. When used with mixed types, it divides each element of the collection by the scalar.",seeAlso:["*","+","-","quot","mod","%"],examples:["12 / 100","-1 / 4","/(7, 4)","/(1, 2, 4, 8)","/()","/(8)","[1, 2, 3] / 2","[1, 2, 3] / [4, 5, 6]","[[1, 2, 3], [4, 5, 6]] / [[7, 8, 9], [10, 11, 12]]","[[1, 2, 3], [4, 5, 6]] / 2"]}},"-":{evaluate:(e,t)=>{if(0===e.length)return 0;const[r,n]=ct(e,t);if("number"===r){const[e,...t]=n;return 0===t.length?-e:t.reduce((e,t)=>e-t,e)}if("vector"===r){const e=n[0];return n.slice(1).reduce((e,t)=>e.map((e,r)=>e-t[r]),e)}{const e=n[0];return n.slice(1).reduce((e,t)=>e.map((e,r)=>e.map((e,n)=>e-t[r][n])),e)}},arity:{},docs:{category:"math",returns:{type:["number","vector","matrix"]},args:{a:{type:["number","vector","matrix"]},b:{type:["number","vector","matrix"]},xs:{type:["number","vector","matrix"],rest:!0}},variants:[{argumentNames:["xs"]}],description:"Computes difference between first value and sum of the rest. When called with only one argument, it does negation.",seeAlso:["+","*","/","dec","abs"],examples:["50 - 8","1 - 1 - 1","-()","-(4, 2)","-(4, 3, 2, 1,)","[1, 2, 3] - 2","[1, 2, 3] - [4, 5, 6]","[[1, 2, 3], [4, 5, 6]] - [[7, 8, 9], [10, 11, 12]]","[[1, 2, 3], [4, 5, 6]] - 2"]}},quot:{evaluate:mt((e,t)=>Math.trunc(e/t)),arity:ie(2),docs:{category:"math",returns:{type:["number","vector","matrix"]},args:{a:{type:["number","vector","matrix"]},b:{type:["number","vector","matrix"]}},variants:[{argumentNames:["a","b"]}],description:"The `quot` function performs integer division truncated toward zero, working on `numbers` and element-wise on `vectors` and `matrices` of compatible dimensions. When used with mixed types, it applies integer division between each element of the collection and the scalar.",seeAlso:["mod","%","/","trunc"],examples:["quot(5, 3)","quot(5.2, 3.1)","quot(-5, 3)","5 quot -3","-5 quot -3","quot(5, 0)","quot(0, 5)","[1, 2, 3] quot 2","2 quot [1, 2, 3]","quot([1, 2, 3], [4, 5, 6])","[[1, 2, 3], [4, 5, 6]] quot [[7, 8, 9], [10, 11, 12]]","quot([[1, 2, 3], [4, 5, 6]], 2)","[[1, 2, 3], [4, 5, 6]] quot [[7, 8, 9], [10, 11, 12]]"]}},mod:{evaluate:mt((e,t)=>e-t*Math.floor(e/t)),arity:ie(2),docs:{category:"math",returns:{type:["number","vector","matrix"]},args:{a:{type:["number","vector","matrix"]},b:{type:["number","vector","matrix"]}},variants:[{argumentNames:["a","b"]}],description:"The `mod` function computes the modulo of division with the same sign as the divisor, working on `numbers` and element-wise on `vectors` and `matrices` of compatible dimensions. When used with mixed types, it applies the modulo operation between each element of the collection and the scalar.",seeAlso:["%","quot","/"],examples:["mod(5, 3)","mod(5.2, 3.1)","mod(-5, 3)","5 mod -3","-5 mod -3","[1, 2, 3] mod 2","2 mod [1, 2, 3]","mod([1, 2, 3], [4, 5, 6])","[[1, 2, 3], [4, 5, 6]] mod [[7, 8, 9], [10, 11, 12]]","mod([[1, 2, 3], [4, 5, 6]], 2)"]}},"%":{evaluate:mt((e,t)=>e%t),arity:ie(2),docs:{category:"math",returns:{type:["number","vector","matrix"]},args:{a:{type:["number","vector","matrix"]},b:{type:["number","vector","matrix"]}},variants:[{argumentNames:["a","b"]}],description:"The `%` function computes the remainder of division with the same sign as the dividend, working on `numbers` and element-wise on `vectors` and `matrices` of compatible dimensions. When used with mixed types, it applies the remainder operation between each element of the collection and the scalar.",seeAlso:["mod","quot","/"],examples:["5 % 3","5.2 % 3.1","-5 % 3","%(5, -3)","%(-5, -3)","[1, 2, 3] % 2","2 % [1, 2, 3]","%([1, 2, 3], [4, 5, 6])","[[1, 2, 3], [4, 5, 6]] % [[7, 8, 9], [10, 11, 12]]","%([[1, 2, 3], [4, 5, 6]], 2)"]}},sqrt:{evaluate:lt(e=>Math.sqrt(e)),arity:ie(1),docs:{category:"math",returns:{type:["number","vector","matrix"]},args:{x:{type:["number","vector","matrix"]}},variants:[{argumentNames:["x"]}],description:"The `sqrt` function calculates the square root of `numbers` and computes element-wise square roots of `vectors` and `matrices`. When applied to collections, it returns the square root of each element while preserving the original structure.",seeAlso:["cbrt","^"],examples:["sqrt(0)","sqrt(9)","sqrt(2)","sqrt(0)","sqrt(9)","sqrt(2)","sqrt([1, 4, 9])","sqrt([[1, 4], [9, 16]])"]}},cbrt:{evaluate:lt(e=>Math.cbrt(e)),arity:ie(1),docs:{category:"math",returns:{type:["number","vector","matrix"]},args:{x:{type:["number","vector","matrix"]}},variants:[{argumentNames:["x"]}],description:"The `cbrt` function calculates the cube root of `numbers` and computes element-wise cube roots of `vectors` and `matrices`. When applied to collections, it returns the cube root of each element while preserving the original structure.",seeAlso:["sqrt","^"],examples:["cbrt(0)","cbrt(27)","cbrt(2)","cbrt(1)","cbrt(0)","cbrt(27)","cbrt(2)","cbrt(1)","cbrt([1, 8, 27])","cbrt([[1, 8], [27, 64]])"]}},"^":{evaluate:mt((e,t)=>e**t),arity:ie(2),docs:{category:"math",returns:{type:["number","vector","matrix"]},args:{a:{type:["number","vector","matrix"]},b:{type:["number","vector","matrix"]}},variants:[{argumentNames:["a","b"]}],description:"The ^ function computes exponentiation, raising the first argument to the power of the second, working on `numbers` and element-wise on `vectors` and `matrices` of compatible dimensions. When used with mixed types, it applies the power operation between each element of the collection and the scalar.",seeAlso:["sqrt","cbrt","*","math.ln"],examples:["2 ^ 3","2 ^ 0","2 ^ -3","^(-2, 3)","^(-2, -3)","[1, 2, 3] ^ 2","2 ^ [1, 2, 3]","^([1, 2, 3], [4, 5, 6])","[[1, 2, 3], [4, 5, 6]] ^ [[7, 8, 9], [10, 11, 12]]","^([[1, 2, 3], [4, 5, 6]], 2)"]}},round:{evaluate:([e,t],r)=>{const[n,o]=ct([e],r);if("number"===n){if(void 0===t||0===t)return Math.round(o[0]);{re(t,r,{integer:!0,positive: