safe-0.3.15: Library of safe (exception free) functions

Safe Haskell | Safe
Language | Haskell2010

Safe.Exact

Contents

- New functions
- Safe wrappers

Description

Provides functions that raise errors in corner cases instead of returning "best effort" results, then provides wrappers like the Safe module. For example:

- ``takeExact` 3 [1,2]` raises an error, in contrast to `take` which would return just two elements.
- ``takeExact` (-1) [1,2]` raises an error, in contrast to `take` which would return no elements.
- ``zip` [1,2] [1]` raises an error, in contrast to `zip` which would only pair up the first element.

Note that the `May` variants of these functions are strict in at least the bit of the prefix of the list required to spot errors. The standard and `Note` versions are lazy, but throw errors later in the process - they do not check upfront.

Synopsis

- takeExact :: Partial => Int -> [a] -> [a]
- dropExact :: Partial => Int -> [a] -> [a]
- splitAtExact :: Partial => Int -> [a] -> ([a], [a])
- zipExact :: Partial => [a] -> [b] -> [(a, b)]
- zipWithExact :: Partial => (a -> b -> c) -> [a] -> [b] -> [c]
- zip3Exact :: Partial => [a] -> [b] -> [c] -> [(a, b, c)]
- zipWith3Exact :: Partial => (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
- takeExactMay :: Int -> [a] -> Maybe [a]
- takeExactNote :: Partial => String -> Int -> [a] -> [a]
- takeExactDef :: [a] -> Int -> [a] -> [a]
- dropExactMay :: Int -> [a] -> Maybe [a]
- dropExactNote :: Partial => String -> Int -> [a] -> [a]
- dropExactDef :: [a] -> Int -> [a] -> [a]
- splitAtExactMay :: Int -> [a] -> Maybe ([a], [a])
- splitAtExactNote :: Partial => String -> Int -> [a] -> ([a], [a])
- splitAtExactDef :: ([a], [a]) -> Int -> [a] -> ([a], [a])
- zipExactMay :: [a] -> [b] -> Maybe [(a, b)]
- zipExactNote :: Partial => String -> [a] -> [b] -> [(a, b)]
- zipExactDef :: [(a, b)] -> [a] -> [b] -> [(a, b)]
- zipWithExactMay :: (a -> b -> c) -> [a] -> [b] -> Maybe [c]
- zipWithExactNote :: Partial => String -> (a -> b -> c) -> [a] -> [b] -> [c]
- zipWithExactDef :: [c] -> (a -> b -> c) -> [a] -> [b] -> [c]
- zip3ExactMay :: [a] -> [b] -> [c] -> Maybe [(a, b, c)]
- zip3ExactNote :: Partial => String -> [a] -> [b] -> [c] -> [(a, b, c)]
- zip3ExactDef :: [(a, b, c)] -> [a] -> [b] -> [c] -> [(a, b, c)]
- zipWith3ExactMay :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> Maybe [d]
- zipWith3ExactNote :: Partial => String -> (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
- zipWith3ExactDef :: [d] -> (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]

# New functions

takeExact :: Partial => Int -> [a] -> [a] #

    takeExact n xs =
      | n >= 0 && n <= length xs = take n xs
      | otherwise                = error "some message"

dropExact :: Partial => Int -> [a] -> [a] #

    dropExact n xs =
      | n >= 0 && n <= length xs = drop n xs
      | otherwise                = error "some message"

splitAtExact :: Partial => Int -> [a] -> ([a], [a]) #

    splitAtExact n xs =
      | n >= 0 && n <= length xs = splitAt n xs
      | otherwise                = error "some message"

zipExact :: Partial => [a] -> [b] -> [(a, b)] #

    zipExact xs ys =
      | length xs == length ys = zip xs ys
      | otherwise              = error "some message"

zipWithExact :: Partial => (a -> b -> c) -> [a] -> [b] -> [c] #

    zipWithExact f xs ys =
      | length xs == length ys = zipWith f xs ys
      | otherwise              = error "some message"

zip3Exact :: Partial => [a] -> [b] -> [c] -> [(a, b, c)] #

    zip3Exact xs ys zs =
      | length xs == length ys && length xs == length zs = zip3 xs ys zs
      | otherwise                                        = error "some message"

zipWith3Exact :: Partial => (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d] #

    zipWith3Exact f xs ys zs =
      | length xs == length ys && length xs == length zs = zipWith3 f xs ys zs
      | otherwise                                        = error "some message"

# Safe wrappers

takeExactMay :: Int -> [a] -> Maybe [a] #

takeExactNote :: Partial => String -> Int -> [a] -> [a] #

takeExactDef :: [a] -> Int -> [a] -> [a] #

dropExactMay :: Int -> [a] -> Maybe [a] #

dropExactNote :: Partial => String -> Int -> [a] -> [a] #

dropExactDef :: [a] -> Int -> [a] -> [a] #

splitAtExactMay :: Int -> [a] -> Maybe ([a], [a]) #

splitAtExactNote :: Partial => String -> Int -> [a] -> ([a], [a]) #

splitAtExactDef :: ([a], [a]) -> Int -> [a] -> ([a], [a]) #

zipExactMay :: [a] -> [b] -> Maybe [(a, b)] #

zipExactNote :: Partial => String -> [a] -> [b] -> [(a, b)] #

zipExactDef :: [(a, b)] -> [a] -> [b] -> [(a, b)] #

zipWithExactMay :: (a -> b -> c) -> [a] -> [b] -> Maybe [c] #

zipWithExactNote :: Partial => String -> (a -> b -> c) -> [a] -> [b] -> [c] #

zipWithExactDef :: [c] -> (a -> b -> c) -> [a] -> [b] -> [c] #

zip3ExactMay :: [a] -> [b] -> [c] -> Maybe [(a, b, c)] #

zip3ExactNote :: Partial => String -> [a] -> [b] -> [c] -> [(a, b, c)] #

zip3ExactDef :: [(a, b, c)] -> [a] -> [b] -> [c] -> [(a, b, c)] #

zipWith3ExactMay :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> Maybe [d] #

zipWith3ExactNote :: Partial => String -> (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d] #

zipWith3ExactDef :: [d] -> (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d] #