packages feed

spoon 0.3 → 0.3.1

raw patch · 2 files changed

+48/−24 lines, 2 filesnew-uploader

Files

Control/Spoon.hs view
@@ -17,25 +17,48 @@ -----------------------------------------------------------------------------  -module Control.Spoon (spoon, teaspoon) where+module Control.Spoon+    ( Handles+    , defaultHandles+    , spoon+    , spoonWithHandles+    , teaspoon+    , teaspoonWithHandles+    ) where  import Control.Exception import Control.DeepSeq import System.IO.Unsafe -handlers :: [Handler (Maybe a)]-handlers = [ Handler $ \(_ :: ArithException)   -> return Nothing-           , Handler $ \(_ :: ArrayException)   -> return Nothing-           , Handler $ \(_ :: ErrorCall)        -> return Nothing-           , Handler $ \(_ :: PatternMatchFail) -> return Nothing-           , Handler $ \(x :: SomeException)    -> throwIO x-           ]+type Handles a = [Handler (Maybe a)] +{-# INLINEABLE defaultHandles #-}+defaultHandles :: Handles a+defaultHandles =+    [ Handler $ \(_ :: ArithException)   -> return Nothing+    , Handler $ \(_ :: ArrayException)   -> return Nothing+    , Handler $ \(_ :: ErrorCall)        -> return Nothing+    , Handler $ \(_ :: PatternMatchFail) -> return Nothing+    , Handler $ \(x :: SomeException)    -> throwIO x ]+ -- | Evaluate a value to normal form and return Nothing if any exceptions are thrown during evaluation. For any error-free value, @spoon = Just@.+{-# INLINEABLE spoonWithHandles #-}+spoonWithHandles :: NFData a => Handles a -> a -> Maybe a+spoonWithHandles handles a = unsafePerformIO $+    deepseq a (Just `fmap` return a) `catches` handles++-- | Evaluate a value to normal form and return Nothing if any exceptions are thrown during evaluation. For any error-free value, @spoon = Just@.+{-# INLINE spoon #-} spoon :: NFData a => a -> Maybe a-spoon a = unsafePerformIO $ deepseq a (Just `fmap` return a) `catches` handlers+spoon = spoonWithHandles defaultHandles +{-# INLINEABLE teaspoonWithHandles #-}+teaspoonWithHandles :: Handles a -> a -> Maybe a+teaspoonWithHandles handles a = unsafePerformIO $+    (Just `fmap` evaluate a) `catches` handles+ -- | Like 'spoon', but only evaluates to WHNF.+{-# INLINE teaspoon #-} teaspoon :: a -> Maybe a-teaspoon a = unsafePerformIO $ (Just `fmap` evaluate a) `catches` handlers+teaspoon = teaspoonWithHandles defaultHandles 
spoon.cabal view
@@ -1,21 +1,22 @@-name:		      spoon-version:	    0.3-license:	    BSD3-license-file: LICENSE-author:		    Matt Morrow, Dan Peebles-maintainer:	  Dan Peebles <pumpkingod@gmail.com>-stability:	  experimental-category:	    Error handling-synopsis:	    Catch errors thrown from pure computations.-copyright:    2009 Matt Morrow & Dan Peebles-description:  Takes an error-throwing expression and puts it back in the Maybe it belongs in.-build-type:   Simple-cabal-version:>=1.2+name:           spoon+version:        0.3.1+license:        BSD3+license-file:   LICENSE+author:         Matt Morrow, Dan Peebles, Liyang HU+maintainer:     Liyang HU <spoon@liyang.hu>+stability:      experimental+category:       Error handling+synopsis:       Catch errors thrown from pure computations.+copyright:      2009 Matt Morrow & Dan Peebles, 2013 Liyang HU+description:    Takes an error-throwing expression and puts it back in the Maybe it belongs in.+build-type:     Simple+cabal-version:  >= 1.2  library-  build-depends: +  build-depends:     base >= 4 && < 5,     deepseq >= 1   exposed-modules:     Control.Spoon   ghc-options: -Wall+