Copyright | (c) Chris Reade 2021 |
---|---|
License | BSD-style |
Maintainer | chrisreade@mac.com |
Stability | experimental |
Safe Haskell | None |
Language | GHC2021 |
Try
Description
Try is a synonym for Either ShowS, and is used for results of partial operations which return either Right something when defined or Left report when there is a problem (where report is a failure report). This is to allow computation to continue in failure cases without necessarily raising an error. This module contains functions associated with Try results.
Synopsis
- type Try a = Either ShowS a
- onFail :: String -> Try a -> Try a
- nothingFail :: Maybe b -> String -> Try b
- failReport :: String -> Try a
- failReports :: [String] -> Try a
- runTry :: Try a -> a
- ifFail :: a -> Try a -> a
- isFail :: Try a -> Bool
- concatFails :: [Try a] -> Try [a]
- ignoreFails :: [Try a] -> [a]
- tryAtLeastOne :: [Try a] -> Try [a]
- atLeastOne :: [Try a] -> [a]
Try - result types with failure reporting (for partial operations).
type Try a = Either ShowS a Source #
Try is a synonym for Either ShowS. Used for results of partial functions which return either Right something when defined or Left r when there is a problem where r is a (prepending) failure report. Note: ShowS = String -> String makes prepending Strings efficient as composition Note: Either ShowS (and hence Try) is a monad, and this is used frequently for combining partial operations.
onFail :: String -> Try a -> Try a Source #
onFail s exp - prepends s at the front of a failure report if exp fails with Left report but does nothing otherwise.
nothingFail :: Maybe b -> String -> Try b Source #
nothingFail a s - Converts a Maybe Result (a) into a Try result by treating Nothing as a failure
(the String s is used for the failure report on failure).
Usually used as infix (exp nothingFail
s)
failReport :: String -> Try a Source #
failReport s - creates a failure (Left), prepending s for the failure report
failReports :: [String] -> Try a Source #
failReports ss - creates a failure (Left), concatenating ss for the failure report
Extract the (Right) result from a Try, raising an error if the Try is Left r. The failure report (from Left r) is converted to a Stirng and passed to error.
ifFail :: a -> Try a -> a Source #
ifFail a tr - extracts the (Right) result from tr but returning a if tr is Left _ .
concatFails :: [Try a] -> Try [a] Source #
Combines a list of Trys into a single Try with failure overriding success. It concatenates all failure reports if there are any and returns a single Left r. Otherwise it produces Right rs where rs is the list of all (successful) results. In particular, concatFails [] = Right [] (so is NOT a fail)
ignoreFails :: [Try a] -> [a] Source #
Combines a list of Trys into a list of the successes, ignoring any failures. In particular, ignoreFails [] = []
tryAtLeastOne :: [Try a] -> Try [a] Source #
tryAtLeastOne rs - returns Right with the list of successful results if there are any, but Left with a fail report otherwise. The error report will include the concatenated reports from multiple failures.
atLeastOne :: [Try a] -> [a] Source #
atLeastOne rs - returns the list of successful results if there are any, but fails with an error otherwise. The error report will include the concatenated reports from multiple failures.