packages feed

Enum (empty) → 0.1.0.0

raw patch · 5 files changed

+135/−0 lines, 5 filesdep +basesetup-changed

Dependencies added: base

Files

+ Enum.cabal view
@@ -0,0 +1,38 @@+name:                Enum+version:             0.1.0.0+synopsis:            Non-crashing `Enum` operations+-- description:+license:             BSD3+license-file:        LICENSE+author:              M Farkas-Dyck+maintainer:          strake888@gmail.com+copyright:           2018 M Farkas-Dyck+-- category:            +build-type:          Simple+extra-source-files:  README.md+cabal-version:       >=1.10++library+  hs-source-dirs:      .+  exposed-modules:     Util.Enum+  build-depends:       base >= 4.7 && < 5+  default-language:    Haskell2010+  default-extensions:  UnicodeSyntax+                     , LambdaCase+                     , EmptyCase+                     , PartialTypeSignatures+                     , ScopedTypeVariables+                     , FlexibleContexts+                     , FlexibleInstances+                     , DeriveFunctor, DeriveFoldable+  ghc-options:         -Wall -Wcompat -Wredundant-constraints -Wno-name-shadowing+                       -Wincomplete-record-updates -Wincomplete-uni-patterns+                       -Werror=incomplete-patterns+                       -Werror=incomplete-uni-patterns+                       -Werror=incomplete-record-updates+                       -Werror=missing-fields+                       -Werror=missing-methods++source-repository head+  type:     git+  location: https://github.com/strake/Enum.hs
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright M Farkas-Dyck © 2018++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 M Farkas-Dyck 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,1 @@+# Enum
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ Util/Enum.hs view
@@ -0,0 +1,64 @@+module Util.Enum (toEnumMay, fromEnum, predMay, succMay) where++import Prelude hiding (filter, fromEnum, head, tail)+import qualified Prelude+import Control.Applicative+import Control.Monad+import Control.Exception+import Data.Bool (bool)+import Data.Maybe (fromMaybe)+import Data.Semigroup (Semigroup (..), stimes)+import System.IO.Unsafe++toEnumMay :: Enum a => Integer -> Maybe a+toEnumMay n = opMay (toEnum . fromIntegral) n <|> toEnumSafe n++toEnumSafe :: Enum a => Integer -> Maybe a+toEnumSafe n = stimes (abs n) (EndoM $ bool succMay predMay $ n < 0) `endoM` toEnum 0++fromEnum :: Enum a => a -> Integer+fromEnum = fromMaybe <$> fromEnumSafe <*> opMay (fromIntegral . Prelude.fromEnum)++fromEnumSafe :: Enum a => a -> Integer+fromEnumSafe a = head $ f `mapMaybe` let go n = n :. negate n :. go (n+1) in 0 :. go 1+  where f n = n <$ (guard . (== 0) =<< opMay Prelude.fromEnum =<<+                    stimes (abs n) (EndoM $ bool succMay predMay $ n < 0) `endoM` a)++predMay, succMay :: Enum a => a -> Maybe a+predMay = opMay pred+succMay = opMay succ++opMay :: (a -> b) -> a -> Maybe b+opMay f a = unsafePerformIO $ (Just <$> evaluate (f a)) `catches` handlers Nothing++handlers :: a -> [Handler a]+handlers a = [Handler $ \ (_ :: ArithException) -> pure a,+              Handler $ \ (_ :: ArrayException) -> pure a,+              Handler $ \ (_ :: AssertionFailed) -> pure a,+              Handler $ \ (_ :: NonTermination) -> pure a,+              Handler $ \ (_ :: NoMethodError) -> pure a,+              Handler $ \ (_ :: PatternMatchFail) -> pure a,+              Handler $ \ (_ :: RecConError) -> pure a,+              Handler $ \ (_ :: RecSelError) -> pure a,+              Handler $ \ (_ :: RecUpdError) -> pure a,+              Handler $ \ (_ :: ErrorCall) -> pure a]++newtype EndoM m a = EndoM { endoM :: a -> m a }+instance Monad m => Semigroup (EndoM m a) where EndoM f <> EndoM g = EndoM (f >=> g)+instance Monad m => Monoid (EndoM m a) where+    mappend = (<>)+    mempty = EndoM pure++infixr 5 :.+data Stream a = (:.) { head :: a, tail :: Stream a }+  deriving (Functor, Foldable)++instance Applicative Stream where+    pure a = a :. pure a+    f :. fs <*> x :. xs = f x :. (fs <*> xs)++instance Monad Stream where+    xs >>= f = join (f <$> xs) where join (a :. as) = head a :. join (tail <$> as)++mapMaybe :: (a -> Maybe b) -> Stream a -> Stream b+mapMaybe f (a :. as) = maybe id (:.) (f a) $ mapMaybe f as