diff --git a/README.md b/README.md index 5eb60fb..ccc59ca 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,9 @@ fact(10); // 3628800 Clear and simple right? -![oh-boy-2](https://cloud.githubusercontent.com/assets/138050/12031158/0e37afda-ae09-11e5-9462-873b45cbb2b4.gif) +

+ +

Note that `when()` is a catch-all pattern and should always be the last condition. If you forget, `match()` will throw a `MissingCatchAllPattern` exception. @@ -93,6 +95,7 @@ I will accept PR with their associated tests for the following features: - `match.and(pattern, ...)` - matches if every pattern matches. - support `range(x, y)` +- try and maybe support `match(input, {patterns...})` syntax instead of `match({patterns...})(input)`? [todo-list inspired by pattern-match from dherman](https://github.com/dherman/pattern-match#patterns). @@ -107,3 +110,51 @@ I will accept PR with their associated tests for the following features: I work at [iAdvize](iadvize.com) as a Lead developer and software architect. iAdvize is the leading real-time customer engagement platform in Europe and is used in 40 different countries. We are one of the french startup with the [fastest growth](http://www.iadvize.com/fr/wp-content/uploads/sites/2/2014/11/CP-Fast-50.pdf) and one of [the **greatest place to work** in **France**](https://vimeo.com/122438055). We are looking for a [NodeJS backend developer](http://smrtr.io/FqP79g), a [Scala backend developer](http://smrtr.io/FqP79g), a [JavaScript frontend developer](http://smrtr.io/wR-y4Q), a [Full-stack Developer](http://smrtr.io/SGhrew) and a [DevOps System Engineer](http://smrtr.io/OIFFMQ) in Paris or Nantes. **[Send me a tweet](https://twitter.com/FGRibreau) if you have any questions**! + +## The Story + +I was working on an internal tool at iAdvize [during our hack days (fr)](http://www.iadvize.com/blog/fr/product-swarms-organisation-parfaite/). This tool is representing our micro-service architecture and every of its components in a near real-time graph (I may open-source it in the future). + +Somewhere in the graph relationship code I thought wait a minute, would it be awesome to use some pattern-matching there? So I tried to write some syntax that could exist for it... + +```js +SyntaxError: /Users/FG/www/iadvize-services-orchestration-tools/src/api/src/repositoryManagers/github.js: Unexpected token (185:32) + 183 | + 184 | _.flatten(links).forEach(link => { +> 185 | [{protocol: 'HTTP'}]: => 1, + | ^ + 186 | [{protocol: 'AMQP'}]: => 2 + 187 | }); + 188 | +``` + +... and of course it failed. + +It was 6PM so the good news was I would have all the time I wanted to see if it could be possible to implement a pattern-matching solution that was both **syntaxically short**, would work well with **functional**-style code while using JavaScript ES6 features for conciseness. + +I ended up with this syntax: + +```js +[2, 4, 1, 2].map(match({ + [when(1)]: "one", + [when(2)]: "two", + [when()]: "many" +})); + +// [ 'two', 'many', 'one', 'two' ] +``` + +Which works also that way: + +```js +function length(list){ + return match({ + [when([])]: 0, + [when()]: ([head, ...tail]) => 1 + length(tail) + })(list); +} + +length([1, 1, 1]); // 3 +``` + +Underneath it uses [enhanced object literals](https://github.com/lukehoban/es6features#enhanced-object-literals), [symbols](https://github.com/lukehoban/es6features#symbols) and, sadly, JSON serialization/deserialization to pass object properties.