UNPKG

@mojir/lits

Version:

Lits is a pure functional programming language implemented in TypeScript

3 lines (2 loc) 16.4 kB
function r(r){if(!r.position||!r.code)return"";const e=r.position.column-1,n=r.code.length-e-1;return`${" ".repeat(Math.max(e,0))}^${" ".repeat(Math.max(n,0))}`}class e extends Error{sourceCodeInfo;shortMessage;constructor(n,a){const t=n instanceof Error?n.message:`${n}`;super(function(e,n){if(!n)return e;const a=`${n.position.line}:${n.position.column}`;return`${e}${n.filePath?`\n${n.filePath}:${a}`:`\nLocation ${a}`}\n${n.code}\n${r(n)}`}(t,a)),this.shortMessage=t,this.sourceCodeInfo=a,Object.setPrototypeOf(this,e.prototype),this.name="LitsError"}getCodeMarker(){return this.sourceCodeInfo&&r(this.sourceCodeInfo)}}const n={Number:1,String:2,NormalExpression:3,SpecialExpression:4,UserDefinedSymbol:5,NormalBuiltinSymbol:6,SpecialBuiltinSymbol:7,ReservedSymbol:8,Binding:9,Spread:10},a=new Set(Object.values(n));const t=new Set(["UserDefined","Partial","Comp","Constantly","Juxt","Complement","EveryPred","SomePred","Fnull","Builtin","SpecialBuiltin","NativeJsFunction","Module"]);function o(r){return null!==r&&"object"==typeof r&&("^^fn^^"in r&&"functionType"in r&&("string"==typeof(e=r.functionType)&&t.has(e)));var e}function m(r){return!(!Array.isArray(r)||r.length<2)&&("number"==typeof(e=r[0])&&a.has(e));var e}function i(r){return o(r)?`<function ${r.name||"λ"}>`:m(r)?`${e=r[0],Object.keys(n).find(r=>n[r]===e)}-node`:null===r?"null":"object"==typeof r&&r instanceof RegExp?`${r}`:"object"==typeof r&&r instanceof Error?r.toString():JSON.stringify(r);var e}function s(r,e){return r?.sourceCodeInfo??e}function d(r,n,a){return new e(`Expected ${r}, got ${i(n)}.`,s(n,a))}function l(r,e){if(!Array.isArray(r))throw d("array",r,e)}function p(r,e){return function(r,e){if(!function(r){return void 0!==r}(r))throw d("not undefined",r,e)}(r,e),r}function u(r,n,a={}){if(!function(r,e={}){return!("number"!=typeof r||Number.isNaN(r)||e.integer&&!Number.isInteger(r)||e.finite&&!Number.isFinite(r)||e.zero&&0!==r||e.nonZero&&0===r||e.positive&&r<=0||e.negative&&r>=0||e.nonPositive&&r>0||e.nonNegative&&r<0||"number"==typeof e.gt&&r<=e.gt||"number"==typeof e.gte&&r<e.gte||"number"==typeof e.lt&&r>=e.lt||"number"==typeof e.lte&&r>e.lte)}(r,a))throw new e(`Expected ${function(r){if(r.zero)return"zero";const e=function(r){return r.positive?"positive":r.negative?"negative":r.nonNegative?"non negative":r.nonPositive?"non positive":r.nonZero?"non zero":""}(r),n=r.integer?"integer":"number",a=r.finite?"finite":"",t=function(r){return"number"!=typeof r.gt&&"number"!=typeof r.gte||"number"!=typeof r.lt&&"number"!=typeof r.lte?"number"==typeof r.gt||"number"==typeof r.gte?"number"==typeof r.gt?`n > ${r.gt}`:`n >= ${r.gte}`:"number"==typeof r.lt||"number"==typeof r.lte?"number"==typeof r.lt?`n < ${r.lt}`:`n <= ${r.lte}`:"":`${"number"==typeof r.gt?`${r.gt} < n `:`${r.gte} <= n `}${"number"==typeof r.lt?`< ${r.lt}`:`<= ${r.lte}`}`}(r);return[e,a,n,t].filter(r=>!!r).join(" ")}(a)}, got ${i(r)}.`,s(r,n))}function g(r,e,n={}){if(!function(r,e={}){return!("string"!=typeof r||e.nonEmpty&&0===r.length||e.char&&1!==r.length)}(r,n))throw d(""+(n.nonEmpty?"non empty string":n.char?"character":"string"),r,e)}function c(r){return{min:r,max:r}}const h={"random!":{category:"random",description:"Returns a random number between 0 and 1.",returns:{type:"number"},args:{},variants:[{argumentNames:[]}],examples:["let { random! } = import(random);\nrandom!()"],seeAlso:["random.random-float!","random.random-int!","random.random-boolean!"]},"random-int!":{category:"random",description:"Returns a random integer between min and max (exclusive).",returns:{type:"integer"},args:{a:{type:"integer",description:"The minimum value."},b:{type:"integer",description:"The maximum value (exclusive)."}},variants:[{argumentNames:["a","b"]}],examples:["let { random-int! } = import(random);\nrandom-int!(0, 10)","let { random-int! } = import(random);\nrandom-int!(1, 100)"],seeAlso:["random.random-int-inclusive!","random.random-float!","random.random!"]},"random-int-inclusive!":{category:"random",description:"Returns a random integer between min and max (inclusive).",returns:{type:"integer"},args:{a:{type:"integer",description:"The minimum value."},b:{type:"integer",description:"The maximum value (inclusive)."}},variants:[{argumentNames:["a","b"]}],examples:["let { random-int-inclusive! } = import(random);\nrandom-int-inclusive!(0, 10)"],seeAlso:["random.random-int!","random.random-float!"]},"random-float!":{category:"random",description:"Returns a random float between min and max.",returns:{type:"number"},args:{a:{type:"number",description:"The minimum value."},b:{type:"number",description:"The maximum value."}},variants:[{argumentNames:["a","b"]}],examples:["let { random-float! } = import(random);\nrandom-float!(0, 10)","let { random-float! } = import(random);\nrandom-float!(1, 100)"],seeAlso:["random.random!","random.random-int!","random.random-int-inclusive!"]},"random-boolean!":{category:"random",description:"Returns a random boolean.",returns:{type:"boolean"},args:{prob:{type:"number",description:"The probability of returning true (between 0 and 1)."}},variants:[{argumentNames:["prob"]}],examples:["let { random-boolean! } = import(random);\nrandom-boolean!()","let { random-boolean! } = import(random);\nrandom-boolean!(0.99)"],seeAlso:["random.random!"]},"random-item!":{category:"random",description:"Returns a random item from the array.",returns:{type:"any"},args:{a:{type:"array",description:"The array to sample from."}},variants:[{argumentNames:["a"]}],examples:["let { random-item! } = import(random);\nrandom-item!([1, 2, 3, 4, 5])",'let { random-item! } = import(random);\nrandom-item!(["apple", "banana", "cherry"])'],seeAlso:["random.random-sample!","random.random-sample-unique!","random.random-char!"]},"random-sample-unique!":{category:"random",description:"Returns a random sample of n unique items from the array.",returns:{type:"array"},args:{a:{type:"array",description:"The array to sample from."},b:{type:"integer",description:"The number of items to sample."}},variants:[{argumentNames:["a","b"]}],examples:["let { random-sample-unique! } = import(random);\nrandom-sample-unique!([1, 2, 3, 4, 5], 3)",'let { random-sample-unique! } = import(random);\nrandom-sample-unique!(["apple", "banana", "cherry"], 2)'],seeAlso:["random.random-sample!","random.random-item!","random.shuffle!"]},"random-sample!":{category:"random",description:"Returns a random sample of n items from the array.",returns:{type:"array"},args:{a:{type:"array",description:"The array to sample from."},b:{type:"integer",description:"The number of items to sample."}},variants:[{argumentNames:["a","b"]}],examples:["let { random-sample! } = import(random);\nrandom-sample!([1, 2, 3, 4, 5], 3)",'let { random-sample! } = import(random);\nrandom-sample!(["apple", "banana", "cherry"], 10)'],seeAlso:["random.random-sample-unique!","random.random-item!","random.shuffle!"]},"shuffle!":{category:"random",description:"Returns a shuffled version of the array.",returns:{type:"array"},args:{a:{type:"array",description:"The array to shuffle."}},variants:[{argumentNames:["a"]}],examples:["let { shuffle! } = import(random);\nshuffle!([1, 2, 3, 4, 5])",'let { shuffle! } = import(random);\nshuffle!(["apple", "banana", "cherry"])'],seeAlso:["random.random-sample!","random.random-sample-unique!"]},"random-normal!":{category:"random",description:"Returns a random number from a normal distribution with the given mean and standard deviation.",returns:{type:"number"},args:{mean:{type:"number",description:"The mean of the normal distribution."},stdDev:{type:"number",description:"The standard deviation of the normal distribution."}},variants:[{argumentNames:["mean","stdDev"]}],examples:["let { random-normal! } = import(random);\nrandom-normal!(0, 1)","let { random-normal! } = import(random);\nrandom-normal!(5, 2)"],seeAlso:["random.random-exponential!","random.random-binomial!","random.random-poisson!"],hideOperatorForm:!0},"random-exponential!":{category:"random",description:"Returns a random number from an exponential distribution with the given rate parameter.",returns:{type:"number"},args:{lambda:{type:"number",description:"The rate parameter of the exponential distribution."}},variants:[{argumentNames:["lambda"]}],examples:["let { random-exponential! } = import(random);\nrandom-exponential!(1)","let { random-exponential! } = import(random);\nrandom-exponential!(0.5)"],seeAlso:["random.random-normal!","random.random-poisson!","random.random-gamma!","random.random-pareto!"]},"random-binomial!":{category:"random",description:"Returns a random number from a binomial distribution with the given number of trials and probability of success.",returns:{type:"integer"},args:{n:{type:"integer",description:"The number of trials."},p:{type:"number",description:"The probability of success on each trial."}},variants:[{argumentNames:["n","p"]}],examples:["let { random-binomial! } = import(random);\nrandom-binomial!(10, 0.5)","let { random-binomial! } = import(random);\nrandom-binomial!(20, 0.3)"],seeAlso:["random.random-normal!","random.random-poisson!"],hideOperatorForm:!0},"random-poisson!":{category:"random",description:"Returns a random number from a Poisson distribution with the given rate parameter.",returns:{type:"integer"},args:{lambda:{type:"number",description:"The rate parameter of the Poisson distribution."}},variants:[{argumentNames:["lambda"]}],examples:["let { random-poisson! } = import(random);\nrandom-poisson!(1)","let { random-poisson! } = import(random);\nrandom-poisson!(5)"],seeAlso:["random.random-binomial!","random.random-normal!","random.random-exponential!"]},"random-gamma!":{category:"random",description:"Returns a random number from a gamma distribution with the given shape and scale parameters.",returns:{type:"number"},args:{shape:{type:"number",description:"The shape parameter of the gamma distribution."},scale:{type:"number",description:"The scale parameter of the gamma distribution."}},variants:[{argumentNames:["shape","scale"]}],examples:["let { random-gamma! } = import(random);\nrandom-gamma!(2, 2)","let { random-gamma! } = import(random);\nrandom-gamma!(5, 1)"],seeAlso:["random.random-exponential!","random.random-pareto!"],hideOperatorForm:!0},"random-pareto!":{category:"random",description:"Returns a random number from a Pareto distribution with the given shape parameter.",returns:{type:"number"},args:{alpha:{type:"number",description:"The shape parameter of the Pareto distribution."}},variants:[{argumentNames:["alpha"]}],examples:["let { random-pareto! } = import(random);\nrandom-pareto!(1)","let { random-pareto! } = import(random);\nrandom-pareto!(2)"],seeAlso:["random.random-gamma!","random.random-exponential!"]},"uuid!":{category:"random",description:"Returns a random UUID v4 (Universally Unique Identifier).",returns:{type:"string"},args:{},variants:[{argumentNames:[]}],examples:["let { uuid! } = import(random);\nuuid!()"],seeAlso:["random.random-id!","random.random-string!"]},"random-char!":{category:"random",description:"Returns a random character from the given string.",returns:{type:"string"},args:{charSet:{type:"string",description:"The string to sample from."}},variants:[{argumentNames:["charSet"]}],examples:['let { random-char! } = import(random);\nrandom-char!("abcde")','let { random-char! } = import(random);\nrandom-char!("ABCDEFGHIJKLMNOPQRSTUVWXYZ")'],seeAlso:["random.random-string!","random.random-item!"]},"random-string!":{category:"random",description:"Returns a random string of the given length from the given string.",returns:{type:"string"},args:{length:{type:"integer",description:"The length of the random string."},charSet:{type:"string",description:"The string to sample from."}},variants:[{argumentNames:["length","charSet"]}],examples:['let { random-string! } = import(random);\nrandom-string!(10, "abcde")','let { random-string! } = import(random);\nrandom-string!(5, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")'],seeAlso:["random.random-char!","random.random-id!","random.uuid!"],hideOperatorForm:!0},"random-id!":{category:"random",description:"Returns a random ID of the given length.",returns:{type:"string"},args:{length:{type:"integer",description:"The length of the random ID."}},variants:[{argumentNames:["length"]}],examples:["let { random-id! } = import(random);\nrandom-id!(10)","let { random-id! } = import(random);\nrandom-id!(5)"],seeAlso:["random.random-string!","random.uuid!"]},"random-color!":{category:"random",description:"Returns a random color in hex format.",returns:{type:"string"},args:{},variants:[{argumentNames:[]}],examples:["let { random-color! } = import(random);\nrandom-color!()"]}},f={"random!":{evaluate:()=>Math.random(),pure:!1,arity:c(0)},"random-int!":{evaluate:([r,e],n)=>(u(r,n,{integer:!0}),u(e,n,{integer:!0,gt:r}),Math.floor(Math.random()*(e-r))+r),pure:!1,arity:c(2)},"random-int-inclusive!":{evaluate:([r,e],n)=>(u(r,n,{integer:!0}),u(e,n,{integer:!0,gte:r}),Math.floor(Math.random()*(e-r+1))+r),pure:!1,arity:c(2)},"random-float!":{evaluate:([r,e],n)=>(u(r,n),u(e,n,{gt:r}),Math.random()*(e-r)+r),pure:!1,arity:c(2)},"random-boolean!":{evaluate:([r],e)=>{const n=r??.5;return u(n,e,{gte:0,lte:1}),Math.random()<n},pure:!1,arity:{min:0,max:1}},"random-item!":{evaluate:([r],e)=>{l(r,e);return p(r[Math.floor(Math.random()*r.length)])},pure:!1,arity:c(1)},"random-sample!":{evaluate:([r,n],a)=>{if(l(r,a),u(n,a,{integer:!0,nonNegative:!0}),0===r.length)throw new e("Cannot sample from an empty array.",a);const t=[];for(let e=0;e<n;e++){const e=Math.floor(Math.random()*r.length);t.push(r[e])}return t},pure:!1,arity:c(2)},"random-sample-unique!":{evaluate:([r,n],a)=>{if(l(r,a),u(n,a,{integer:!0,nonNegative:!0,lte:r.length}),0===r.length)throw new e("Cannot sample from an empty array.",a);const t=[],o=[...r];for(let r=0;r<n;r++){const r=Math.floor(Math.random()*o.length);t.push(o[r]),o.splice(r,1)}return t},pure:!1,arity:c(2)},"shuffle!":{evaluate:([r],e)=>{l(r,e);const n=[...r];for(let r=n.length-1;r>0;r--){const e=Math.floor(Math.random()*(r+1));[n[r],n[e]]=[n[e],n[r]]}return n},pure:!1,arity:c(1)},"random-normal!":{evaluate:([r,e],n)=>{u(r,n),u(e,n,{gt:0});const a=Math.random(),t=Math.random();return Math.sqrt(-2*Math.log(a))*Math.cos(2*Math.PI*t)*e+r},pure:!1,arity:c(2)},"random-exponential!":{evaluate:([r],e)=>{u(r,e,{gt:0});const n=Math.random();return-Math.log(n)/r},pure:!1,arity:c(1)},"random-binomial!":{evaluate:([r,e],n)=>{u(r,n,{integer:!0,nonNegative:!0}),u(e,n,{gte:0,lte:1});let a=0;for(let n=0;n<r;n++)Math.random()<e&&a++;return a},pure:!1,arity:c(2)},"random-poisson!":{evaluate:([r],e)=>{u(r,e,{gt:0});const n=Math.exp(-r);let a=0,t=1;do{a++,t*=Math.random()}while(t>n);return a-1},pure:!1,arity:c(1)},"random-gamma!":{evaluate:([r,e],n)=>(u(r,n,{gt:0}),u(e,n,{gt:0}),y(r,e)),pure:!1,arity:c(2)},"random-pareto!":{evaluate:([r],e)=>{u(r,e,{gt:0});return(1/Math.random())**(1/r)},pure:!1,arity:c(1)},"uuid!":{evaluate:()=>"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,r=>{const e=16*Math.random()|0;return("x"===r?e:3&e|8).toString(16)}),pure:!1,arity:c(0)},"random-char!":{evaluate:([r],n)=>{if(g(r,n),0===r.length)throw new e("Character set cannot be empty.",n);return r[Math.floor(Math.random()*r.length)]},pure:!1,arity:c(1)},"random-string!":{evaluate:([r,n],a)=>{if(u(r,a,{integer:!0,positive:!0}),g(n,a),0===n.length)throw new e("Character set cannot be empty.",a);let t="";for(let e=0;e<r;e++){t+=n[Math.floor(Math.random()*n.length)]}return t},pure:!1,arity:c(2)},"random-id!":{evaluate:([r],e)=>{u(r,e,{integer:!0,positive:!0});const n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";let a="";for(let e=0;e<r;e++){a+=n[Math.floor(62*Math.random())]}return a},pure:!1,arity:c(1)},"random-color!":{evaluate:()=>`#${Math.floor(16777216*Math.random()).toString(16).padStart(6,"0")}`,pure:!1,arity:c(0)}};function y(r,e=1){if(r<1){return y(r+1-1/3,e)*Math.random()**(1/r)}const n=r-1/3,a=1/Math.sqrt(9*n);let t,o,m;for(;;){do{t=b(),o=1+a*t}while(o<=0);if(o*=o*o,m=Math.random(),m<1-.0331*t*t*t*t)return e*n*o;if(Math.log(m)<.5*t*t+n*(1-o+Math.log(o)))return e*n*o}}function b(){let r=0,e=0;for(;0===r;)r=Math.random();for(;0===e;)e=Math.random();return Math.sqrt(-2*Math.log(r))*Math.cos(2*Math.PI*e)}for(const[r,e]of Object.entries(h))f[r]&&(f[r].docs=e);const v={name:"random",functions:f};export{v as randomModule}; //# sourceMappingURL=random.esm.js.map