packages feed

alternators (empty) → 0.1.1.0

raw patch · 7 files changed

+171/−0 lines, 7 filesdep +basedep +mmorphdep +transformerssetup-changed

Dependencies added: base, mmorph, transformers

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2017++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 Author name here 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.
+ README.md view
@@ -0,0 +1,4 @@+[![Hackage](https://img.shields.io/hackage/v/alternators.svg)](https://hackage.haskell.org/package/alternators)+[![Build Status](https://secure.travis-ci.org/louispan/alternators.png?branch=master)](http://travis-ci.org/louispan/alternators)++Handy functions when using transformers
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ alternators.cabal view
@@ -0,0 +1,30 @@+name:                alternators+version:             0.1.1.0+synopsis:            Handy functions when using transformers+description:         Please see README.md+homepage:            https://github.com/louispan/alternators#readme+license:             BSD3+license-file:        LICENSE+author:              Louis Pan+maintainer:          louis@pan.me+copyright:           2017 Louis Pan+category:            Control+build-type:          Simple+extra-source-files:  README.md+cabal-version:       >=1.10+tested-with:         GHC == 7.10.3, GHC == 8.0.1++library+  hs-source-dirs:      src+  exposed-modules:     Control.Monad.Trans.Reader.Extras+                       Control.Monad.Trans.State.Lazy.Extras+                       Control.Monad.Trans.State.Strict.Extras+  build-depends:       base >= 4.7 && < 5+                     , mmorph >= 1 && < 2+                     , transformers >= 0.4 && < 0.6+  ghc-options:         -Wall+  default-language:    Haskell2010++source-repository head+  type:     git+  location: https://github.com/louispan/alternators
+ src/Control/Monad/Trans/Reader/Extras.hs view
@@ -0,0 +1,41 @@+module Control.Monad.Trans.Reader.Extras where++import Control.Monad.Trans.Reader+import Control.Monad.Morph+import Data.Functor.Identity++-- | This function combines two different monadic effects,+-- where one of the effects is a reader effect and the other is a+-- producing effect.+--+-- This version results in the reader monad's inner effect to be wrapped+-- around the producing effect.+-- This requires the Reader inner effect to be an MFunctor on Identity.+--+-- This can enable past-Dependence.+-- Elm has foldp : (a -> state -> state) -> state -> Signal a -> Signal state+-- This is equivalent to a creating a @StateT state (Signal m) ()@+--+-- 'runReaderM' is a more general form of @StateT state (Signal m) ()@ where+-- given a reader monad to transform "a" to "c" with effects, and an "as"+-- monad that produces "a"s with other effects, run the result of "as" through+-- the reader monad to produce "c"s with both effects.+-- @+-- runReaderM :: Monad m => Reader a (State s) c -> m a                      -> StateT s m c+-- runReaderM ::            Reader a (State s) c -> Signal STM a             -> StateT state (Signal STM) c+-- runReaderM ::            Reader a (State s) c -> Pipes.Concurrent.Input a -> StateT state Pipes.Concurrent.Input c+-- @+runReaderM :: (Monad m, Monad (t m), MonadTrans t, MFunctor t)+  => ReaderT a (t Identity) c -> m a -> t m c+runReaderM c as = do+  a <- lift as+  hoist generalize $ runReaderT c a++-- | An alternate form of runReaderM where the producing effect is+-- wrapped around the reader monad's inner effect.+-- This requires the producing effect to be an MFunctor on Identity.+runReaderM' :: (Monad m, Monad (t m), MonadTrans t, MFunctor t)+  => ReaderT a m c -> (t Identity) a -> t m c+runReaderM' c as = do+  a <- hoist generalize as+  lift $ runReaderT c a
+ src/Control/Monad/Trans/State/Lazy/Extras.hs view
@@ -0,0 +1,32 @@+module Control.Monad.Trans.State.Lazy.Extras where++import Control.Monad.Trans.Class+import Control.Monad.Trans.Except+import Control.Monad.Trans.Maybe+import Control.Monad.Trans.State.Lazy++-- | The problem with @StateT s (MaybeT m) a@ is that on failure, the original state is lost.+-- A more useful type is @MaybeT (StateT s m) a@ which at least keeps the original input state+-- on failure.+maybeState :: Monad m => StateT s (MaybeT m) a -> MaybeT (StateT s m) a+maybeState sm = MaybeT $ do+    s <- get+    r <- lift $ runMaybeT $ runStateT sm s+    case r of+        Nothing -> pure Nothing+        Just (a, s') -> do+            put s'+            pure (Just a)++-- | The problem with @StateT s (ExceptT e m) a@ is that on failure, the original state is lost.+-- A more useful type is @ExceptT e (StateT s m) a@ which at least keeps the original input state+-- on failure.+exceptState :: Monad m => StateT s (ExceptT e m) a -> ExceptT e (StateT s m) a+exceptState sm = ExceptT $ do+    s <- get+    r <- lift $ runExceptT $ runStateT sm s+    case r of+        Left e -> pure $ Left e+        Right (a, s') -> do+            put s'+            pure (Right a)
+ src/Control/Monad/Trans/State/Strict/Extras.hs view
@@ -0,0 +1,32 @@+module Control.Monad.Trans.State.Strict.Extras where++import Control.Monad.Trans.Class+import Control.Monad.Trans.Except+import Control.Monad.Trans.Maybe+import Control.Monad.Trans.State.Strict++-- | The problem with @StateT s (MaybeT m) a@ is that on failure, the original state is lost.+-- A more useful type is @MaybeT (StateT s m) a@ which at least keeps the original input state+-- on failure.+maybeState :: Monad m => StateT s (MaybeT m) a -> MaybeT (StateT s m) a+maybeState sm = MaybeT $ do+    s <- get+    r <- lift $ runMaybeT $ runStateT sm s+    case r of+        Nothing -> pure Nothing+        Just (a, s') -> do+            put s'+            pure (Just a)++-- | The problem with @StateT s (ExceptT e m) a@ is that on failure, the original state is lost.+-- A more useful type is @ExceptT e (StateT s m) a@ which at least keeps the original input state+-- on failure.+exceptState :: Monad m => StateT s (ExceptT e m) a -> ExceptT e (StateT s m) a+exceptState sm = ExceptT $ do+    s <- get+    r <- lift $ runExceptT $ runStateT sm s+    case r of+        Left e -> pure $ Left e+        Right (a, s') -> do+            put s'+            pure (Right a)