packages feed

cond (empty) → 0.0

raw patch · 4 files changed

+142/−0 lines, 4 filesdep +basesetup-changed

Dependencies added: base

Files

+ LICENSE view
@@ -0,0 +1,25 @@+Copyright (c) 2012, Adam Curtis+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 Adam Curtis 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 THE COPYRIGHT+HOLDER OR CONTRIBUTORS 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.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ cond.cabal view
@@ -0,0 +1,26 @@+Name: cond+Version: 0.0+Synopsis: Basic conditional operators with monadic variants.+Category: Control, Logic, Monad+License: BSD3+License-File: LICENSE+Author: Adam Curtis+Maintainer: acurtis@spsu.edu+Homepage: https://github.com/kallisti-dev/cond+Cabal-Version: >= 1.6+Build-Type: Simple+Description:+  A very simple library implementing various conditional operations, as well+  as some functions for dealing with conditions in monadic code. Feel free+  to send ideas and suggestions for new conditional operators to the+  maintainer.+ +source-repository head+  type: git+  location: git://github.com/kallisti-dev/cond.git ++library+  hs-source-dirs: src+  ghc-options: -Wall+  exposed-modules: Control.Cond+  build-depends: base >= 3 && < 5
+ src/Control/Cond.hs view
@@ -0,0 +1,89 @@+-- |A convenient set of useful conditional operators.+module Control.Cond +       ( -- * Simple conditional operators+         if', (??), bool+         -- * Higher-order conditional+       , select+         -- * Lisp-style conditional operators +       , cond, condPlus+         -- * Lifted conditional and boolean operators+       , ifM, (<||>), (<&&>), condM, condPlusM+       ) where+import Control.Monad ( MonadPlus (mzero) )++infixr 1 ??+infixr 2 <||>+infixr 3 <&&>++-- |A simple conditional function.+if' :: Bool -> a -> a -> a+if' p a b = if p then a else b+{-# INLINE if' #-}++-- |'if'' with the 'Bool' argument at the end (infixr 1).+(??) :: a -> a -> Bool -> a+(??) a b p = if' p a b +{-# INLINE (??) #-}++-- |A catamorphism for the Bool type. This is analogous to foldr, maybe, and +-- either. The first argument is the false case, the second argument is the +-- true case, and the last argument is the predicate value.+bool :: a -> a -> Bool -> a+bool b a p = if' p a b+{-# INLINE bool #-}++-- |Lisp-style conditionals. If no conditions match, then a runtime exception+-- is thrown. Here's a trivial example:+--+-- @+--   signum x = cond [(x > 0     , 1 )+--                   ,(x < 0     , -1)+--                   ,(otherwise , 0 )]+-- @+cond :: [(Bool, a)] -> a+cond [] = error "cond: no matching conditions"+cond ((p,v):ls) = if' p v (cond ls)++-- |Lisp-style conditionals generalized over 'MonadPlus'. If no conditions+-- match, then the result is 'mzero'. This is a safer variant of 'cond'.+condPlus :: MonadPlus m => [(Bool, a)] -> m a+condPlus [] = mzero+condPlus ((p,v):ls) = if' p (return v) (condPlus ls)++-- |Composes a predicate function and 2 functions into a single+-- function. The first function is called when the predicate yields True, the+-- second when the predicate yields False.+select :: (a -> Bool) -> (a -> b) -> (a -> b) -> (a -> b)+select p a b x = if' (p x) (a x) (b x)+{-# INLINE select #-}++-- |'if'' lifted to 'Monad'. Unlike 'liftM3' 'if'', this is  +-- short-circuiting in the monad, such that only the predicate action and one of+-- the remaining argument actions are executed.+ifM :: Monad m => m Bool -> m a -> m a -> m a +ifM p a b = p >>= bool b a+{-# INLINE ifM #-}++-- |Lifted boolean or. Unlike 'liftM2' ('||'), This function is short-circuiting+-- in the monad. Fixity is the same as '||' (infixr 2).+(<||>) :: Monad m => m Bool -> m Bool -> m Bool+(<||>) a b = ifM a (return True) b+{-# INLINE (<||>) #-}++-- |Lifted boolean and. Unlike 'liftM2' ('&&'), this function is +-- short-circuiting in the monad. Fixity is the same as '&&' (infxr 3).+(<&&>) :: Monad m => m Bool -> m Bool -> m Bool+(<&&>) a b = ifM a b (return False)+{-# INLINE (<&&>) #-}++-- |'cond' lifted to 'Monad'. If no conditions match, a runtime exception+-- is thrown.+condM :: Monad m => [(m Bool, m a)] -> m a +condM [] = error "condM: no matching conditions"+condM ((p, v):ls) = ifM p v (condM ls)++-- |'condPlus' lifted to 'Monad'. If no conditions match, then 'mzero'+-- is returned.+condPlusM :: MonadPlus m => [(m Bool, m a)] -> m a+condPlusM [] = mzero+condPlusM ((p, v):ls) = ifM p v (condPlusM ls)