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
Case clauses currently allow only one input and output types, In and Out. By introducing an intermediary type Mid we can allow users to extract information from the initial value in a match clause, and use that intermediate output value in the guard case and the handler function. This would particularly be beneficial in regex patterns. Consider the following example:
Given an input string, we'd like to extract the color and type of a wearable. An implementation of this use case in the current version of the codebase might look like this:
constthing='red shirt';constpattern=()=>/(red|green|blue)\s+(shirt|pants|skirt|hat)/g;match(thing).case([pattern(),str=>{const[_,color,wearable]=pattern().exec(str);console.log(`It's a ${color}${wearable}`);}],[_,str=>console.log(`Don't know what ${str} is`)]);// prints "It's a red shirt"
Notice how the pattern constant is a function that returns the regex pattern, instead of being the pattern instance itself. This is due to the fact that RegExp instances are stateful, and therefore matching it and reusing it later would fail, as in the case below;
constthing='red shirt';constpattern=/(red|green|blue)\s+(shirt|pants|skirt|hat)/g;match(thing).case([pattern,str=>{const[_,color,wearable]=pattern.exec(str);console.log(`It's a ${color}${wearable}`);}],[_,str=>console.log(`Don't know what ${str} is`)]);// fails with the following error:// const [_, color, wearable] = pattern.exec(str);// ^// TypeError: object null is not iterable (cannot read property Symbol(Symbol.iterator))
Likewise, implementing guard cases against regex matches are so cumbersome that it's impractical:
constthing='yellow submarine';constpattern=()=>/(red|green|blue|yellow)\s+(shirt|pants|skirt|hat|submarine)/g;match(thing).case([pattern(),str=>{const[_,color,wearable]=pattern().exec(str);returncolor=='yellow'&&wearable=='submarine';},str=>{const[_,color,wearable]=pattern().exec(str);console.log(`We all live in a ${color}${wearable}, ${color}${wearable}, ${color}${wearable}`);}],[pattern(),str=>{const[_,color,wearable]=pattern().exec(str);console.log(`It's a ${color}${wearable}`);}],[_,str=>console.log(`Don't know what ${str} is`)]);// prints "We all live in a yellow submarine, yellow submarine, yellow submarine"
If transformative match clauses were to be implemented, we'd expect the examples above to be boiled down to the following:
constthing='red shirt';constpattern=/(red|green|blue)\s+(shirt|pants|skirt|hat)/g;match(thing).case([pattern,([_,color,wearable])=>console.log(`It's a ${color}${wearable}`)],[_,str=>console.log(`Don't know what ${str} is`)]);// prints "It's a red shirt"
import{_,match}from'..';constthing='yellow submarine';constpattern=()=>/(red|green|blue|yellow)\s+(shirt|pants|skirt|hat|submarine)/g;match(thing).case([pattern(),[_,'yellow','submarine'],([_,color,wearable])=>console.log(`We all live in a ${color}${wearable}, ${color}${wearable}, ${color}${wearable}`)],[pattern(),([_,color,wearable])=>console.log(`It's a ${color}${wearable}`)],[_,str=>console.log(`Don't know what ${str} is`)]);// prints "We all live in a yellow submarine, yellow submarine, yellow submarine"
Note: We had to reuse the same pattern in two matching match clauses, so we converted it back into a function
The text was updated successfully, but these errors were encountered:
Case clauses currently allow only one input and output types,
In
andOut
. By introducing an intermediary typeMid
we can allow users to extract information from the initial value in a match clause, and use that intermediate output value in the guard case and the handler function. This would particularly be beneficial in regex patterns. Consider the following example:Given an input string, we'd like to extract the color and type of a wearable. An implementation of this use case in the current version of the codebase might look like this:
Notice how the
pattern
constant is a function that returns the regex pattern, instead of being the pattern instance itself. This is due to the fact that RegExp instances are stateful, and therefore matching it and reusing it later would fail, as in the case below;Likewise, implementing guard cases against regex matches are so cumbersome that it's impractical:
If transformative match clauses were to be implemented, we'd expect the examples above to be boiled down to the following:
Note: We had to reuse the same pattern in two matching match clauses, so we converted it back into a function
The text was updated successfully, but these errors were encountered: