spoon 0.1 → 0.2
raw patch · 2 files changed
+41/−10 lines, 2 filesdep +deepseq
Dependencies added: deepseq
Files
- Control/Spoon.hs +38/−8
- spoon.cabal +3/−2
Control/Spoon.hs view
@@ -1,11 +1,41 @@-module Control.Spoon (spoon) where+{-# LANGUAGE ScopedTypeVariables #-} -import Prelude hiding (catch)-import System.IO.Unsafe (unsafePerformIO)-import Control.Exception (catch, SomeException)+-----------------------------------------------------------------------------+-- |+-- Module : Control.Spoon+-- Copyright : (c) Matt Morrow & Dan Peebles+-- License : see LICENSE+-- +-- Maintainer : pumpkingod@gmail.com+-- Stability : experimental+-- Portability : non-portable (Scoped Type Variables)+--+-- Two functions for catching pureish exceptions in pure values. This library+-- considers pureish to be any error call or undefined, failed pattern matches, +-- arithmetic exceptions, and array bounds exceptions.+--+----------------------------------------------------------------------------- -catchAll :: IO a -> (SomeException -> IO a) -> IO a-catchAll = catch -spoon :: a -> Maybe a-spoon a = unsafePerformIO (catchAll (Just `fmap` (return $! a)) (return . const Nothing))+module Control.Spoon (spoon, teaspoon) 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+ ]++-- | Evaluate a value to normal form and return Nothing if any exceptions are thrown during evaluation. For any error-free value, @spoon = Just@.+spoon :: NFData a => a -> Maybe a+spoon a = unsafePerformIO $ (deepseq a (Just `fmap` return a)) `catches` handlers++-- | Like 'spoon', but only evaluates to WHNF.+teaspoon :: a -> Maybe a+teaspoon a = unsafePerformIO $ (Just `fmap` (return $! a)) `catches` handlers+
spoon.cabal view
@@ -1,5 +1,5 @@ name: spoon-version: 0.1+version: 0.2 license: BSD3 license-file: LICENSE author: Matt Morrow, Dan Peebles@@ -14,7 +14,8 @@ library build-depends: - base >= 4 && < 5+ base >= 4 && < 5,+ deepseq >= 1 exposed-modules: Control.Spoon ghc-options: -Wall