caseof: Combinators for casing on constructors

[ bsd3, development, library ] [ Propose Tags ]

Template-Haskell-based combinators that let you select on constructors.


[Skip to Readme]

Modules

[Index]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.0.0, 0.0.1
Dependencies base (>=4.7 && <5), template-haskell [details]
License BSD-3-Clause
Copyright 2018 Chris Done
Author Chris Done
Maintainer chrisdone@gmail.com
Category Development
Home page https://github.com/chrisdone/caseof#readme
Source repo head: git clone https://github.com/chrisdone/caseof
Uploaded by ChrisDone at 2018-03-07T13:07:25Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 1330 total (6 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2018-03-07 [all 1 reports]

Readme for caseof-0.0.0

[back to package description]

caseof

A simple way to query constructors, like cases but slightly more concise.

Aimed at sum types with many constructors:

data Wiggle = Woo Int Char | Wibble Int deriving Show

There is a case predicate:

> $(isCaseOf 'Woo) (Woo 5 'a')
True

There is a Maybe-based matcher:

> $(maybeCaseOf 'Woo) (Woo 1 'a')
Just (1,'a')

There is a combinator which calls your function with n arguments, or passes the whole value to an "else" clause.

> $(caseOf 'Woo) (\x y -> show x ++ show y) (const "") (Wibble 5)
""

This allows them to be nested:

> $(caseOf 'Woo) (\x y -> show x ++ show y) (const "") (Woo 5 'a')
"5'a'"
> $(caseOf 'Woo) (\x y -> show x ++ show y) ($(caseOf 'Wibble) show (const "")) (Woo 5 'a')
"5'a'"

What's the point of caseOf? To more easily dispatch on functions:

handleHuman name age = ...
handleMachine id = ..
handleWithDefault def =
   $(caseOf 'Human) handleHuman .
   $(caseOf 'Machine) handleMachine def

This applies to any kind of "case" that you'd like to refactor into a function.

Use in your project

In your stack.yaml, put:

extra-deps:
- git: https://github.com/chrisdone/caseof.git
  commit: 9a7f6bb