evilsoft / evilsoft/crocks

Missing "join" method on ADTs

Open
#388 9 comments 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1.6k
Forks
103
PR merge metrics
No merged PRs in 30d

Description

Based on my understanding of many of the ADTs, as described [here](https://mostly-adequate.gitbooks.io/mostly-adequate-guide/ch09.html#mixing-metaphors), I think crocks is missing `join`.

Here is a simplified use case (I realize I could create this function with more of the functional building-blocks Crocks supplies):

```js
// { } -> String -> Either(Error String, a)
const getData = payload => {
return field => {
const data = payload[field]

// hmmm..
if (!data) {
return Left(new Error(`Cannot get field "${data}"`))
} else {
return Right(data)
}
}
}

const result = Either(
getData({ 10: 100, 'dogs': 'cats' })
)
.ap(Right('dogs'))

console.log(result)
// Right Right 'cats'
```

The problem is that the result is a `Right` inside a `Right`.

I'd like to be able to do this:

```js
const result = Either(
getData({ 10: 100, 'dogs': 'cats' })
)
.ap(Right('dogs'))
.join()

console.log(result)
// Right 'cats'
```

Am I missing something? Is there a different way to use `ap` that wouldn't create nested types?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.