@mojir/lits
Version:
Lits is a pure functional programming language implemented in TypeScript
1 lines • 607 kB
JavaScript
"use strict";function e(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 t extends Error{params;constructor(e){super(`recur, params: ${e}`),Object.setPrototypeOf(this,t.prototype),this.name="RecurSignal",this.params=e}}class r extends Error{sourceCodeInfo;shortMessage;constructor(t,n){const o=t instanceof Error?t.message:`${t}`;super(function(t,r){if(!r)return t;const n=`${r.position.line}:${r.position.column}`;return`${t}${r.filePath?`\n${r.filePath}:${n}`:`\nLocation ${n}`}\n${r.code}\n${e(r)}`}(o,n)),this.shortMessage=o,this.sourceCodeInfo=n,Object.setPrototypeOf(this,r.prototype),this.name="LitsError"}getCodeMarker(){return this.sourceCodeInfo&&e(this.sourceCodeInfo)}}class n extends r{userMessage;constructor(e,t){super(e,t),this.userMessage=e,Object.setPrototypeOf(this,n.prototype),this.name="UserDefinedError"}}class o extends r{constructor(e,t){super(e,t),Object.setPrototypeOf(this,o.prototype),this.name="AssertionError"}}class a extends r{symbol;constructor(e,t){super(`Undefined symbol '${e}'.`,t),this.symbol=e,Object.setPrototypeOf(this,a.prototype),this.name="UndefinedSymbolError"}}const i={Number:1,String:2,NormalExpression:3,SpecialExpression:4,UserDefinedSymbol:5,NormalBuiltinSymbol:6,SpecialBuiltinSymbol:7,ReservedSymbol:8,Binding:9,Spread:10},s=new Set(Object.values(i));function c(e){return Object.keys(i).find(t=>i[t]===e)}const l=new Set(["UserDefined","Partial","Comp","Constantly","Juxt","Complement","EveryPred","SomePred","Fnull","Builtin","SpecialBuiltin","NativeJsFunction","Module"]);const m="^^fn^^",u="^^re^^";function p(e){return null!==e&&"object"==typeof e&&(m in e&&"functionType"in e&&function(e){return"string"==typeof e&&l.has(e)}(e.functionType))}function g(e){return!(!Array.isArray(e)||e.length<2)&&function(e){return"number"==typeof e&&s.has(e)}(e[0])}function h(e){return p(e)?`<function ${e.name||"λ"}>`:g(e)?`${c(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 d(e,t){return e?.sourceCodeInfo??t}function y(e,t,n){return new r(`Expected ${e}, got ${h(t)}.`,d(t,n))}function v(e,t){return f(e,t),e}function f(e,t){if(!function(e){return void 0!==e}(e))throw new r("Unexpected undefined",d(e,t))}function b(e){return null!==e&&"object"==typeof e&&!Array.isArray(e)}function x(e){return null!==e&&"object"==typeof e&&!!e[m]}function w(e,t){if(!x(e))throw y("LitsFunction",e,t)}function k(e){return x(e)&&"UserDefined"===e.functionType}function q(e,t){if(!k(e))throw y("NativeJsFunction",e,t)}function A(e){return x(e)&&"NativeJsFunction"===e.functionType}function N(e,t){if(!A(e))throw y("NativeJsFunction",e,t)}function $(e){return void 0!==e}function T(e,t){return z(e,t),e}function z(e,t){if(!$(e))throw y("not undefined",e,t)}function S(e){return Array.isArray(e)||"string"==typeof e}function E(e,t){return j(e,t),e}function j(e,t){if(!S(e))throw y("string or array",e,t)}function M(e){return!(null===e||"object"!=typeof e||Array.isArray(e)||e instanceof RegExp||x(e)||O(e))}function C(e,t){if(!M(e))throw y("object",e,t)}function R(e){return S(e)||M(e)}function P(e,t){return I(e,t),e}function I(e,t){if(!R(e))throw y("string, array or object",e,t)}function O(e){return null!==e&&"object"==typeof e&&!!e[u]}function F(e,t){if(!function(e){return O(e)||"string"==typeof e}(e))throw y("string or RegularExpression",e,t)}function U(e,t){return D(e,t),e}function D(e,t){if(!function(e){return"number"==typeof e||!!R(e)||!!x(e)}(e))throw y("FunctionLike",e,t)}function W(e,t={}){return"string"==typeof e&&((!t.nonEmpty||0!==e.length)&&(!t.char||1===e.length))}function G(e,t,r={}){if(!W(e,r))throw y(""+(r.nonEmpty?"non empty string":r.char?"character":"string"),e,t)}function V(e,t,r={}){return G(e,t,r),e}function B(e){return"string"==typeof e||"number"==typeof e}function L(e,t){return _(e,t),e}function _(e,t){if(!B(e))throw y("string or number",e,t)}const K={assert:{evaluate:(e,t)=>{const r=e[0],n=2===e.length?e[1]:`${r}`;if(G(n,t),!r)throw new o(n,t);return T(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 J(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 Y(e,t,n={}){if(!J(e,n))throw new r(`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(" ")}(n)}, got ${h(e)}.`,d(e,t))}function H(e,t,r={}){return Y(e,t,r),e}function X(e){return"number"==typeof e||R(e)?ee(1):e.arity}function Z(e,t,n){const{min:o,max:a}=e;if("number"==typeof o&&t<o)throw new r(`Wrong number of arguments, expected at least ${o}, got ${h(t)}.`,n);if("number"==typeof a&&t>a)throw new r(`Wrong number of arguments, expected at most ${a}, got ${h(t)}.`,n)}function Q(e){return!("number"==typeof e.max&&e.max<2)&&!("number"==typeof e.min&&e.min>2)}function ee(e){return{min:e,max:e}}function te(e,t){return{a:{type:e},b:{type:t}}}const re={"<<":{evaluate:([e,t],r)=>(Y(e,r,{integer:!0}),Y(t,r,{integer:!0,nonNegative:!0}),e<<t),arity:ee(2),docs:{category:"bitwise",returns:{type:"integer"},args:{...te("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)=>(Y(e,r,{integer:!0}),Y(t,r,{integer:!0,nonNegative:!0}),e>>t),arity:ee(2),docs:{category:"bitwise",returns:{type:"integer"},args:{...te("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)=>(Y(e,r,{integer:!0}),Y(t,r,{integer:!0,nonNegative:!0}),e>>>t),arity:ee(2),docs:{category:"bitwise",returns:{type:"integer"},args:{...te("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)=>(Y(e,r,{integer:!0}),t.reduce((e,t)=>(Y(t,r,{integer:!0}),e&t),e)),arity:{min:2},docs:{category:"bitwise",returns:{type:"integer"},args:{...te("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)=>(Y(e,r,{integer:!0}),t.reduce((e,t)=>(Y(t,r,{integer:!0}),e|t),e)),arity:{min:2},docs:{category:"bitwise",returns:{type:"integer"},args:{...te("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)=>(Y(e,r,{integer:!0}),t.reduce((e,t)=>(Y(t,r,{integer:!0}),e^t),e)),arity:{min:2},docs:{category:"bitwise",returns:{type:"integer"},args:{...te("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 ne(e,t){return!!R(e)&&("string"==typeof e||Array.isArray(e)?!!J(t,{integer:!0})&&(t>=0&&t<e.length):!!Object.getOwnPropertyDescriptor(e,t))}function oe(e,t,n){if(_(e,n),_(t,n),"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 r(`Cannot compare values of different types: ${typeof e} and ${typeof t}`,n)}function ae(e,t,r){if(e===t)return!0;if("number"==typeof e&&"number"==typeof t)return ge(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(!ae(T(e[n],r),T(t[n],r),r))return!1;return!0}if(O(e)&&O(t))return e.s===t.s&&e.f===t.f;if(b(e)&&b(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=V(n[o],r);if(!ae(e[a],t[a],r))return!1}return!0}return!1}function ie(e){return Math.max(0,Math.ceil(e))}function se(e){return e??null}function ce(e){return M(e)?Object.entries(e).reduce((e,t)=>{const[r,n]=t;return e[r]=ce(n),e},{}):Array.isArray(e)?e.map(e=>ce(e)):e}function le(e){return ce(e)}function me(...e){const t=new Set;for(const r of e)r.forEach(e=>t.add(e));return t}function ue(e,t){t.forEach(t=>e.add(t))}const pe=1e-10;function ge(e,t,r=pe){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 he(e){return Math.abs(e)<pe}function de(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 ye(e,t){if(!Array.isArray(e))throw y("array",e,t)}function ve(e,t){if(!function(e){return Array.isArray(e)&&e.every(e=>"string"==typeof e)}(e))throw y("array of strings",e,t)}function fe(e,t){if(!function(e){return Array.isArray(e)&&e.every(e=>"string"==typeof e&&1===e.length)}(e))throw y("array of strings",e,t)}function be(e,t){return e instanceof Promise?e.then(t):t(e)}function xe(e,t){const r=[];for(let n=0;n<e.length;n++){const o=t(e[n],n);if(o instanceof Promise)return we(o,r,e,t,n);r.push(o)}return r}async function we(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 ke(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 qe(o,e,t,r);n=o}return n}async function qe(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 Ae(e,t){for(let r=0;r<e.length;r++){const n=t(e[r],r);if(n instanceof Promise)return Ne(n,e,t,r)}}async function Ne(e,t,r,n){await e;for(let e=n+1;e<t.length;e++)await r(t[e],e)}function $e(e,t){try{const r=e();return r instanceof Promise?r.catch(t):r}catch(e){return t(e)}}function Te(e,t){for(let r=0;r<e.length;r++){const n=t(e[r],r);if(n instanceof Promise)return ze(n,e,t,r);if(n)return!0}return!1}async function ze(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 Se(e,t){for(let r=0;r<e.length;r++){const n=t(e[r],r);if(n instanceof Promise)return Ee(n,e,t,r);if(!n)return!1}return!0}async function Ee(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 je(e,t){const r=[];for(let n=0;n<e.length;n++){const o=t(e[n],n);if(o instanceof Promise)return Me(o,r,e,t,n);o&&r.push(e[n])}return r}async function Me(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 Ce(e,t){for(let r=0;r<e.length;r++){const n=t(e[r],r);if(n instanceof Promise)return Re(n,e,t,r);if(n)return r}return-1}async function Re(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 Pe={filter:{evaluate:([e,t],r,n,{executeFunction:o})=>{if(I(e,r),D(t,r),Array.isArray(e))return ke(e,(e,a)=>be(o(t,[a],n,r),t=>(t&&e.push(a),e)),[]);if(W(e)){return be(ke(e.split(""),(e,a)=>be(o(t,[a],n,r),t=>(t&&e.push(a),e)),[]),e=>e.join(""))}return ke(Object.entries(e),(e,[a,i])=>be(o(t,[i],n,r),t=>(t&&(e[a]=i),e)),{})},arity:ee(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,n,{executeFunction:o})=>{const a=U(e.at(-1),t);if(M(e[0]))return function({colls:e,contextStack:t,executeFunction:n,fn:o,sourceCodeInfo:a}){C(e[0],a);const i=Object.keys(e[0]),s={};return e.forEach(e=>{C(e,a);const t=Object.keys(e);if(t.length!==i.length)throw new r(`All objects must have the same keys. Expected: ${i.join(", ")}. Found: ${t.join(", ")}`,a);if(!t.every(e=>i.includes(e)))throw new r(`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)})}),ke(i,(e,r)=>be(n(o,s[r],t,a),t=>(e[r]=t,e)),{})}({colls:e.slice(0,-1),fn:a,sourceCodeInfo:t,contextStack:n,executeFunction:o});const i=e.slice(0,-1);j(i[0],t);const s="string"==typeof i[0];let c=i[0].length;i.slice(1).forEach(e=>{s?G(e,t):ye(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=xe(l,e=>o(a,e,n,t));return s?be(m,e=>(e.forEach(e=>G(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})=>(I(e,n),D(t,n),z(r,n),"string"==typeof e?(G(r,n),0===e.length?r:ke(e.split(""),(e,r)=>a(t,[e,r],o,n),r)):Array.isArray(e)?0===e.length?r:ke(e,(e,r)=>a(t,[e,r],o,n),r):0===Object.keys(e).length?r:ke(Object.entries(e),(e,[,r])=>a(t,[e,r],o,n),r)),arity:ee(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=se(e[2]);if(_(n,t),null===r)return o;I(r,t);const a=function(e,t){if(M(e)){if("string"==typeof t&&ne(e,t))return se(e[t])}else if(J(t,{nonNegative:!0,integer:!0})&&t>=0&&t<e.length)return se(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:(I(e,t),Array.isArray(e)?e.length:Object.keys(e).length),arity:ee(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&&(I(e,r),W(e)?(G(t,r),e.includes(t)):S(e)?(z(t,r),!!e.find(e=>ae(T(e),t,r))):(G(t,r),t in e)),arity:ee(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)=>(I(e,n),_(t,n),z(r,n),function(e,t,r,n){if(I(e,n),_(t,n),Array.isArray(e)||"string"==typeof e){if(Y(t,n,{integer:!0}),Y(t,n,{gte:0}),Y(t,n,{lte:e.length}),"string"==typeof e)return G(r,n,{char:!0}),`${e.slice(0,t)}${r}${e.slice(t+1)}`;const o=[...e];return o[t]=r,o}G(t,n);const o={...e};return o[t]=r,o}(e,t,r,n)),arity:ee(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)=>(J(e[0])||I(e[0],t),Array.isArray(e[0])?e.reduce((e,r)=>(ye(r,t),e.concat(r)),[]):B(e[0])?e.reduce((e,r)=>(_(r,t),`${e}${r}`),""):e.reduce((e,r)=>(C(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 })"]}}},Ie={range:{evaluate:(e,t)=>{const[r,n,o]=e;let a,i,s;Y(r,t,{finite:!0}),1===e.length?(a=0,i=r,s=i>=0?1:-1):2===e.length?(Y(n,t,{finite:!0}),a=r,i=n,s=i>=a?1:-1):(Y(n,t,{finite:!0}),Y(o,t,{finite:!0}),a=r,i=n,s=o,Y(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)=>{Y(t,r,{integer:!0,nonNegative:!0});const n=[];for(let r=0;r<t;r+=1)n.push(e);return n},arity:ee(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)=>{ye(e,r);const n=void 0===t||t===Number.POSITIVE_INFINITY?Number.POSITIVE_INFINITY:H(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})=>(ye(e,r),D(t,r),be(xe(e,e=>o(t,[e],n,r)),e=>e.flat(1))),arity:ee(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})=>{ye(e,n),Y(t,n,{integer:!0,lte:e.length}),D(r,n);const i=[];for(let r=0;r<=e.length-t;r++)i.push(e.slice(r,r+t));return xe(i,e=>a(r,[e],o,n))},arity:ee(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})=>{ye(e,r),D(t,r);const a=[];for(let t=0;t<e.length;t+=1)a.push(e.slice(0,t+1));return xe(a,e=>o(t,[e],n,r))},arity:ee(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)"]}}},Oe={nth:{evaluate:(e,t)=>{const[r,n]=e,o=se(e[2]);if(Y(n,t,{integer:!0}),null===r)return o;if(j(r,t),n>=0&&n<r.length){return se(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;j(e,t);return se(e[0])},arity:ee(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;j(e,t);return se(e.at(-1))},arity:ee(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)=>(j(e,t),"string"==typeof e?e.substring(0,e.length-1):e.slice(0,e.length-1)),arity:ee(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(z(t,r),null===e)return null;if(j(e,r),"string"==typeof e){G(t,r);const n=e.indexOf(t);return-1!==n?n:null}{const n=e.findIndex(e=>ae(T(e,r),t),r);return-1!==n?n:null}},arity:ee(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)=>(j(e,r),"string"==typeof e?(fe(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)=>(j(e,t),Array.isArray(e)?e.length<=1?[]:e.slice(1):e.substring(1)),arity:ee(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)=>(j(e,t),Array.isArray(e)?e.length<=1?null:e.slice(1):e.length<=1?null:e.substring(1)),arity:ee(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:(j(e,t),Array.isArray(e)?[...e].reverse():e.split("").reverse().join("")),arity:ee(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:(j(e,t),se(e[1])),arity:ee(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 j(r,t),Y(n,t,{integer:!0}),2===e.length?(Array.isArray(r),r.slice(n)):(Y(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(D(t,r),null===e)return null;if(j(e,r),0===e.length)return null;return ke("string"==typeof e?e.split(""):e,(e,a)=>null!==e?e:be(o(t,[a],n,r),e=>e?se(a):null),null)},arity:ee(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(j(o,t),"string"==typeof o){const e=o.split("");return a?e.sort((e,r)=>oe(e,r,t)):(D(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 Y(a,t,{finite:!0}),a})),e.join("")}const s=[...o];return a?s.sort((e,r)=>(_(e,t),_(r,t),oe(e,r,t))):s.sort((e,o)=>{D(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 Y(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)=>{Y(t,r),j(e,r);const n=Math.max(Math.ceil(t),0);return e.slice(0,n)},arity:ee(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)=>{j(e,r),Y(t,r);const n=Math.max(Math.ceil(t),0),o=e.length-n;return e.slice(o)},arity:ee(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)=>{Y(t,r);const n=Math.max(Math.ceil(t),0);return j(e,r),e.slice(n)},arity:ee(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)=>{j(e,r),Y(t,r);const n=Math.max(Math.ceil(t),0),o=e.length-n;return e.slice(0,o)},arity:ee(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})=>{j(e,r),D(t,r);const a="string"==typeof e?e.split(""):Array.from(e);return be(Ce(a,e=>be(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:ee(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})=>{j(e,r),D(t,r);const a=Array.isArray(e)?e:e.split("");return be(Ce(a,e=>be(o(t,[e],n,r),e=>!e)),t=>-1===t?"string"==typeof e?"":[]:"string"==typeof e?a.slice(t).join(""):e.slice(t))},arity:ee(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)"]}}},Fe=new WeakSet,Ue=new WeakSet,De=new WeakSet,We=new WeakSet,Ge=new WeakSet,Ve=new WeakSet,Be=new WeakSet;function Le(e){return Array.isArray(e)?(Fe.has(e)||(_e(e),Qe(e)||Xe(e)),e):e}function _e(e){return!!Array.isArray(e)&&(!!Ue.has(e)||!De.has(e)&&(e.every(e=>J(e))?(Fe.add(e),Ue.add(e),!0):(De.add(e),!1)))}function Ke(e,t){if(!_e(e))throw new r(`Expected a vector, but got ${e}`,t)}function Je(e,t){if(!function(e){return!!_e(e)&&2===e.length}(e))throw new r(`Expected a 2d vector, but got ${e}`,t)}function Ye(e,t){if(!function(e){return!!_e(e)&&3===e.length}(e))throw new r(`Expected a 3d vector, but got ${e}`,t)}function He(e,t){if(Ke(e,t),0===e.length)throw new r(`Expected a non empty vector, but got ${e}`,t)}function Xe(e){if(!Array.isArray(e))return!1;if(Ve.has(e))return!0;if(Be.has(e))return!1;if(0===e.length)return Be.add(e),!1;if(!Array.isArray(e[0]))return Be.add(e),!1;const t=e[0].length;for(const r of e.slice(1)){if(!Array.isArray(r))return Be.add(e),!1;if(r.length!==t)return Be.add(e),!1}return Fe.add(e),Ve.add(e),!0}function Ze(e,t){if(!Xe(e))throw new r(`Expected a grid, but got ${e}`,t)}function Qe(e){if(!Array.isArray(e))return!1;if(We.has(e))return!0;if(Ge.has(e))return!1;if(0===e.length)return Ge.add(e),!1;if(!Array.isArray(e[0])||0===e[0].length)return Ge.add(e),!1;const t=e[0].length;for(const r of e)if(!Array.isArray(r)||r.length!==t||r.some(e=>!J(e)))return Ge.add(e),!1;return Fe.add(e),Ve.add(e),We.add(e),!0}function et(e,t){if(!Qe(e))throw new r(`Expected a matrix, but got ${e}`,t)}function tt(e,t){if(!Qe(e))throw new r(`Expected a matrix, but got ${e}`,t);if(e.length!==e[0].length)throw new r(`Expected square matrix, but got ${e.length} and ${e[0].length}`,t)}function rt(e,t){let n=!1,o=!1;for(const a of e)if(_e(a))n=!0;else if(Qe(a))o=!0;else if(!J(a))throw new r("Invalid parameter type: "+typeof a,t);if(o){if(n)throw new r("Cannot mix vector and matrix types",t);let o=null,a=null;for(const n of e)if(Qe(n))if(null===o)o=n.length,a=n[0].length;else if(n.length!==o||n[0].length!==a)throw new r("Matrix dimensions do not match",t);return["matrix",e.map(e=>Qe(e)?e:Array.from({length:o},()=>Array.from({length:a},()=>e)))]}if(n){let n=null;for(const o of e)if(_e(o))if(null===n)n=o.length;else if(o.length!==n)throw new r("Vector lengths do not match",t);return["vector",e.map(e=>_e(e)?e:Array.from({length:n},()=>e))]}return["number",e]}function nt(e){return(t,r)=>{const[n,o]=rt(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 ot(e){return(t,r)=>{const[n,o]=rt(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 at(e,t){return(r,n)=>{if(0===r.length)return e;const[o,a]=rt(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 it={inc:{evaluate:nt(e=>e+1),arity:ee(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:nt(e=>e-1),arity:ee(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:at(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:at(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]=rt(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]=rt(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:ot((e,t)=>Math.trunc(e/t)),arity:ee(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:ot((e,t)=>e-t*Math.floor(e/t)),arity:ee(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:ot((e,t)=>e%t),arity:ee(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:nt(e=>Math.sqrt(e)),arity:ee(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:nt(e=>Math.cbrt(e)),arity:ee(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:ot((e,t)=>e**t),arity:ee(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]=rt([e],r);if("number"===n){if(void 0===t||0===t)return Math.round(o[0]);{Y(t,r,{integer:!0,positive:!0});const e=10**t;return Math.round(o[0]*e)/e}}if("vector"===n){const e=o[0];if(void 0===t||0===t)return e.map(e=>Math.round(e));{Y(t,r,{integer:!0,positive:!0});const n=10**t;return e.map(e=>Math.round(e*n)/n)}}{const e=o[0];if(void 0===t||0===t)return e.map(e=>e.map(e=>Math.round(e)));{Y(t,r,{integer:!0,positive:!0});const n=10**t;return e.map(e=>e.map(e=>Math.round(e*n)/n))}}},arity:{min:1,max:2},docs:{category:"math",returns:{type:["number","vector","matrix"]},args:{a:{type:["number","vector","matrix"]},b:{type:"integer"}},variants:[{argumentNames:["a"]},{argumentNames:["a","b"]}],description:"The `round` function rounds a `number` to the nearest `integer` or to a specified number of `decimal` places, working on `numbers` and element-wise on `vectors` and `matrices`. When applied to collections, it rounds each element while preserving the original structure.",seeAlso:["floor","ceil","trunc"],examples:["round(2)","round(2.49)","round(2.5)","round(-2.49)","round(-2.5)","round(-2.501)","round(1.23456789, 4)","1.123456789 round 2","round([1.23456789, 2.3456789], 1)","[1.23456789, 2.3456789] round 4","[[1.23456789, 2.3456789], [3.456789, 4.56789]] round 4","round([[1.23456789, 2.3456789], [3.456789, 4.56789]], 2)"]}}