Pārlūkot izejas kodu

Initial version

Marcos Dumay de Medeiros 9 gadi atpakaļ
revīzija
aa8b5ff17c
5 mainītis faili ar 115 papildinājumiem un 0 dzēšanām
  1. 7 0
      .gitignore
  2. 30 0
      LICENSE
  3. 2 0
      Setup.hs
  4. 25 0
      more-monads.cabal
  5. 51 0
      src/Control/Monad/Extras.hs

+ 7 - 0
.gitignore

@@ -0,0 +1,7 @@
+dist/
+.cabal-sandbox/
+cabal.sandbox.config
+*~
+**/*~
+
+

+ 30 - 0
LICENSE

@@ -0,0 +1,30 @@
+Copyright (c) 2016, Marcos Dumay de Medeiros
+
+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 Marcos Dumay de Medeiros nor the names of other
+      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
+OWNER 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.

+ 2 - 0
Setup.hs

@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain

+ 25 - 0
more-monads.cabal

@@ -0,0 +1,25 @@
+-- Initial more-monads.cabal generated by cabal init.  For further 
+-- documentation, see http://haskell.org/cabal/users-guide/
+
+name:                more-monads
+version:             0.1.0.0
+synopsis:            Extra tools for monads.
+-- description:         
+homepage:            https://sealgram.com/git/haskell/more-monads
+license:             BSD3
+license-file:        LICENSE
+author:              Marcos Dumay de Medeiros
+maintainer:          marcos@marcosdumay.com
+-- copyright:           
+category:            Language
+build-type:          Simple
+-- extra-source-files:  
+cabal-version:       >=1.10
+
+library
+  exposed-modules:     Control.Monad.Extras
+  -- other-modules:       
+  -- other-extensions:    
+  build-depends:       base >=4.7 && <4.8
+  hs-source-dirs:      src
+  default-language:    Haskell2010

+ 51 - 0
src/Control/Monad/Extras.hs

@@ -0,0 +1,51 @@
+module Control.Monad.Extras (
+  ifM,
+  unlessM,
+  whenM,
+  intercalateM
+  )where
+
+import Control.Monad
+import Control.Applicative
+
+ifThenElse :: Bool -> a -> a -> a
+ifThenElse i t e = if i then t else e
+
+-- | A version of if with monadic test.
+ifM :: Monad m => m Bool -> m a -> m a -> m a
+ifM i t e = do
+  i' <- i
+  if i' then t else e
+
+-- | A version of unless with monadic test
+unlessM :: Monad m => m Bool -> m () -> m ()
+unlessM p me = do
+  p' <- p
+  unless p' me
+
+-- | A version of when with monadic test
+whenM :: Monad m => m Bool -> m () -> m ()
+whenM p me = do
+  p' <- p
+  when p' me
+
+{- |
+Folds the second list with the function applied to the first,
+intercalating the evaluation. That is:
+
+intercalateM f -> [a00, a10, a20] -> [b1, b2] = do
+  a01 <- f a00 b1
+  a11 <- f a10 b1
+  a21 <- f a20 b1
+  a02 <- f a11 b2
+  a12 <- f a21 b2
+  a22 <- f a31 b2
+  return [a02, a12, a22]
+
+Usefull for consuming lazy sequences.
+-}
+intercalateM :: Monad m => (a -> b -> m a) -> [a] -> [b] -> m [a]
+intercalateM _ aa [] = return aa
+intercalateM f aa (b:bb) = do
+  aa' <- sequence $ f <$> aa <*> [b]
+  intercalateM f aa' bb