fantasyland / fantasy-land

Specification for interoperability of common algebraic structures in JavaScript
MIT License
10.12k stars 374 forks source link

add Compactable algebra #278

Closed davidchambers closed 6 years ago

davidchambers commented 6 years ago

Closes #33

This is an alternative to #274, based around compact rather than filter. compact refers to Maybe a, so this pull request defines what is meant by a value of type Maybe a. I opted for “object encoding” as suggested by @paldepind, as it seems more idiomatic in a JavaScript setting.

Fresheyeball commented 6 years ago

Good job! Find throwing me a mention for this?

davidchambers commented 6 years ago

Find throwing me a mention for this?

Adding a link to Control.Compactable do you mean, @Fresheyeball? That sounds good to me.

rjmk commented 6 years ago

Got it. I think that could be clarified

Sent from my phone

El 1 dic 2017, a las 6:51 p. m., Gabe Johnson notifications@github.com escribió:

@gabejohnson commented on this pull request.

In README.md:

+Maybe a is used in type signatures in this specification. A JavaScript value +x is of type Maybe a if: +

    • x.isJust is false; or
    • x.isJust is true and x.value is of type a.
  • +{isJust: false} represents Nothing; {isJust: true, value: 42} represents +Just 42.

  • +### Nothing

  • +Nothing is defined as:

  • +```js +// Nothing :: Maybe a +const Nothing = {isJust: false}; Any conforming library which implements compact would also have to have an implementation of Maybe. There is no requirement that it have Maybe.prototype.isJust, just that compact can tell the difference between Nothing and Just a.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

paldepind commented 6 years ago

This is really great work @davidchambers 👍 I think adding an abstraction that supports filter is a very worthwhile and practically useful endeavor. I think the arguments for Compactable are pretty solid. So while #274 was already really nice this PR seems even better. Naturally, I think representing the two possible return values from compact with an object is a good choice 😉 It is very natural and easy to work with in JavaScript.

I must admit I had never heard of Compactable before the discussion in #274. I'll definitely be looking into it in more detail. I think a good abstraction that encompases filter has been much needed.

gabejohnson commented 6 years ago

I've been thinking this over, and I think we should just use the sum type we have (Boolean). Introducing types to the spec should probably be done in a separate PR. We can always revisit Compactable if Maybe is added to the spec.

So my vote's for #274.

davidchambers commented 6 years ago

We can always revisit Compactable if Maybe is added to the spec.

Indeed. :)