from-sum (empty) → 0.1.0.0
raw patch · 5 files changed
+197/−0 lines, 5 filesdep +Globdep +basedep +doctestsetup-changed
Dependencies added: Glob, base, doctest
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- from-sum.cabal +35/−0
- src/Control/FromSum.hs +90/−0
- test/DocTest.hs +40/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Dennis Gosnell (c) 2016++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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ from-sum.cabal view
@@ -0,0 +1,35 @@+name: from-sum+version: 0.1.0.0+synopsis: Canonical fromMaybeM and fromEitherM functions.+description: Please see README.md+homepage: https://github.com/cdepillabout/from-sum+license: BSD3+license-file: LICENSE+author: Dennis Gosnell+maintainer: cdep.illabout@gmail.com+copyright: 2016 Dennis Gosnell+category: Control+build-type: Simple+-- extra-source-files:+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: Control.FromSum+ build-depends: base >= 4.6 && < 5+ default-language: Haskell2010+ ghc-options: -Wall++test-suite from-sum-doctest+ type: exitcode-stdio-1.0+ main-is: DocTest.hs+ hs-source-dirs: test+ build-depends: base+ , doctest+ , Glob+ default-language: Haskell2010+ ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N++source-repository head+ type: git+ location: git@github.com:cdepillabout/from-sum.git
+ src/Control/FromSum.hs view
@@ -0,0 +1,90 @@+{-# LANGUAGE CPP #-}++{-|+Module : Control.FromSum+Copyright : (c) Dennis Gosnell, 2016+License : BSD-style (see LICENSE file)+Maintainer : cdep.illabout@gmail.com+Stability : experimental+Portability : POSIX++This Haskell module exports various \"from\" functions for 'Either' and+'Maybe'.+-}+module Control.FromSum+ ( -- * Monadic in return value+ fromEitherM+ , fromMaybeM+ -- * Monadic in both return and sum-type value+ , fromEitherMM+ , fromMaybeMM+ -- * Completely non-monadic functions+ , fromEither+ , fromMaybe+ ) where+#if __GLASGOW_HASKELL__ < 710+-- We don't need this import for GHC 7.10 as it exports all required functions+-- from Prelude+import Control.Applicative+#endif+import Data.Maybe (fromMaybe)++-- | A monadic version of 'fromEither'.+--+-- >>> fromEitherM (\s -> [length s]) $ Right 5+-- [5]+-- >>> fromEitherM (\s -> [length s]) $ Left ("foo" :: String)+-- [3]+fromEitherM+ :: Applicative m+ => (e -> m a) -> Either e a -> m a+fromEitherM leftAction = either leftAction pure++-- | A monadic version of 'fromMaybe'.+--+-- >>> fromMaybeM [] $ Just 5+-- [5]+-- >>> fromMaybeM [] Nothing+-- []+fromMaybeM+ :: Applicative m+ => m a -> Maybe a -> m a+fromMaybeM nothingAction = maybe nothingAction pure++-- | Similar to 'fromEitherM' but 'Either' argument is also a monadic value.+--+-- >>> fromEitherMM (\s -> [length s]) . pure $ Right 5+-- [5]+-- >>> fromEitherMM (\s -> [length s]) . pure $ Left ("foo" :: String)+-- [3]+--+-- *NOTE*: I don't particularly like the name of this function. If you have a+-- suggestion for a better name, please submit a PR or issue.+fromEitherMM+ :: Monad m+ => (e -> m a) -> m (Either e a) -> m a+fromEitherMM eitherAction mEither = fromEitherM eitherAction =<< mEither++-- | Similar to 'fromMaybeMA monadic version of 'fromMaybe'.+--+-- >>> fromMaybeMM [] . pure $ Just 5+-- [5]+-- >>> fromMaybeMM [] $ pure Nothing+-- []++-- *NOTE*: I don't particularly like the name of this function. If you have a+-- suggestion for a better name, please submit a PR or issue.+fromMaybeMM+ :: Monad m+ => m a -> m (Maybe a) -> m a+fromMaybeMM nothingAction mMaybe = fromMaybeM nothingAction =<< mMaybe++-- | Similar to 'fromMaybe'.+--+-- >>> fromEither show $ Left 5+-- "5"+-- >>> fromEither show $ Right "hello"+-- "hello"+fromEither :: (e -> a) -> Either e a -> a+fromEither f (Left e) = f e+fromEither _ (Right a) = a
+ test/DocTest.hs view
@@ -0,0 +1,40 @@++module Main (main) where++import Prelude++import Data.Monoid ((<>))+import System.FilePath.Glob (glob)+import Test.DocTest (doctest)++main :: IO ()+main = glob "src/**/*.hs" >>= doDocTest++doDocTest :: [String] -> IO ()+doDocTest options = doctest $ options <> ghcExtensions++ghcExtensions :: [String]+ghcExtensions =+ [+ -- "-XConstraintKinds"+ -- , "-XDataKinds"+ "-XDeriveDataTypeable"+ , "-XDeriveGeneric"+ -- , "-XEmptyDataDecls"+ , "-XFlexibleContexts"+ -- , "-XFlexibleInstances"+ -- , "-XGADTs"+ -- , "-XGeneralizedNewtypeDeriving"+ -- , "-XInstanceSigs"+ -- , "-XMultiParamTypeClasses"+ -- , "-XNoImplicitPrelude"+ , "-XOverloadedStrings"+ -- , "-XPolyKinds"+ -- , "-XRankNTypes"+ -- , "-XRecordWildCards"+ , "-XScopedTypeVariables"+ -- , "-XStandaloneDeriving"+ -- , "-XTupleSections"+ -- , "-XTypeFamilies"+ -- , "-XTypeOperators"+ ]