Copyright | (c) 2022 Tim Emiola |
---|---|
License | BSD3 |
Maintainer | Tim Emiola <adetokunbo@emio.la> |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Imports and re-export commonly-used functions and data types
Synopsis
- isNull :: Char -> Bool
- isNullOrSpace :: Char -> Bool
- readUtf8Text :: FilePath -> IO Text
- threadDelay :: Int -> IO ()
- handle :: Exception e => (e -> IO a) -> IO a -> IO a
- throwIO :: Exception e => e -> IO a
- unless :: Applicative f => Bool -> f () -> f ()
- filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a]
- when :: Applicative f => Bool -> f () -> f ()
- isSpace :: Char -> Bool
- foldlM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b
- hash :: Hashable a => a -> Int
- foldl' :: Foldable t => (b -> a -> b) -> b -> t a -> b
- data NonEmpty a = a :| [a]
- nonEmpty :: [a] -> Maybe (NonEmpty a)
- mapMaybe :: (a -> Maybe b) -> [a] -> [b]
- isJust :: Maybe a -> Bool
- data Map k a
- data Set a
- data Text
- decodeUtf8 :: ByteString -> Text
- data Natural
- takeBaseName :: FilePath -> String
- doesFileExist :: FilePath -> IO Bool
- getSymbolicLinkTarget :: FilePath -> IO FilePath
- listDirectory :: FilePath -> IO [FilePath]
- isPermissionError :: IOError -> Bool
- isDoesNotExistError :: IOError -> Bool
- stderr :: Handle
- type ProcessID = CPid
- newtype CPid = CPid Int32
- readMaybe :: Read a => String -> Maybe a
- readEither :: Read a => String -> Either String a
functions
isNullOrSpace :: Char -> Bool Source #
True
for the null
char or any space
module re-exports
threadDelay :: Int -> IO () Source #
Suspends the current thread for a given number of microseconds (GHC only).
There is no guarantee that the thread will be rescheduled promptly when the delay has expired, but the thread will never continue to run earlier than specified.
handle :: Exception e => (e -> IO a) -> IO a -> IO a Source #
A version of catch
with the arguments swapped around; useful in
situations where the code for the handler is shorter. For example:
do handle (\NonTermination -> exitWith (ExitFailure 1)) $ ...
throwIO :: Exception e => e -> IO a Source #
A variant of throw
that can only be used within the IO
monad.
Although throwIO
has a type that is an instance of the type of throw
, the
two functions are subtly different:
throw e `seq` x ===> throw e throwIO e `seq` x ===> x
The first example will cause the exception e
to be raised,
whereas the second one won't. In fact, throwIO
will only cause
an exception to be raised when it is used within the IO
monad.
The throwIO
variant should be used in preference to throw
to
raise an exception within the IO
monad because it guarantees
ordering with respect to other IO
operations, whereas throw
does not.
filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a] Source #
This generalizes the list-based filter
function.
when :: Applicative f => Bool -> f () -> f () Source #
Conditional execution of Applicative
expressions. For example,
when debug (putStrLn "Debugging")
will output the string Debugging
if the Boolean value debug
is True
, and otherwise do nothing.
isSpace :: Char -> Bool Source #
Returns True
for any Unicode space character, and the control
characters \t
, \n
, \r
, \f
, \v
.
foldlM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b Source #
Left-to-right monadic fold over the elements of a structure.
Given a structure t
with elements (a, b, ..., w, x, y)
, the result of
a fold with an operator function f
is equivalent to:
foldlM f z t = do aa <- f z a bb <- f aa b ... xx <- f ww x yy <- f xx y return yy -- Just @return z@ when the structure is empty
For a Monad m
, given two functions f1 :: a -> m b
and f2 :: b -> m c
,
their Kleisli composition (f1 >=> f2) :: a -> m c
is defined by:
(f1 >=> f2) a = f1 a >>= f2
Another way of thinking about foldlM
is that it amounts to an application
to z
of a Kleisli composition:
foldlM f z t = flip f a >=> flip f b >=> ... >=> flip f x >=> flip f y $ z
The monadic effects of foldlM
are sequenced from left to right.
If at some step the bind operator (
short-circuits (as with, e.g.,
>>=
)mzero
in a MonadPlus
), the evaluated effects will be from an initial
segment of the element sequence. If you want to evaluate the monadic
effects in right-to-left order, or perhaps be able to short-circuit after
processing a tail of the sequence of elements, you'll need to use foldrM
instead.
If the monadic effects don't short-circuit, the outermost application of
f
is to the rightmost element y
, so that, ignoring effects, the result
looks like a left fold:
((((z `f` a) `f` b) ... `f` w) `f` x) `f` y
Examples
Basic usage:
>>>
let f a e = do { print e ; return $ e : a }
>>>
foldlM f [] [0..3]
0 1 2 3 [3,2,1,0]
hash :: Hashable a => a -> Int Source #
Like hashWithSalt
, but no salt is used. The default
implementation uses hashWithSalt
with some default salt.
Instances might want to implement this method to provide a more
efficient implementation than the default implementation.
foldl' :: Foldable t => (b -> a -> b) -> b -> t a -> b Source #
Left-associative fold of a structure but with strict application of the operator.
This ensures that each step of the fold is forced to Weak Head Normal
Form before being applied, avoiding the collection of thunks that would
otherwise occur. This is often what you want to strictly reduce a
finite structure to a single strict result (e.g. sum
).
For a general Foldable
structure this should be semantically identical
to,
foldl' f z =foldl'
f z .toList
Since: base-4.6.0.0
Non-empty (and non-strict) list type.
Since: base-4.9.0.0
a :| [a] infixr 5 |
Instances
mapMaybe :: (a -> Maybe b) -> [a] -> [b] Source #
The mapMaybe
function is a version of map
which can throw
out elements. In particular, the functional argument returns
something of type
. If this is Maybe
bNothing
, no element
is added on to the result list. If it is
, then Just
bb
is
included in the result list.
Examples
Using
is a shortcut for mapMaybe
f x
in most cases:catMaybes
$ map
f x
>>>
import Text.Read ( readMaybe )
>>>
let readMaybeInt = readMaybe :: String -> Maybe Int
>>>
mapMaybe readMaybeInt ["1", "Foo", "3"]
[1,3]>>>
catMaybes $ map readMaybeInt ["1", "Foo", "3"]
[1,3]
If we map the Just
constructor, the entire list should be returned:
>>>
mapMaybe Just [1,2,3]
[1,2,3]
A Map from keys k
to values a
.
The Semigroup
operation for Map
is union
, which prefers
values from the left operand. If m1
maps a key k
to a value
a1
, and m2
maps the same key to a different value a2
, then
their union m1 <> m2
maps k
to a1
.
Instances
Bifoldable Map | Since: containers-0.6.3.1 |
Eq2 Map | Since: containers-0.5.9 |
Ord2 Map | Since: containers-0.5.9 |
Defined in Data.Map.Internal | |
Show2 Map | Since: containers-0.5.9 |
Hashable2 Map | Since: hashable-1.3.4.0 |
Foldable (Map k) | Folds in order of increasing key. |
Defined in Data.Map.Internal fold :: Monoid m => Map k m -> m Source # foldMap :: Monoid m => (a -> m) -> Map k a -> m Source # foldMap' :: Monoid m => (a -> m) -> Map k a -> m Source # foldr :: (a -> b -> b) -> b -> Map k a -> b Source # foldr' :: (a -> b -> b) -> b -> Map k a -> b Source # foldl :: (b -> a -> b) -> b -> Map k a -> b Source # foldl' :: (b -> a -> b) -> b -> Map k a -> b Source # foldr1 :: (a -> a -> a) -> Map k a -> a Source # foldl1 :: (a -> a -> a) -> Map k a -> a Source # toList :: Map k a -> [a] Source # null :: Map k a -> Bool Source # length :: Map k a -> Int Source # elem :: Eq a => a -> Map k a -> Bool Source # maximum :: Ord a => Map k a -> a Source # minimum :: Ord a => Map k a -> a Source # | |
Eq k => Eq1 (Map k) | Since: containers-0.5.9 |
Ord k => Ord1 (Map k) | Since: containers-0.5.9 |
Defined in Data.Map.Internal | |
(Ord k, Read k) => Read1 (Map k) | Since: containers-0.5.9 |
Defined in Data.Map.Internal liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Map k a) Source # liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [Map k a] Source # liftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (Map k a) Source # liftReadListPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec [Map k a] Source # | |
Show k => Show1 (Map k) | Since: containers-0.5.9 |
Traversable (Map k) | Traverses in order of increasing key. |
Functor (Map k) | |
Hashable k => Hashable1 (Map k) | Since: hashable-1.3.4.0 |
Defined in Data.Hashable.Class | |
(Data k, Data a, Ord k) => Data (Map k a) | |
Defined in Data.Map.Internal gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Map k a -> c (Map k a) Source # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Map k a) Source # toConstr :: Map k a -> Constr Source # dataTypeOf :: Map k a -> DataType Source # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Map k a)) Source # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Map k a)) Source # gmapT :: (forall b. Data b => b -> b) -> Map k a -> Map k a Source # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Map k a -> r Source # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Map k a -> r Source # gmapQ :: (forall d. Data d => d -> u) -> Map k a -> [u] Source # gmapQi :: Int -> (forall d. Data d => d -> u) -> Map k a -> u Source # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Map k a -> m (Map k a) Source # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Map k a -> m (Map k a) Source # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Map k a -> m (Map k a) Source # | |
Ord k => Monoid (Map k v) | |
Ord k => Semigroup (Map k v) | |
Ord k => IsList (Map k v) | Since: containers-0.5.6.2 |
(Ord k, Read k, Read e) => Read (Map k e) | |
(Show k, Show a) => Show (Map k a) | |
(NFData k, NFData a) => NFData (Map k a) | |
Defined in Data.Map.Internal | |
(Eq k, Eq a) => Eq (Map k a) | |
(Ord k, Ord v) => Ord (Map k v) | |
(Hashable k, Hashable v) => Hashable (Map k v) | Since: hashable-1.3.4.0 |
type Item (Map k v) | |
Defined in Data.Map.Internal |
A set of values a
.
Instances
Foldable Set | Folds in order of increasing key. |
Defined in Data.Set.Internal fold :: Monoid m => Set m -> m Source # foldMap :: Monoid m => (a -> m) -> Set a -> m Source # foldMap' :: Monoid m => (a -> m) -> Set a -> m Source # foldr :: (a -> b -> b) -> b -> Set a -> b Source # foldr' :: (a -> b -> b) -> b -> Set a -> b Source # foldl :: (b -> a -> b) -> b -> Set a -> b Source # foldl' :: (b -> a -> b) -> b -> Set a -> b Source # foldr1 :: (a -> a -> a) -> Set a -> a Source # foldl1 :: (a -> a -> a) -> Set a -> a Source # toList :: Set a -> [a] Source # null :: Set a -> Bool Source # length :: Set a -> Int Source # elem :: Eq a => a -> Set a -> Bool Source # maximum :: Ord a => Set a -> a Source # minimum :: Ord a => Set a -> a Source # | |
Eq1 Set | Since: containers-0.5.9 |
Ord1 Set | Since: containers-0.5.9 |
Defined in Data.Set.Internal | |
Show1 Set | Since: containers-0.5.9 |
Hashable1 Set | Since: hashable-1.3.4.0 |
Defined in Data.Hashable.Class | |
(Data a, Ord a) => Data (Set a) | |
Defined in Data.Set.Internal gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Set a -> c (Set a) Source # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Set a) Source # toConstr :: Set a -> Constr Source # dataTypeOf :: Set a -> DataType Source # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Set a)) Source # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Set a)) Source # gmapT :: (forall b. Data b => b -> b) -> Set a -> Set a Source # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Set a -> r Source # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Set a -> r Source # gmapQ :: (forall d. Data d => d -> u) -> Set a -> [u] Source # gmapQi :: Int -> (forall d. Data d => d -> u) -> Set a -> u Source # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Set a -> m (Set a) Source # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Set a -> m (Set a) Source # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Set a -> m (Set a) Source # | |
Ord a => Monoid (Set a) | |
Ord a => Semigroup (Set a) | Since: containers-0.5.7 |
Ord a => IsList (Set a) | Since: containers-0.5.6.2 |
(Read a, Ord a) => Read (Set a) | |
Show a => Show (Set a) | |
NFData a => NFData (Set a) | |
Defined in Data.Set.Internal | |
Eq a => Eq (Set a) | |
Ord a => Ord (Set a) | |
Defined in Data.Set.Internal | |
Hashable v => Hashable (Set v) | Since: hashable-1.3.4.0 |
type Item (Set a) | |
Defined in Data.Set.Internal |
A space efficient, packed, unboxed Unicode text type.
decodeUtf8 :: ByteString -> Text Source #
Decode a ByteString
containing UTF-8 encoded text that is known
to be valid.
If the input contains any invalid UTF-8 data, an exception will be
thrown that cannot be caught in pure code. For more control over
the handling of invalid data, use decodeUtf8'
or
decodeUtf8With
.
Natural number
Invariant: numbers <= 0xffffffffffffffff use the NS
constructor
Instances
takeBaseName :: FilePath -> String Source #
Get the base name, without an extension or path.
takeBaseName "/directory/file.ext" == "file" takeBaseName "file/test.txt" == "test" takeBaseName "dave.ext" == "dave" takeBaseName "" == "" takeBaseName "test" == "test" takeBaseName (addTrailingPathSeparator x) == "" takeBaseName "file/file.tar.gz" == "file.tar"
doesFileExist :: FilePath -> IO Bool #
getSymbolicLinkTarget :: FilePath -> IO FilePath #
listDirectory :: FilePath -> IO [FilePath] #
isPermissionError :: IOError -> Bool Source #
An error indicating that an IO
operation failed because
the user does not have sufficient operating system privilege
to perform that operation.
isDoesNotExistError :: IOError -> Bool Source #
An error indicating that an IO
operation failed because
one of its arguments does not exist.
Instances
readMaybe :: Read a => String -> Maybe a Source #
Parse a string using the Read
instance.
Succeeds if there is exactly one valid result.
>>>
readMaybe "123" :: Maybe Int
Just 123
>>>
readMaybe "hello" :: Maybe Int
Nothing
Since: base-4.6.0.0