packages feed

accuerr (empty) → 0.2.0.0

raw patch · 5 files changed

+201/−0 lines, 5 filesdep +basedep +bifunctorsdep +lenssetup-changed

Dependencies added: base, bifunctors, lens

Files

+ LICENSE view
@@ -0,0 +1,31 @@+Copyright (c) 2016 Omari Norman.+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 Omari Norman nor the names of contributors+    to this software 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.
+ README.md view
@@ -0,0 +1,13 @@+accuerr - Data type like "Either" but with an accumulating error+type++This package is based on work in the "validation" package, which is+available at++https://hackage.haskell.org/package/validation++For more information please see the documentation for the+"Accuerr" module.++This package is licensed under the BSD license; please see the+LICENSE file.
+ Setup.hs view
@@ -0,0 +1,3 @@+module Main where+import Distribution.Simple+main = defaultMain
+ accuerr.cabal view
@@ -0,0 +1,45 @@+-- This Cabal file generated using the Cartel library.+-- Cartel is available at:+-- http://www.github.com/massysett/cartel+--+-- Script name used to generate: genCabal+-- Generated on: 2016-06-11 16:12:57.38586 EDT+-- Cartel library version: 0.16.0.0++name: accuerr+version: 0.2.0.0+cabal-version: >= 1.10+license: BSD3+license-file: LICENSE+build-type: Simple+copyright: 2016 Omari Norman+author: Omari Norman+maintainer: omari@smileystation.com+stability: Experimental+homepage: http://www.github.com/massysett/accuerr+bug-reports: http://www.github.com/massysett/accuerr/issues+synopsis: Data type like Either but with accumulating error type+description:+  Please see the "Accuerr" Haddock documentation for more information.+category: Development+extra-source-files:+  README.md++Library+  exposed-modules:+    Accuerr+  build-depends:+      base >= 4.9.0.0 && < 5+    , lens >= 4.14+    , bifunctors >= 5.3+  ghc-options:+    -W+  default-language: Haskell2010+  hs-source-dirs:+    lib+  other-extensions:+    TemplateHaskell++source-repository head+  type: git+  location: https://github.com/massysett/accuerr.git
+ lib/Accuerr.hs view
@@ -0,0 +1,109 @@+{-# LANGUAGE TemplateHaskell #-}+{-# LANGUAGE DeriveFunctor, DeriveFoldable, DeriveTraversable #-}++-- | Provides 'Accuerr', a type similar to 'Either' but where the+-- @Applicative@ instance accumulates error values.  This module is+-- based on work in the @validation@ package, which is available at+--+-- <https://hackage.haskell.org/package/validation>+--+-- The main differences between the @validation@ package and this one:+--+-- * @validation@ has more data types, many of which overlap with+-- those available in other packages.  This package sticks with the+-- 'Accuerr' type only.+--+-- * This package works with GHC 8; as of 2016-06-11, @validation@ does not.+--+-- * 'Accuerr' has fewer typeclass instances than its corresponding type+-- in @validation@.++module Accuerr where++import Data.Bifunctor (Bifunctor(bimap))+import Data.Bifoldable (Bifoldable(bifoldMap))+import Data.Bitraversable (Bitraversable(bitraverse))+import Data.Semigroup (Semigroup((<>)))+import qualified Control.Lens as Lens++-- | A type similar to 'Either' but the 'Applicative' instance+-- accumulates error values.  Unlike 'Either', there is no 'Monad'+-- instance, because there is no '>>=' such that 'Control.Monad.ap'+-- equals '<*>'.+--+-- For the 'Applicative' instance to work, your error type must be a+-- 'Semigroup', such as a list or a 'Data.List.NonEmpty.NonEmpty'.+--+-- ==== __Examples__+--+-- >>> import Text.Read+-- >>> :{+--        let readInt x = case readMaybe x of+--              Nothing -> AccFailure [x]+--              Just a -> AccSuccess a+--                where _types = a :: Int+--     :}+--+-- >>> (+) <$> readInt "3" <*> readInt "4"+-- AccSuccess 7+-- >>> (+) <$> readInt "x3" <*> readInt "4"+-- AccFailure ["x3"]+-- >>> (+) <$> readInt "x3" <*> readInt "x4"+-- AccFailure ["x3","x4"]+-- >>> (,,) <$> readInt "3" <*> readInt "4" <*> readInt "x5"+-- AccFailure ["x5"]+-- >>> sequenceA [AccSuccess 3, AccSuccess 4]+-- AccSuccess [3,4]+-- >>> sequenceA [AccSuccess 3, AccSuccess 4, AccFailure ['c'], AccFailure ['a']]+-- AccFailure "ca"+data Accuerr e a+  = AccFailure e+  | AccSuccess a+  deriving (Eq, Ord, Show, Functor, Foldable, Traversable)++Lens.makePrisms ''Accuerr++instance Semigroup e => Applicative (Accuerr e) where+  pure = AccSuccess+  AccFailure e1 <*> AccFailure e2 = AccFailure (e1 <> e2)+  AccFailure e1 <*> AccSuccess _ = AccFailure e1+  AccSuccess _ <*> AccFailure e2 = AccFailure e2+  AccSuccess f <*> AccSuccess a = AccSuccess (f a)++instance Bifunctor Accuerr where+  bimap fab fcd x = case x of+    AccFailure a -> AccFailure (fab a)+    AccSuccess c -> AccSuccess (fcd c)++instance Bifoldable Accuerr where+  bifoldMap f _ (AccFailure a) = f a+  bifoldMap _ g (AccSuccess b) = g b++instance Bitraversable Accuerr where+  bitraverse f _ (AccFailure x) = AccFailure <$> f x+  bitraverse _ g (AccSuccess y) = AccSuccess <$> g y++-- | Case analysis for the 'Accuerr' type.  Like 'either'.+accuerr :: (a -> c) -> (b -> c) -> Accuerr a b -> c+accuerr f _ (AccFailure a) = f a+accuerr _ f (AccSuccess a) = f a++accuerrToEither :: Accuerr e a -> Either e a+accuerrToEither (AccFailure e) = Left e+accuerrToEither (AccSuccess a) = Right a++eitherToAccuerr :: Either e a -> Accuerr e a+eitherToAccuerr (Left e) = AccFailure e+eitherToAccuerr (Right a) = AccSuccess a++isoAccuerrEither :: Lens.Iso' (Accuerr e a) (Either e a)+isoAccuerrEither = Lens.iso accuerrToEither eitherToAccuerr++isoEitherAccuerr :: Lens.Iso' (Either e a) (Accuerr e a)+isoEitherAccuerr = Lens.iso eitherToAccuerr accuerrToEither++instance Lens.Swapped Accuerr where+  swapped = Lens.iso to to+    where+      to (AccFailure e) = AccSuccess e+      to (AccSuccess a) = AccFailure a