generic-match: For when first class pattern matches are needed

[ data, library, mit ] [ Propose Tags ]

For when first class pattern matches are needed.


[Skip to Readme]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.2.0.0, 0.2.0.1, 0.2.0.2, 0.3.0.0, 0.3.0.1
Change log CHANGELOG.md
Dependencies base (>=4.12 && <4.15) [details]
License MIT
Copyright 2020 Samuel Schlesinger
Author Samuel Schlesinger
Maintainer sgschlesinger@gmail.com
Category Data
Source repo head: git clone https://github.com/samuelschlesinger/generic-match
Uploaded by sgschlesinger at 2020-08-23T20:10:43Z
Distributions
Downloads 929 total (26 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2020-08-23 [all 1 reports]

Readme for generic-match-0.1.0.0

[back to package description]

generic-match

When I'm writing Haskell code, often I write things like:

x <- doThing >>= either errorHandler pure
y <- doOtherThing >>= maybe (throwIO Shriek) pure

This comes up in more places than error handling, but I think this is a sufficient example. There is a compromise one makes with their API, where they either offer a specific error type, and force you to deconstruct it and fiddle with it on your own, but usually the names are more descriptive. On the other hand, with Either or Maybe, we can use all of the standard functions available for operating on them, such as either and maybe. This package is getting rid of the cost of entry for deconstructing your own types in this same style. Now we can write:

data DatabaseAccess a =
    ConnectionFailure String
  | InvalidRowCount Int
  | Successful a
  deriving Generic
...
x <- doThing >>= match error (error . show) pure

This is the motivating case, but there are many others! For instance, you can also replace your use of either and maybe with the more "Generic" (heh) match.

x <- doThing >>= match errorHandler pure
y <- doOtherThing >>= match (throwIO Shriek) pure