Skip to content

Commit

Permalink
Merge pull request #13 from raine/update-readme
Browse files Browse the repository at this point in the history
README: explain caseOn
  • Loading branch information
paldepind committed Jun 22, 2015
2 parents 899dbe5 + 8745f68 commit b4e85bc
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ var area = Shape.case({
});
```

`caseOn` is similar to `case`, but allows passing additional data directly
into each case function. With `caseOn`, the `advancePlayer` example from
before could be written in "point-free style" like this:

```javascript
// No need to wrap this into a function that passes `player`
const advancePlayer = Move.caseOn({
Up: (player) => ({x: player.x, y: player.y - 1}),
Right: (player) => ({x: player.x + 1, y: player.y}),
Down: (player) => ({x: player.x, y: player.y + 1}),
Left: (player) => ({x: player.x - 1, y: player.y})
});

advancePlayer(Move.Up(), player);
```

As a catch all you can supply a property with the key `_` to case. When a type
doesn't match another handler `_` will be used.

Expand Down

0 comments on commit b4e85bc

Please sign in to comment.