Comments (12)
Ah cool, yeah if you're interested I can modify what I've done to work with the guard syntax you're after.
Could also make it generate es5 so it wouldn't need to be transpiled/ran in a modern browser.
(as a PR)
from pattern-guard.
Awwesome 👍 I think the only issue was that some browsers will fail on the es6 stuff (const {destructure}).
from pattern-guard.
Hi @edmulraney,
Thanks a lot for your feedback! Of course, you can do it this way but IMHO your solution is a lot more verbose and starts to be really hard to read.
My point was to re-implement the Haskell pattern guard syntax, which I find quite clean and expressive.
By the way, I'm not really sure if what you suggest is really an issue (more an alternative implementation which is totally fine). Do you mind if we close it?
from pattern-guard.
Yeah sorry, close this 👍
I was curious about the tradeoff of using template syntax vs plain JS.
Cheers @yamafaktory 😄
from pattern-guard.
Thanks @edmulraney 🍻
from pattern-guard.
I had a quick go at reducing verbosity and came up with this monstrosity:
const [a, b, c] = [1, 3, 7]
const destructProps = args => Object.keys(args).join(",")
const buildPredicateFn = (args, pattern) => new Function('', `const {${destructProps(args)}} = ${JSON.stringify(args)}; return ${pattern}`)
const guard = args => patterns => patterns.find(pair => buildPredicateFn(args, pair[0])())[1]
const result = guard({a,b,c})([
['a > b', 1337],
['b > c', 999],
['c > a', 42],
['c > b', 11],
[true, 'default value']
])
console.log(result) // 42
Possible alternative to running VM, and would run in the browser.
from pattern-guard.
@edmulraney This sounds really great 👍.
As I want to stick with the Haskell pattern guard syntax and with a tagged template literal, I've modified your code as follow https://jsfiddle.net/yamafaktory/92mgnnpo/.
I think this could definitely be a great improvement as it would run in the browser (if supported, or could be transpiled). But in this case, maybe you can create a PR for what you did?
from pattern-guard.
Actually not that much needs to be done, the quick fiddle I did reusing your code mixed with mine is already working. We can drop the class in https://github.com/yamafaktory/pattern-guard/blob/master/index.js#L3 and simply export a function like you did.
from pattern-guard.
Had a go at making it parse the guard as a string just using string manipulation: https://goo.gl/crHzUo
Might be a bit faster than the regex.
const [a, b, c] = [1, 3, 7]
const destructProps = args => Object.keys(args).join(",")
const buildPredicateFn = (args, pattern) => new Function('', `const {${destructProps(args)}} = ${JSON.stringify(args)}; return ${pattern}`)
const buildPatternPairs = pattern => pipe(
split("\n"),
map(replace("| ", "")),
map(trim),
map(split(" = "))
)(pattern)
const guard = args => pattern => buildPatternPairs(pattern).find(pair => buildPredicateFn(args, pair[0])())[1]
const result = guard({a,b,c})(`
| a > b = 1337
| b > c = 999
| c > a = 42
| c > b = 11
| true = 'default value'
`)
console.log(result) // 42
from pattern-guard.
Thanks a lot for your work, I'm going to think about all of these implementations. I don't want to use Ramda (which is great) just for the benefit of the pipe
function, it will increase the size of the module and I want to avoid any dependency. I also want to keep the nicety of the tagged template literal (not enclosed in parens). The last point, I don't think using a regex will make it run slower (pre-compiled regex are faster).
from pattern-guard.
Fair do's. Ramda was just for POC ;)
Out of curiosity is the tagged template for aesthetics or functionality?
from pattern-guard.
Definitely, for aesthetics, many libraries are going this way recently (can think of https://github.com/hyperapp/hyperapp#html for example).
from pattern-guard.
Related Issues (2)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pattern-guard.