You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible to find a construction for advanced pattern matching including type signatures in vanilla* js?
The following construction doesn't seem very sound to me, but maybe we can come up with something.
// Turns numbers into strings, strings into numbers, and throws errors for anything elsepatternMatch(is_a(String),Number,is_a(Number),String,yes,identity);tryPatternMatch(is_a(String),Number,is_a(Number),String);exec(Y(rematch=>patternMatch(is_any([String,Number]),(v)=>rematch([v]),is_a(Array),map(x=>Number(x)*2+1),)));
*vanilla – as in no transpilers or compilers involved
The text was updated successfully, but these errors were encountered:
Update: We might be able to model this as a trait:
constPatternMatching=Trait("PatternMatching");// Implementation for primitives: by identity// Implementation for arrays, objects, etc: By recursion// Implementation for regex: As group matchconsttryMatch=(targ,default,pattern)=>ifdef(patternMatching.invoke(pattern,targ),(m)=>Object.assign([],m));constmatch=(targ,pattern)=>{constNone=Symbol();constm=tryMatch(targ,None,pattern);if(m===None)throwMatchFailed(/* … */);rerturnm;};constAnyMatcher=Tuple('any',[],{[PatternMatching.sym](value,matches){matches.push(value);}});constany=AnyMatcher.new();constany_as=NamedAnyMatcher.new;constgroup=(name,pattern)=>…// produces an entire subgroup matchconstas=(name,pattern)=>…// just binds the nameconstwhen=(predicate)=>…// Just executes the predicateconstanyof=(patterns)=>…// allows multiple patternsconstallof=(patterns)=>…// requires multiple patterns to matchconstexact=(value)=>…// match by identityconstequality=(value)=>…// match anything that equals the given predicateconstsubtype=(value)=>consttype=(value)=>constclassy=(type,records)=>constif_else_match=(targ,default,pattern,fn)=>{constNone=Symbol();constm=tryMatch(targ,None,pattern);returnm===None ? default : fn(m);};constif_match=(targ,pattern,fn)=>if_else_match(targ,undefined,pattern,fn);constmatch_on=(value,patterns…)=>…constfibonacci=(x)=>match_on(x,match(0,_=>0),match(1,_=>1),match(any,(_)=>fibonacci(x-1)+fibonacci(x-2));// Complex exampleconstAdd=Tuple('Add',['a','b']);constMul=Tuple("Mul",['a','b']);constcalc=(tree)=>match_on(tree,match(Add(any,any),([a,b])=>a+b),match(Mul(any,any),([a,b])=>a*b),match(Number,(x)=>x,/* throws */);calc(2)// 2calc(Mul(Add(2,3),4))// 20
This basically models matching as a trait where the implementation is given the object to be matched; if the object matches successfully, the implementation returns an array that may contain number and string keys matched otherwise undefined is returned. Some standard types such as Object, Array, etc and Primitive types are implemented by default. Containers recurse and primitive types compare by identity. RegExps are integrated into matching. This probably needs to be extended to support customizers.
Is it possible to find a construction for advanced pattern matching including type signatures in vanilla* js?
The following construction doesn't seem very sound to me, but maybe we can come up with something.
*vanilla – as in no transpilers or compilers involved
The text was updated successfully, but these errors were encountered: