Skip to content

Commit

Permalink
docs(README): add Scala classic example to README
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiansen committed Jul 21, 2016
1 parent 295617f commit d1d3781
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,36 @@ const m = match(1)(
console.log(m); // Integer ONE
```

### Using Extractor Objects
Ported from the [Scala Example](http://docs.scala-lang.org/tutorials/tour/extractor-objects.html)
```
class Twice {
constructor(x) {
this.value = x*2;
this.funny_prop = 'funny';
}
unapply(x) {
if (x instanceof Twice && x.value%2 ==0) {
return x.value / 2
}
if( x%2 ==0 ) {
return x/2;
}else{
return undefined;
}
}
}
const twice5 = new Twice(5);
const m = match(twice5)(
[1, 'ONE'],
[new Twice(6), 'Twice(6)'],
[Twice, (x) => x]
)
console.log(m); // 5
```

### More Examples
See *.spec.es6.js files in the [test](./test) folder.

Expand Down

0 comments on commit d1d3781

Please sign in to comment.