Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kdungs/python-mcheck
Check monad for Python.
https://github.com/kdungs/python-mcheck
experiment
Last synced: 8 days ago
JSON representation
Check monad for Python.
- Host: GitHub
- URL: https://github.com/kdungs/python-mcheck
- Owner: kdungs
- License: mit
- Created: 2016-02-19T14:57:06.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-02-19T15:50:35.000Z (almost 9 years ago)
- Last Synced: 2023-03-24T18:56:31.179Z (over 1 year ago)
- Topics: experiment
- Language: Python
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mcheck: check monad for Python
_In Haskell terms: `Either String a`_
Allows you to build pipelines of checks on values. For example
```python
import mcheck as cpipeline = c.compose_many(
c.lift(lambda x: x > 0, 'Value has to be positive.'),
c.lift(lambda x: x < 100, 'Value has to be smaller than 100.'),
c.lift(lambda x: not x & 1, 'Value has to be even.'),
c.lift(lambda x: not x & (x - 1), 'Value has to be a power of two.')
)result = pipeline(34)
print(result.message)
```will result in the output `Value has to be a power of two.`.
## Definition
In terms of (pseudo-)Haskell, we could define `Check` as follows:
```haskell
data Check a = Pass a | Fail Stringinstance Monad Check where
return x = Pass x -- is mcheck.return_Fail msg >>= f = Fail msg -- is mcheck.bind
Pass x >>= f = f x
fail x = Fail x -- is not implemented
```we also have Kleisli composition `>=>` which is defined as
```haskell
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
```and implemented through `mcheck.compose` and the convenience function
`mcheck.compose_many` which takes an arbitrary number of functions (`a -> m b`)
and folds that list using Kleisli composition.And then there is `mcheck.lift` which is technically not `liftM` (or `fmap`)
which is defined as```haskell
liftM :: Monad m => (a -> b) -> m a -> m b
```while `mcheck.lift` is defined as
```haskell
lift :: (a -> bool) -> String -> (a -> Check a)
```so maybe `lift` is not such a good name after all…