Skip to content

Commit

Permalink
docs(readme): explain motivation more
Browse files Browse the repository at this point in the history
  • Loading branch information
Gipphe committed Aug 4, 2018
1 parent e8ef3b1 commit 18f619f
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,24 @@ function whatFoo(x) {
}
```

To me, this is rather verbose, and there are a lot of things being repeated
in terms of statements. Keep in mind though: there is absolutely nothing
inherently *wrong* with this kind of "if-stacking".
Inevitably, even with monads encapsulating most of this kind of control flow
for you, you might inevitably find yourself repeating this kind of
"if-stacking" pattern.

To me, this is rather verbose and ugly, and there are a lot of things being
repeated in terms of statements. Keep in mind though: there is absolutely
nothing inherently *wrong* with this kind of "if-stacking".

Because of this, and because I simply adore [Elm](http://elm-lang.org/) and
[Haskell](https://www.haskell.org/)'s `case x of` expressions, I saw it
necessary to implement something along those lines in Javascript.
[Haskell](https://www.haskell.org/)'s `case x of` expressions (aka simply
"Case expressions"), I saw it necessary to implement something along those
lines in Javascript.

So, instead of the "ugly" if-stacking above, we can write

```javascript
function whatFoo(x) {
caseOf((when) => {
return caseOf((when) => {
when(x => x === 3)(() => 'foo')
when(x => x > 3)(() => 'bar')
when(x => x < 3)(() => 'foobar')
Expand Down Expand Up @@ -200,6 +205,31 @@ var S = require('sanctuary')
'foo'
```

Give it a little more breathing room,
[like you might do in a language where spaces are function application](https://github.com/sanctuary-js/sanctuary/issues/438),
and you've got yourself some easily readable code:

```javascript
var S = require ('sanctuary')
> caseOf ((when) => {
when (S.equals (3))
(S.K ('foo'))

when (S.gt (3))
(S.K ('bar'))

when (S.lt (3))
(S.K ('foobar'))

when (S.K (true))
(S.K ('quack'))
}) (3)
'foo'
```

That is, "easily readable" if you're used to a more Haskell-like style. How
you want to style your code is up to you.

## Contribution

This package is open to pull requests. To set up the development environment,
Expand Down

0 comments on commit 18f619f

Please sign in to comment.