safe 0.3.8 → 0.3.9
raw patch · 6 files changed
+45/−5 lines, 6 files
Files
- CHANGES.txt +2/−0
- LICENSE +1/−1
- README.md +15/−0
- Safe.hs +21/−0
- Safe/Foldable.hs +1/−0
- safe.cabal +5/−4
CHANGES.txt view
@@ -1,5 +1,7 @@ Changelog for Safe +0.3.9+ #9, add Safe toEnum 0.3.8 #8, remove unnecessary Ord constraints from Foldable functions 0.3.7
LICENSE view
@@ -1,4 +1,4 @@-Copyright Neil Mitchell 2007-2014.+Copyright Neil Mitchell 2007-2015. All rights reserved. Redistribution and use in source and binary forms, with or without
+ README.md view
@@ -0,0 +1,15 @@+# Safe [](https://hackage.haskell.org/package/safe) [](https://travis-ci.org/ndmitchell/safe)++A library wrapping `Prelude`/`Data.List` functions that can throw exceptions, such as `head` and `!!`. Each unsafe function has up to four variants, e.g. with `tail`:++* <tt>tail :: [a] -> [a]</tt>, raises an error on `tail []`.+* <tt>tailMay :: [a] -> <i>Maybe</i> [a]</tt>, turns errors into `Nothing`.+* <tt>tailDef :: <i>[a]</i> -> [a] -> [a]</tt>, takes a default to return on errors.+* <tt>tailNote :: <i>String</i> -> [a] -> [a]</tt>, takes an extra argument which supplements the error message.+* <tt>tailSafe :: [a] -> [a]</tt>, returns some sensible default if possible, `[]` in the case of `tail`.++This package is divided into three modules:++* `Safe` contains safe variants of `Prelude` and `Data.List` functions.+* `Safe.Foldable` contains safe variants of `Foldable` functions.+* `Safe.Exact` creates crashing versions of functions like `zip` (errors if the lists are not equal) and `take` (errors if there are not enough elements), then wraps them to provide safe variants.
Safe.hs view
@@ -40,6 +40,7 @@ findJustDef, findJustNote, elemIndexJustDef, elemIndexJustNote, findIndexJustDef, findIndexJustNote,+ toEnumMay, toEnumDef, toEnumNote, toEnumSafe ) where import Safe.Util@@ -267,3 +268,23 @@ findIndexJustNote :: String -> (a -> Bool) -> [a] -> Int findIndexJustNote note = fromNote note "findIndexJustNote, no matching value" .^ findIndex++-- From http://stackoverflow.com/questions/2743858/safe-and-polymorphic-toenum+-- answer by C. A. McCann+toEnumMay :: (Enum a, Bounded a) => Int -> Maybe a+toEnumMay i =+ let r = toEnum i+ max = maxBound `asTypeOf` r+ min = minBound `asTypeOf` r+ in if i >= fromEnum min && i <= fromEnum max+ then Just r+ else Nothing++toEnumDef :: (Enum a, Bounded a) => a -> Int -> a+toEnumDef def = fromMaybe def . toEnumMay++toEnumNote :: (Enum a, Bounded a) => String -> Int -> a+toEnumNote note = fromNote note "toEnumNote, out of range" . toEnumMay++toEnumSafe :: (Enum a, Bounded a) => Int -> a+toEnumSafe = toEnumDef minBound
Safe/Foldable.hs view
@@ -1,3 +1,4 @@+{-# OPTIONS_GHC -fno-warn-unused-imports #-} -- Monoid required < 7.9 {- | 'Foldable' functions, with wrappers like the "Safe" module. -}
safe.cabal view
@@ -1,17 +1,17 @@ cabal-version: >= 1.6 build-type: Simple name: safe-version: 0.3.8+version: 0.3.9 license: BSD3 license-file: LICENSE category: Unclassified author: Neil Mitchell <ndmitchell@gmail.com> maintainer: Neil Mitchell <ndmitchell@gmail.com>-copyright: Neil Mitchell 2007-2014-homepage: http://community.haskell.org/~ndm/safe/+copyright: Neil Mitchell 2007-2015+homepage: https://github.com/ndmitchell/safe#readme synopsis: Library of safe (exception free) functions bug-reports: https://github.com/ndmitchell/safe/issues-tested-with: GHC==7.8.2, GHC==7.6.3, GHC==7.4.2, GHC==7.2.2+tested-with: GHC==7.10.1, GHC==7.8.4, GHC==7.6.3, GHC==7.4.2, GHC==7.2.2 description: A library wrapping @Prelude@/@Data.List@ functions that can throw exceptions, such as @head@ and @!!@. Each unsafe function has up to four variants, e.g. with @tail@:@@ -35,6 +35,7 @@ * "Safe.Exact" creates crashing versions of functions like @zip@ (errors if the lists are not equal) and @take@ (errors if there are not enough elements), then wraps them to provide safe variants. extra-source-files: CHANGES.txt+ README.md source-repository head type: git