Copyright | (c) Lars Brünjes, 2016 |
---|---|
License | MIT |
Maintainer | [email protected] |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Data.MyPrelude
Description
This module simply reexports a selection of commonly used standard types and functions.
- class NFData a where
- rnf :: a -> ()
- (&) :: a -> (a -> b) -> b
- (^.) :: s -> Getting a s a -> a
- (.~) :: ASetter s t a b -> b -> s -> t
- type Lens' s a = Lens s s a a
- type Getter s a = forall f. (Contravariant f, Functor f) => (a -> f a) -> s -> f s
- to :: (Profunctor p, Contravariant f) => (s -> a) -> Optic' * * p f s a
- lens :: (s -> a) -> (s -> b -> t) -> Lens s t a b
- when :: Applicative f => Bool -> f () -> f ()
- unless :: Applicative f => Bool -> f () -> f ()
- forM :: (Traversable t, Monad m) => t a -> (a -> m b) -> m (t b)
- forM_ :: (Foldable t, Monad m) => t a -> (a -> m b) -> m ()
- void :: Functor f => f a -> f ()
- replicateM :: Monad m => Int -> m a -> m [a]
- forever :: Monad m => m a -> m b
- guard :: Alternative f => Bool -> f ()
- newtype Identity a :: * -> * = Identity {
- runIdentity :: a
- class Monad m => MonadIO m where
- class Monad m => MonadRandom m where
- getRandom :: Random a => m a
- getRandomR :: Random a => (a, a) -> m a
- getRandom :: MonadRandom m => forall a. Random a => m a
- getRandomR :: MonadRandom m => forall a. Random a => (a, a) -> m a
- data RandT g m a :: * -> (* -> *) -> * -> *
- runRandT :: RandT g m a -> g -> m (a, g)
- evalRandT :: Monad m => RandT g m a -> g -> m a
- data StdGen :: *
- mkStdGen :: Int -> StdGen
- class Monad m => MonadState s m | m -> s where
- lift :: MonadTrans t => forall m a. Monad m => m a -> t m a
- type State s = StateT s Identity
- data StateT s m a :: * -> (* -> *) -> * -> *
- modify :: Monad m => (s -> s) -> StateT s m ()
- runState :: State s a -> s -> (a, s)
- evalState :: State s a -> s -> a
- execState :: State s a -> s -> s
- runStateT :: StateT s m a -> s -> m (a, s)
- evalStateT :: Monad m => StateT s m a -> s -> m a
- execStateT :: Monad m => StateT s m a -> s -> m s
- type Writer w = WriterT w Identity
- data WriterT w m a :: * -> (* -> *) -> * -> *
- tell :: (Monoid w, Monad m) => w -> WriterT w m ()
- runWriter :: Writer w a -> (a, w)
- execWriter :: Writer w a -> w
- runWriterT :: WriterT w m a -> m (a, w)
- execWriterT :: Monad m => WriterT w m a -> m w
- lefts :: [Either a b] -> [a]
- rights :: [Either a b] -> [b]
- toList :: Foldable t => forall a. t a -> [a]
- on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
- sort :: Ord a => [a] -> [a]
- sortBy :: (a -> a -> Ordering) -> [a] -> [a]
- minimumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a
- maximumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a
- foldl' :: Foldable t => forall b a. (b -> a -> b) -> b -> t a -> b
- intercalate :: [a] -> [[a]] -> [a]
- catMaybes :: [Maybe a] -> [a]
- fromJust :: Maybe a -> a
- fromMaybe :: a -> Maybe a -> a
- (<>) :: Monoid m => m -> m -> m
- getDirectoryContents :: FilePath -> IO [FilePath]
- getArgs :: IO [String]
- (</>) :: FilePath -> FilePath -> FilePath
- (<.>) :: FilePath -> String -> FilePath
- withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
- data IOMode :: *
- hPutStr :: Handle -> String -> IO ()
- hPutStrLn :: Handle -> String -> IO ()
- printf :: PrintfType r => String -> r
Documentation
class NFData a where
A class of types that can be fully evaluated.
Since: 1.1.0.0
Minimal complete definition
Nothing
Methods
rnf :: a -> ()
rnf
should reduce its argument to normal form (that is, fully
evaluate all sub-components), and then return '()'.
Generic
NFData
deriving
Starting with GHC 7.2, you can automatically derive instances
for types possessing a Generic
instance.
{-# LANGUAGE DeriveGeneric #-} import GHC.Generics (Generic) import Control.DeepSeq data Foo a = Foo a String deriving (Eq, Generic) instance NFData a => NFData (Foo a) data Colour = Red | Green | Blue deriving Generic instance NFData Colour
Starting with GHC 7.10, the example above can be written more
concisely by enabling the new DeriveAnyClass
extension:
{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-} import GHC.Generics (Generic) import Control.DeepSeq data Foo a = Foo a String deriving (Eq, Generic, NFData) data Colour = Red | Green | Blue deriving (Generic, NFData)
Compatibility with previous deepseq
versions
Prior to version 1.4.0.0, the default implementation of the rnf
method was defined as
rnf
a =seq
a ()
However, starting with deepseq-1.4.0.0
, the default
implementation is based on DefaultSignatures
allowing for
more accurate auto-derived NFData
instances. If you need the
previously used exact default rnf
method implementation
semantics, use
instance NFData Colour where rnf x = seq x ()
or alternatively
{-# LANGUAGE BangPatterns #-} instance NFData Colour where rnf !_ = ()
Instances
NFData Bool | |
NFData Char | |
NFData Double | |
NFData Float | |
NFData Int | |
NFData Int8 | |
NFData Int16 | |
NFData Int32 | |
NFData Int64 | |
NFData Integer | |
NFData Word | |
NFData Word8 | |
NFData Word16 | |
NFData Word32 | |
NFData Word64 | |
NFData TypeRep | NOTE: Only defined for Since: 1.4.0.0 |
NFData () | |
NFData Void | Since: 1.4.0.0 |
NFData Unique | Since: 1.4.0.0 |
NFData Natural | Since: 1.4.0.0 |
NFData Version | Since: 1.3.0.0 |
NFData ThreadId | Since: 1.4.0.0 |
NFData CChar | Since: 1.4.0.0 |
NFData CSChar | Since: 1.4.0.0 |
NFData CUChar | Since: 1.4.0.0 |
NFData CShort | Since: 1.4.0.0 |
NFData CUShort | Since: 1.4.0.0 |
NFData CInt | Since: 1.4.0.0 |
NFData CUInt | Since: 1.4.0.0 |
NFData CLong | Since: 1.4.0.0 |
NFData CULong | Since: 1.4.0.0 |
NFData CLLong | Since: 1.4.0.0 |
NFData CULLong | Since: 1.4.0.0 |
NFData CFloat | Since: 1.4.0.0 |
NFData CDouble | Since: 1.4.0.0 |
NFData CPtrdiff | Since: 1.4.0.0 |
NFData CSize | Since: 1.4.0.0 |
NFData CWchar | Since: 1.4.0.0 |
NFData CSigAtomic | Since: 1.4.0.0 |
NFData CClock | Since: 1.4.0.0 |
NFData CTime | Since: 1.4.0.0 |
NFData CUSeconds | Since: 1.4.0.0 |
NFData CSUSeconds | Since: 1.4.0.0 |
NFData CFile | Since: 1.4.0.0 |
NFData CFpos | Since: 1.4.0.0 |
NFData CJmpBuf | Since: 1.4.0.0 |
NFData CIntPtr | Since: 1.4.0.0 |
NFData CUIntPtr | Since: 1.4.0.0 |
NFData CIntMax | Since: 1.4.0.0 |
NFData CUIntMax | Since: 1.4.0.0 |
NFData All | Since: 1.4.0.0 |
NFData Any | Since: 1.4.0.0 |
NFData TyCon | NOTE: Only defined for Since: 1.4.0.0 |
NFData Fingerprint | Since: 1.4.0.0 |
NFData ByteString | |
NFData ByteString | |
NFData IntSet | |
NFData Doc | |
NFData TextDetails | |
NFData LocalTime | |
NFData ZonedTime | |
NFData TimeOfDay | |
NFData TimeZone | |
NFData UTCTime | |
NFData NominalDiffTime | |
NFData Day | |
NFData a => NFData [a] | |
(Integral a, NFData a) => NFData (Ratio a) | |
NFData (StableName a) | Since: 1.4.0.0 |
NFData a => NFData (Identity a) | Since: 1.4.0.0 |
NFData (Fixed a) | Since: 1.3.0.0 |
NFData a => NFData (Complex a) | |
NFData a => NFData (ZipList a) | Since: 1.4.0.0 |
NFData a => NFData (Dual a) | Since: 1.4.0.0 |
NFData a => NFData (Sum a) | Since: 1.4.0.0 |
NFData a => NFData (Product a) | Since: 1.4.0.0 |
NFData a => NFData (First a) | Since: 1.4.0.0 |
NFData a => NFData (Last a) | Since: 1.4.0.0 |
NFData a => NFData (Down a) | Since: 1.4.0.0 |
NFData a => NFData (Maybe a) | |
NFData a => NFData (Digit a) | |
NFData a => NFData (Node a) | |
NFData a => NFData (Elem a) | |
NFData a => NFData (FingerTree a) | |
NFData a => NFData (IntMap a) | |
NFData a => NFData (Set a) | |
NFData a => NFData (Tree a) | |
NFData a => NFData (Seq a) | |
NFData a => NFData (Probability a) | |
NFData (Vector a) | |
NFData a => NFData (NonEmpty a) | |
NFData a => NFData (Vector a) | |
NFData (Vector a) | |
NFData (Vector a) | |
NFData a => NFData (HashSet a) | |
NFData m => NFData (WrappedMonoid m) | |
NFData a => NFData (Option a) | |
NFData a => NFData (Min a) | |
NFData a => NFData (Max a) | |
NFData a => NFData (Last a) | |
NFData a => NFData (First a) | |
NFData (a -> b) | This instance is for convenience and consistency with Since: 1.3.0.0 |
(NFData a, NFData b) => NFData (Either a b) | |
(NFData a, NFData b) => NFData (a, b) | |
(Ix a, NFData a, NFData b) => NFData (Array a b) | |
NFData a => NFData (Const a b) | Since: 1.4.0.0 |
NFData (Proxy * a) | Since: 1.4.0.0 |
(NFData k, NFData a) => NFData (Map k a) | |
NFData (MVector s a) | |
NFData (MVector s a) | |
NFData (MVector s a) | |
(NFData k, NFData v) => NFData (HashMap k v) | |
(NFData a, NFData b) => NFData (Arg a b) | |
(NFData k, NFData v) => NFData (Leaf k v) | |
(NFData a, NFData b, NFData c) => NFData (a, b, c) | |
NFData b => NFData (Tagged k s b) | |
(NFData a, NFData b, NFData c, NFData d) => NFData (a, b, c, d) | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5) => NFData (a1, a2, a3, a4, a5) | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6) => NFData (a1, a2, a3, a4, a5, a6) | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7) => NFData (a1, a2, a3, a4, a5, a6, a7) | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7, NFData a8) => NFData (a1, a2, a3, a4, a5, a6, a7, a8) | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7, NFData a8, NFData a9) => NFData (a1, a2, a3, a4, a5, a6, a7, a8, a9) |
(&) :: a -> (a -> b) -> b infixl 1
(^.) :: s -> Getting a s a -> a
(.~) :: ASetter s t a b -> b -> s -> t
type Lens' s a = Lens s s a a
to :: (Profunctor p, Contravariant f) => (s -> a) -> Optic' * * p f s a
lens :: (s -> a) -> (s -> b -> t) -> Lens s t a b
when :: Applicative f => Bool -> f () -> f ()
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.
unless :: Applicative f => Bool -> f () -> f ()
The reverse of when
.
forM :: (Traversable t, Monad m) => t a -> (a -> m b) -> m (t b)
void :: Functor f => f a -> f ()
discards or ignores the result of evaluation, such
as the return value of an void
valueIO
action.
Examples
Replace the contents of a
with unit:Maybe
Int
>>>
void Nothing
Nothing>>>
void (Just 3)
Just ()
Replace the contents of an
with unit,
resulting in an Either
Int
Int
:Either
Int
'()'
>>>
void (Left 8675309)
Left 8675309>>>
void (Right 8675309)
Right ()
Replace every element of a list with unit:
>>>
void [1,2,3]
[(),(),()]
Replace the second element of a pair with unit:
>>>
void (1,2)
(1,())
Discard the result of an IO
action:
>>>
mapM print [1,2]
1 2 [(),()]>>>
void $ mapM print [1,2]
1 2
replicateM :: Monad m => Int -> m a -> m [a]
performs the action replicateM
n actn
times,
gathering the results.
newtype Identity a :: * -> *
Identity functor and monad. (a non-strict monad)
Since: 4.8.0.0
Constructors
Identity | |
Fields
|
Instances
Monad Identity | |
Functor Identity | |
MonadFix Identity | |
Applicative Identity | |
Foldable Identity | |
Traversable Identity | |
Generic1 Identity | |
MonadZip Identity | |
Eq1 Identity | |
Ord1 Identity | |
Read1 Identity | |
Show1 Identity | |
Comonad Identity | |
ComonadApply Identity | |
Representable Identity | |
Sieve ReifiedGetter Identity | |
Cosieve ReifiedGetter Identity | |
Eq a => Eq (Identity a) | |
Data a => Data (Identity a) | |
Ord a => Ord (Identity a) | |
Read a => Read (Identity a) | This instance would be equivalent to the derived instances of the
|
Show a => Show (Identity a) | This instance would be equivalent to the derived instances of the
|
Generic (Identity a) | |
NFData a => NFData (Identity a) | Since: 1.4.0.0 |
Wrapped (Identity a) | |
Ixed (Identity a) | |
Semigroup a => Semigroup (Identity a) | |
(~) * t (Identity b) => Rewrapped (Identity a) t | |
Field1 (Identity a) (Identity b) a b | |
type Rep1 Identity = D1 D1Identity (C1 C1_0Identity (S1 S1_0_0Identity Par1)) | |
type Rep Identity = () | |
type Rep (Identity a) = D1 D1Identity (C1 C1_0Identity (S1 S1_0_0Identity (Rec0 a))) | |
type Unwrapped (Identity a) = a | |
type IxValue (Identity a) = a | |
type Index (Identity a) = () |
class Monad m => MonadIO m where
Monads in which IO
computations may be embedded.
Any monad built by applying a sequence of monad transformers to the
IO
monad will be an instance of this class.
Instances should satisfy the following laws, which state that liftIO
is a transformer of monads:
Instances
MonadIO IO | |
MonadIO m => MonadIO (MaybeT m) | |
MonadIO m => MonadIO (ListT m) | |
MonadIO m => MonadIO (IdentityT m) | |
MonadIO m => MonadIO (ListT m) | |
(Monoid w, MonadIO m) => MonadIO (WriterT w m) | |
(Monoid w, MonadIO m) => MonadIO (WriterT w m) | |
(Error e, MonadIO m) => MonadIO (ErrorT e m) | |
MonadIO m => MonadIO (ExceptT e m) | |
MonadIO m => MonadIO (StateT s m) | |
MonadIO m => MonadIO (StateT s m) | |
MonadIO m => MonadIO (ReaderT r m) | |
MonadIO m => MonadIO (ContT r m) | |
MonadIO m => MonadIO (RandT g m) | |
(Functor f, MonadIO m) => MonadIO (FreeT f m) | |
(Monoid w, MonadIO m) => MonadIO (RWST r w s m) | |
(Monoid w, MonadIO m) => MonadIO (RWST r w s m) | |
MonadIO m => MonadIO (Proxy a' a b' b m) |
class Monad m => MonadRandom m where
Minimal complete definition
getRandom, getRandoms, getRandomR, getRandomRs
Instances
(Monad m, RandomGen g) => MonadRandom (RandT g m) |
getRandom :: MonadRandom m => forall a. Random a => m a
getRandomR :: MonadRandom m => forall a. Random a => (a, a) -> m a
data RandT g m a :: * -> (* -> *) -> * -> *
Instances
MonadState s m => MonadState s (RandT g m) | |
MonadWriter w m => MonadWriter w (RandT g m) | |
MonadReader r m => MonadReader r (RandT g m) | |
(Monad m, RandomGen g) => MonadSplit g (RandT g m) | |
MonadTrans (RandT g) | |
Monad m => Monad (RandT g m) | |
Functor m => Functor (RandT g m) | |
MonadFix m => MonadFix (RandT g m) | |
(Functor m, Monad m) => Applicative (RandT g m) | |
(Functor m, MonadPlus m) => Alternative (RandT g m) | |
MonadPlus m => MonadPlus (RandT g m) | |
MonadIO m => MonadIO (RandT g m) | |
(Monad m, RandomGen g) => MonadRandom (RandT g m) |
class Monad m => MonadState s m | m -> s where
Instances
MonadState s m => MonadState s (MaybeT m) | |
MonadState s m => MonadState s (ListT m) | |
MonadState s m => MonadState s (IdentityT m) | |
MonadState s m => MonadState s (ListT m) | |
(Monoid w, MonadState s m) => MonadState s (WriterT w m) | |
(Monoid w, MonadState s m) => MonadState s (WriterT w m) | |
Monad m => MonadState s (StateT s m) | |
Monad m => MonadState s (StateT s m) | |
MonadState s m => MonadState s (ReaderT r m) | |
MonadState s m => MonadState s (ExceptT e m) | |
(Error e, MonadState s m) => MonadState s (ErrorT e m) | |
MonadState s m => MonadState s (ContT r m) | |
MonadState s m => MonadState s (RandT g m) | |
(Functor f, MonadState s m) => MonadState s (FreeT f m) | |
(Monad m, Monoid w) => MonadState s (RWST r w s m) | |
(Monad m, Monoid w) => MonadState s (RWST r w s m) | |
MonadState s m => MonadState s (Proxy a' a b' b m) |
lift :: MonadTrans t => forall m a. Monad m => m a -> t m a
Lift a computation from the argument monad to the constructed monad.
type State s = StateT s Identity
A state monad parameterized by the type s
of the state to carry.
The return
function leaves the state unchanged, while >>=
uses
the final state of the first computation as the initial state of
the second.
data StateT s m a :: * -> (* -> *) -> * -> *
A state transformer monad parameterized by:
s
- The state.m
- The inner monad.
The return
function leaves the state unchanged, while >>=
uses
the final state of the first computation as the initial state of
the second.
Instances
Monad m => MonadState s (StateT s m) | |
MonadTrans (StateT s) | |
Monad m => Monad (StateT s m) | |
Functor m => Functor (StateT s m) | |
MonadFix m => MonadFix (StateT s m) | |
(Functor m, Monad m) => Applicative (StateT s m) | |
(Functor m, MonadPlus m) => Alternative (StateT s m) | |
MonadPlus m => MonadPlus (StateT s m) | |
MonadIO m => MonadIO (StateT s m) | |
Contravariant m => Contravariant (StateT s m) | |
PrimMonad m => PrimMonad (StateT s m) | |
Monad z => Zoom (StateT s z) (StateT t z) s t | |
Wrapped (StateT s m a) | |
(~) * t (StateT s' m' a') => Rewrapped (StateT s m a) t | |
type Zoomed (StateT s z) = Focusing z | |
type PrimState (StateT s m) = PrimState m | |
type Unwrapped (StateT s m a) = s -> m (a, s) |
Arguments
:: State s a | state-passing computation to execute |
-> s | initial state |
-> (a, s) | return value and final state |
Unwrap a state monad computation as a function.
(The inverse of state
.)
Arguments
:: State s a | state-passing computation to execute |
-> s | initial value |
-> a | return value of the state computation |
Arguments
:: State s a | state-passing computation to execute |
-> s | initial value |
-> s | final state |
evalStateT :: Monad m => StateT s m a -> s -> m a
Evaluate a state computation with the given initial state and return the final value, discarding the final state.
evalStateT
m s =liftM
fst
(runStateT
m s)
execStateT :: Monad m => StateT s m a -> s -> m s
Evaluate a state computation with the given initial state and return the final state, discarding the final value.
execStateT
m s =liftM
snd
(runStateT
m s)
data WriterT w m a :: * -> (* -> *) -> * -> *
A writer monad parameterized by:
w
- the output to accumulate.m
- The inner monad.
The return
function produces the output mempty
, while >>=
combines the outputs of the subcomputations using mappend
.
Instances
(Monoid w, MonadState s m) => MonadState s (WriterT w m) | |
Monoid w => MonadTrans (WriterT w) | |
(Monoid w, Monad m) => Monad (WriterT w m) | |
Functor m => Functor (WriterT w m) | |
(Monoid w, MonadFix m) => MonadFix (WriterT w m) | |
(Monoid w, Applicative m) => Applicative (WriterT w m) | |
Foldable f => Foldable (WriterT w f) | |
Traversable f => Traversable (WriterT w f) | |
(Monoid w, Alternative m) => Alternative (WriterT w m) | |
(Monoid w, MonadPlus m) => MonadPlus (WriterT w m) | |
(Monoid w, MonadIO m) => MonadIO (WriterT w m) | |
(Eq w, Eq1 m) => Eq1 (WriterT w m) | |
(Ord w, Ord1 m) => Ord1 (WriterT w m) | |
(Read w, Read1 m) => Read1 (WriterT w m) | |
(Show w, Show1 m) => Show1 (WriterT w m) | |
Contravariant m => Contravariant (WriterT w m) | |
(Monoid w, PrimMonad m) => PrimMonad (WriterT w m) | |
(Monoid w, Zoom m n s t) => Zoom (WriterT w m) (WriterT w n) s t | |
(Eq w, Eq1 m, Eq a) => Eq (WriterT w m a) | |
(Ord w, Ord1 m, Ord a) => Ord (WriterT w m a) | |
(Read w, Read1 m, Read a) => Read (WriterT w m a) | |
(Show w, Show1 m, Show a) => Show (WriterT w m a) | |
Wrapped (WriterT w m a) | |
(~) * t (WriterT w' m' a') => Rewrapped (WriterT w m a) t | |
type Zoomed (WriterT w m) = FocusingPlus w (Zoomed m) | |
type PrimState (WriterT w m) = PrimState m | |
type Unwrapped (WriterT w m a) = m (a, w) |
runWriter :: Writer w a -> (a, w)
Unwrap a writer computation as a (result, output) pair.
(The inverse of writer
.)
execWriter :: Writer w a -> w
Extract the output from a writer computation.
execWriter
m =snd
(runWriter
m)
runWriterT :: WriterT w m a -> m (a, w)
execWriterT :: Monad m => WriterT w m a -> m w
Extract the output from a writer computation.
execWriterT
m =liftM
snd
(runWriterT
m)
on :: (b -> b -> c) -> (a -> b) -> a -> a -> c infixl 0
minimumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a
The least element of a non-empty structure with respect to the given comparison function.
maximumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a
The largest element of a non-empty structure with respect to the given comparison function.
intercalate :: [a] -> [[a]] -> [a]
intercalate
xs xss
is equivalent to (
.
It inserts the list concat
(intersperse
xs xss))xs
in between the lists in xss
and concatenates the
result.
The catMaybes
function takes a list of Maybe
s and returns
a list of all the Just
values.
Examples
Basic usage:
>>>
catMaybes [Just 1, Nothing, Just 3]
[1,3]
When constructing a list of Maybe
values, catMaybes
can be used
to return all of the "success" results (if the list is the result
of a map
, then mapMaybe
would be more appropriate):
>>>
import Text.Read ( readMaybe )
>>>
[readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ]
[Just 1,Nothing,Just 3]>>>
catMaybes $ [readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ]
[1,3]
fromMaybe :: a -> Maybe a -> a
The fromMaybe
function takes a default value and and Maybe
value. If the Maybe
is Nothing
, it returns the default values;
otherwise, it returns the value contained in the Maybe
.
Examples
Basic usage:
>>>
fromMaybe "" (Just "Hello, World!")
"Hello, World!"
>>>
fromMaybe "" Nothing
""
Read an integer from a string using readMaybe
. If we fail to
parse an integer, we want to return 0
by default:
>>>
import Text.Read ( readMaybe )
>>>
fromMaybe 0 (readMaybe "5")
5>>>
fromMaybe 0 (readMaybe "")
0
getDirectoryContents :: FilePath -> IO [FilePath]
returns a list of all entries
in dir.getDirectoryContents
dir
The operation may fail with:
HardwareFault
A physical I/O error has occurred.[EIO]
InvalidArgument
The operand is not a valid directory name.[ENAMETOOLONG, ELOOP]
isDoesNotExistError
/NoSuchThing
The directory does not exist.[ENOENT, ENOTDIR]
isPermissionError
/PermissionDenied
The process has insufficient privileges to perform the operation.[EACCES]
ResourceExhausted
Insufficient resources are available to perform the operation.[EMFILE, ENFILE]
InappropriateType
The path refers to an existing non-directory object.[ENOTDIR]
Computation getArgs
returns a list of the program's command
line arguments (not including the program name).
(</>) :: FilePath -> FilePath -> FilePath infixr 5
Join two values with a path separator. For examples and caveats see the equivalent function combine
.
Posix: "/directory" </> "file.ext" == "/directory/file.ext" Windows: "/directory" </> "file.ext" == "/directory\\file.ext"
(<.>) :: FilePath -> String -> FilePath infixr 7
Add an extension, even if there is already one there, equivalent to addExtension
.
"/directory/path" <.> "ext" == "/directory/path.ext" "/directory/path" <.> ".ext" == "/directory/path.ext"
withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
opens a file using withFile
name mode actopenFile
and passes
the resulting handle to the computation act
. The handle will be
closed on exit from withFile
, whether by normal termination or by
raising an exception. If closing the handle raises an exception, then
this exception will be raised by withFile
rather than any exception
raised by act
.
hPutStr :: Handle -> String -> IO ()
Computation hPutStr
hdl s
writes the string
s
to the file or channel managed by hdl
.
This operation may fail with:
isFullError
if the device is full; orisPermissionError
if another system resource limit would be exceeded.
printf :: PrintfType r => String -> r
Format a variable number of arguments with the C-style formatting string.
The return value is either String
or (
(which
should be IO
a)(
, but Haskell's type system
makes this hard).IO
'()')
The format string consists of ordinary characters and
conversion specifications, which specify how to format
one of the arguments to printf
in the output string. A
format specification is introduced by the %
character;
this character can be self-escaped into the format string
using %%
. A format specification ends with a /format
character/ that provides the primary information about
how to format the value. The rest of the conversion
specification is optional. In order, one may have flag
characters, a width specifier, a precision specifier, and
type-specific modifier characters.
Unlike C printf(3)
, the formatting of this printf
is driven by the argument type; formatting is type specific. The
types formatted by printf
"out of the box" are:
printf
is also extensible to support other types: see below.
A conversion specification begins with the
character %
, followed by zero or more of the following flags:
- left adjust (default is right adjust) + always use a sign (+ or -) for signed conversions space leading space for positive numbers in signed conversions 0 pad with zeros rather than spaces # use an \"alternate form\": see below
When both flags are given, -
overrides 0
and +
overrides space.
A negative width specifier in a *
conversion is treated as
positive but implies the left adjust flag.
The "alternate form" for unsigned radix conversions is
as in C printf(3)
:
%o prefix with a leading 0 if needed %x prefix with a leading 0x if nonzero %X prefix with a leading 0X if nonzero %b prefix with a leading 0b if nonzero %[eEfFgG] ensure that the number contains a decimal point
Any flags are followed optionally by a field width:
num field width * as num, but taken from argument list
The field width is a minimum, not a maximum: it will be expanded as needed to avoid mutilating a value.
Any field width is followed optionally by a precision:
.num precision . same as .0 .* as num, but taken from argument list
Negative precision is taken as 0. The meaning of the precision depends on the conversion type.
Integral minimum number of digits to show RealFloat number of digits after the decimal point String maximum number of characters
The precision for Integral types is accomplished by zero-padding. If both precision and zero-pad are given for an Integral field, the zero-pad is ignored.
Any precision is followed optionally for Integral types by a width modifier; the only use of this modifier being to set the implicit size of the operand for conversion of a negative operand to unsigned:
hh Int8 h Int16 l Int32 ll Int64 L Int64
The specification ends with a format character:
c character Integral d decimal Integral o octal Integral x hexadecimal Integral X hexadecimal Integral b binary Integral u unsigned decimal Integral f floating point RealFloat F floating point RealFloat g general format float RealFloat G general format float RealFloat e exponent format float RealFloat E exponent format float RealFloat s string String v default format any type
The "%v" specifier is provided for all built-in types, and should be provided for user-defined type formatters as well. It picks a "best" representation for the given type. For the built-in types the "%v" specifier is converted as follows:
c Char u other unsigned Integral d other signed Integral g RealFloat s String
Mismatch between the argument types and the format string, as well as any other syntactic or semantic errors in the format string, will cause an exception to be thrown at runtime.
Note that the formatting for RealFloat
types is
currently a bit different from that of C printf(3)
,
conforming instead to showEFloat
,
showFFloat
and showGFloat
(and their
alternate versions showFFloatAlt
and
showGFloatAlt
). This is hard to fix: the fixed
versions would format in a backward-incompatible way.
In any case the Haskell behavior is generally more
sensible than the C behavior. A brief summary of some
key differences:
- Haskell
printf
never uses the default "6-digit" precision used by C printf. - Haskell
printf
treats the "precision" specifier as indicating the number of digits after the decimal point. - Haskell
printf
prints the exponent of e-format numbers without a gratuitous plus sign, and with the minimum possible number of digits. - Haskell
printf
will place a zero after a decimal point when possible.
Examples
> printf "%d\n" (23::Int) 23 > printf "%s %s\n" "Hello" "World" Hello World > printf "%.2f\n" pi 3.14