monadplus (empty) → 1.0
raw patch · 4 files changed
+134/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- COPYING +26/−0
- Setup.lhs +4/−0
- monadplus.cabal +30/−0
- src/Control/Monad/Plus.hs +74/−0
+ COPYING view
@@ -0,0 +1,26 @@++Copyright (c) 2013, Hans Höglund+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:+ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.+ * Redistributions in binary form must reproduce the above copyright+ notice, this list of conditions and the following disclaimer in the+ documentation and/or other materials provided with the distribution.+ * Neither the name of the <organization> nor the+ names of its contributors may be used to endorse or promote products+ derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.+
+ Setup.lhs view
@@ -0,0 +1,4 @@+#! /usr/bin/env runhaskell++> import Distribution.Simple+> main = defaultMain
+ monadplus.cabal view
@@ -0,0 +1,30 @@++name: monadplus+version: 1.0+cabal-version: >= 1.6+author: Hans Hoglund+maintainer: Hans Hoglund <hans@hanshoglund.se>+license: BSD3+license-file: COPYING+synopsis: To be written.+category: Music+tested-with: GHC+build-type: Simple++description: + Filtering and folding over arbitrary `MonadPlus` instances.++ This package generalizes many common stream operations such as+ `filter`, `catMaybes` etc.++source-repository head+ type: git+ location: git://github.com/hanshoglund/monadplus.git+ +library + build-depends: + base >= 4 && < 5+ hs-source-dirs: src+ exposed-modules:+ Control.Monad.Plus+
+ src/Control/Monad/Plus.hs view
@@ -0,0 +1,74 @@++module Control.Monad.Plus (+ MonadPlus(..),+ msum,+ mfilter,+ mremove,+ mpartition,+ mfromMaybe,+ mcatMaybes,+ mmapMaybe,+ ) where++import Control.Monad++-- | +-- Generalizes the 'listToMaybe' function.+-- +mfromList :: MonadPlus m => [a] -> m a+mfromList = msum . map return++-- | +-- Generalizes the 'remove' function.+-- +mremove :: MonadPlus m => (a -> Bool) -> m a -> m a+mremove p = mfilter (not . p)++-- | +-- Generalizes the 'partition' function.+-- +mpartition :: MonadPlus m => (a -> Bool) -> m a -> (m a, m a)+mpartition p a = (mfilter p a, mremove p a)++-- | +-- Pass through @Just@ occurrences.+-- Generalizes the 'catMaybes' function.+-- +mcatMaybes :: MonadPlus m => m (Maybe a) -> m a+mcatMaybes = (>>= maybe mzero return)++{-+ mcatMaybes a = + a >>= maybe mzero return+-}++-- | +-- Translate maybe to an arbitrary 'MonadPlus' type.+-- Generalizes the 'maybeToList' function.+-- +mfromMaybe :: MonadPlus m => Maybe a -> m a+mfromMaybe = maybe mzero return++-- | +-- Modify or discard a value.+-- Generalizes the 'mapMaybe' function.+-- +mmapMaybe :: MonadPlus m => (a -> Maybe b) -> m a -> m b+mmapMaybe f = mcatMaybes . liftM f++mlefts :: MonadPlus m => m (Either a b) -> m a+mlefts = mcatMaybes . liftM l+ where+ l (Left a) = Just a+ l (Right a) = Nothing++mrights :: MonadPlus m => m (Either a b) -> m b+mrights = mcatMaybes . liftM r+ where+ r (Left a) = Nothing+ r (Right a) = Just a++mpartitionEithers :: MonadPlus m => m (Either a b) -> (m a, m b)+mpartitionEithers a = (mlefts a, mrights a)++