An open API service indexing awesome lists of open source software.

https://github.com/chrispenner/unipatterns

Helpers which allow safe partial pattern matching in lambdas
https://github.com/chrispenner/unipatterns

Last synced: about 2 months ago
JSON representation

Helpers which allow safe partial pattern matching in lambdas

Awesome Lists containing this project

README

        

# unipatterns

[Hackage docs](http://hackage.haskell.org/package/unipatterns)

Thanks to @isovector for the idea

This library provides helpers for using unipatterns safely; what's a unipattern you ask?

Have you ever wanted to match on a really large expression in-line but don't want to bother pulling out a whole case-statement?

Scrap your case statements with unipattern matches!

For example 'maybeMatch' will detect failed pattern matches and will inject the result into 'Maybe'

```haskell
>>> maybeMatch (\[a] -> show a) [1, 2, 3]
Nothing
>>> maybeMatch (\[a] -> show a) [1]
Just "1"
```

Most other operations provide different failure modes; for instance returning the original argument, returning 'empty', or using a provided failure handler.

It turns out this is pretty handy when using scrap-your-boilerplate operations:

```haskell
everywhere (mkT (match (\"hidden" -> "found")))
```

This searches through a generic structure and will map any @"hidden"@ values into @"found"@; and will leave everything else alone.

You can also use this with humble fmap, inside monadic binds, etc.
Anywhere that you really only care about one particular pattern, and want some trivial behaviour for the others.
The following changes all Lefts into Rights, but leaves the Rights alone.

```haskell
>>> match (\(Left n, x) -> (Right (show n), x)) <$> [(Right "a", 1), (Left 10, 2)]
[(Right "a",1),(Right "10",2)]
```

Does this library really need to exist? Probably not, but there are times when it's handy to have.