@mojir/lits
Version:
Lits is a pure functional programming language implemented in TypeScript
3 lines (2 loc) • 7.24 kB
JavaScript
function n(n){if(!n.position||!n.code)return"";const t=n.position.column-1,e=n.code.length-t-1;return`${" ".repeat(Math.max(t,0))}^${" ".repeat(Math.max(e,0))}`}class t extends Error{sourceCodeInfo;shortMessage;constructor(e,r){const o=e instanceof Error?e.message:`${e}`;super(function(t,e){if(!e)return t;const r=`${e.position.line}:${e.position.column}`;return`${t}${e.filePath?`\n${e.filePath}:${r}`:`\nLocation ${r}`}\n${e.code}\n${n(e)}`}(o,r)),this.shortMessage=o,this.sourceCodeInfo=r,Object.setPrototypeOf(this,t.prototype),this.name="LitsError"}getCodeMarker(){return this.sourceCodeInfo&&n(this.sourceCodeInfo)}}const e={Number:1,String:2,NormalExpression:3,SpecialExpression:4,UserDefinedSymbol:5,NormalBuiltinSymbol:6,SpecialBuiltinSymbol:7,ReservedSymbol:8,Binding:9,Spread:10},r=new Set(Object.values(e));const o=new Set(["UserDefined","Partial","Comp","Constantly","Juxt","Complement","EveryPred","SomePred","Fnull","Builtin","SpecialBuiltin","NativeJsFunction","Module"]);const u="^^fn^^",a="^^re^^";function i(n){return null!==n&&"object"==typeof n&&(u in n&&"functionType"in n&&("string"==typeof(t=n.functionType)&&o.has(t)));var t}function l(n){return!(!Array.isArray(n)||n.length<2)&&("number"==typeof(t=n[0])&&r.has(t));var t}function s(n){return i(n)?`<function ${n.name||"λ"}>`:l(n)?`${t=n[0],Object.keys(e).find(n=>e[n]===t)}-node`:null===n?"null":"object"==typeof n&&n instanceof RegExp?`${n}`:"object"==typeof n&&n instanceof Error?n.toString():JSON.stringify(n);var t}function c(n){return null!==n&&"object"==typeof n&&!!n[u]}function f(n){return!(null===n||"object"!=typeof n||Array.isArray(n)||n instanceof RegExp||c(n)||(t=n,null!==t&&"object"==typeof t&&t[a]));var t}function m(n){return function(n){return Array.isArray(n)||"string"==typeof n}(n)||f(n)}function p(n,t){return y(n,t),n}function y(n,e){if(!function(n){return"number"==typeof n||!!m(n)||!!c(n)}(n))throw function(n,e,r){return new t(`Expected ${n}, got ${s(e)}.`,function(n,t){return n?.sourceCodeInfo??t}(e,r))}("FunctionLike",n,e)}function d(n){return"number"==typeof n||m(n)?g(1):n.arity}function g(n){return{min:n,max:n}}const h={name:"functional",functions:{juxt:{evaluate:(n,e)=>{n.forEach(n=>y(n,e));const r=function(n){return n.reduce((n,t)=>{if(null===n)return null;const e="number"==typeof t||m(t)?g(1):t.arity,{min:r,max:o}=e,{min:u,max:a}=n,i="number"==typeof r&&"number"==typeof u?Math.max(r,u):"number"==typeof r?r:"number"==typeof u?u:void 0,l="number"==typeof o&&"number"==typeof a?Math.min(o,a):"number"==typeof o?o:"number"==typeof a?a:void 0;return"number"==typeof i&&"number"==typeof l&&i>l?null:{min:i,max:l}},{})}(n);if(null===r)throw new t("All functions must accept the same number of arguments",e);return{[u]:!0,sourceCodeInfo:e,functionType:"Juxt",params:n,arity:r}},arity:{min:1},docs:{category:"functional",returns:{type:"function"},args:{a:{type:"function"},b:{type:"function"},fun:{type:"function"},fns:{type:"function",rest:!0}},variants:[{argumentNames:["fun"]},{argumentNames:["fun","fns"]}],description:"Takes one or many function and returns a function that is the juxtaposition of those functions.\nThe returned function takes a variable number of args,\nand returns a vector containing the result of applying each function to the args (left-to-right).",seeAlso:["comp"],examples:["let { juxt } = import(functional);\njuxt(+, *, min, max)(\n 3,\n 4,\n 6,\n)",'let { juxt } = import(functional);\njuxt("a", "b")(\n {\n a: 1,\n b: 2,\n c: 3,\n d: 4\n }\n)',"let { juxt } = import(functional);\njuxt(+, *, min, max) apply range(1, 11)"]}},complement:{evaluate:([n],t)=>{const e=p(n,t);return{[u]:!0,sourceCodeInfo:t,functionType:"Complement",function:e,arity:d(e)}},arity:g(1),docs:{category:"functional",returns:{type:"function"},args:{fun:{type:"function"}},variants:[{argumentNames:["fun"]}],description:"Takes a function $fun and returns a new function that takes the same arguments as f, has the same effects, if any, and returns the opposite truth value.",seeAlso:["comp","functional.every-pred","functional.some-pred"],examples:["let { complement } = import(functional);\ncomplement(>)(1, 3)","let { complement } = import(functional);\ncomplement(<)(1, 3)","let { complement } = import(functional);\ncomplement(+)(1, 3)","let { complement } = import(functional);\ncomplement(+)(0, 0)"]}},"every-pred":{evaluate:(n,t)=>({[u]:!0,sourceCodeInfo:t,functionType:"EveryPred",params:n,arity:{min:1,max:1}}),arity:{min:1},docs:{category:"functional",returns:{type:"function"},args:{fun:{type:"function"},fns:{type:"function",rest:!0}},variants:[{argumentNames:["fun"]},{argumentNames:["fun","fns"]}],description:"\nTakes a number of predicates and returns a function that returns `true` if all predicates\nreturn a truthy value against all of its arguments, else it returns `false`.",seeAlso:["functional.some-pred","functional.complement","collection.every?"],examples:['let { every-pred } = import(functional);\nevery-pred(string?, -> count($) > 3)(\n "Albert",\n "Mojir"\n)','let { every-pred } = import(functional);\n(string? every-pred -> count($) > 3)(\n "Albert",\n "M"\n)'],hideOperatorForm:!0}},"some-pred":{evaluate:(n,t)=>({[u]:!0,sourceCodeInfo:t,functionType:"SomePred",params:n,arity:{min:1,max:1}}),arity:{min:1},docs:{category:"functional",returns:{type:"function"},args:{fun:{type:"function"},fns:{type:"function",rest:!0}},variants:[{argumentNames:["fun"]},{argumentNames:["fun","fns"]}],description:"Takes a number of `predicates` and returns a function that returns `true` if at least one of the `predicates` return a truthy `true` value against at least one of its arguments, else it returns `false`.",seeAlso:["functional.every-pred","functional.complement","collection.any?"],examples:['let { some-pred } = import(functional);\nsome-pred(string?, -> count($) > 3)("Albert", "Mojir")','let { some-pred } = import(functional);\nsome-pred(string?, -> count($) > 3)("a", "M")','let { some-pred } = import(functional);\nsome-pred(string?, -> count($) > 3)("a", [1, 2, 3])',"let { some-pred } = import(functional);\nsome-pred(string?, -> count($) > 3)([1, 2, 3], [2])"],hideOperatorForm:!0}},fnull:{evaluate:([n,...t],e)=>{const r=p(n,e);return{[u]:!0,sourceCodeInfo:e,functionType:"Fnull",function:r,params:t,arity:d(r)}},arity:{min:2},docs:{category:"functional",returns:{type:"function"},args:{a:{type:"function"},b:{type:"any"},fun:{type:"function"},arg:{type:"any"},args:{type:"any",rest:!0}},variants:[{argumentNames:["fun","arg"]},{argumentNames:["fun","arg","args"]}],description:"Takes a function $fun, and returns a function that calls $fun, replacing a null argument to the corresponding argument.",seeAlso:["identity","constantly"],examples:["let { fnull } = import(functional);\nfnull(inc, 0)(1)","let { fnull } = import(functional);\nfnull(inc, 0)(null)","let { fnull } = import(functional);\n(inc fnull 0)(null)","let { fnull } = import(functional);\nfnull(+, 1, 2)(null, 0)","let { fnull } = import(functional);\nfnull(+, 1, 2)(0, null)","let { fnull } = import(functional);\nfnull(+, 1, 2)(null, null)","let { fnull } = import(functional);\nfnull(+, 1, 2)(null, null, 3, 4)"]}}}};export{h as functionalUtilsModule};
//# sourceMappingURL=functional.esm.js.map