| Copyright | Oleg Grenrus |
|---|---|
| License | GPL-3.0-or-later |
| Safe Haskell | None |
| Language | Haskell2010 |
CabalFmt.Prelude
Description
Fat-prelude.
Synopsis
- (&&&) :: Arrow a => a b c -> a b c' -> a b (c, c')
- when :: Applicative f => Bool -> f () -> f ()
- unless :: Applicative f => Bool -> f () -> f ()
- void :: Functor f => f a -> f ()
- bimap :: Bifunctor p => (a -> b) -> (c -> d) -> p a c -> p b d
- toLower :: Char -> Char
- partitionEithers :: [Either a b] -> ([a], [b])
- toList :: Foldable t => t a -> [a]
- traverse_ :: (Foldable t, Applicative f) => (a -> f b) -> t a -> f ()
- asum :: (Foldable t, Alternative f) => t (f a) -> f a
- for_ :: (Foldable t, Applicative f) => t a -> (a -> f b) -> f ()
- on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
- (&) :: a -> (a -> b) -> b
- intercalate :: [a] -> [[a]] -> [a]
- sortOn :: Ord b => (a -> b) -> [a] -> [a]
- sortBy :: (a -> a -> Ordering) -> [a] -> [a]
- nub :: Eq a => [a] -> [a]
- catMaybes :: [Maybe a] -> [a]
- fromMaybe :: a -> Maybe a -> a
- isJust :: Maybe a -> Bool
- isNothing :: Maybe a -> Bool
- data ByteString
- fromUTF8BS :: ByteString -> String
- toUTF8BS :: String -> ByteString
- pack' :: Newtype o n => (o -> n) -> o -> n
- unpack' :: Newtype o n => (o -> n) -> n -> o
- data Set a
- dropExtension :: FilePath -> FilePath
- splitDirectories :: FilePath -> [FilePath]
- catchError :: MonadError e m => m a -> (e -> m a) -> m a
- throwError :: MonadError e m => e -> m a
- traverseOf :: Applicative f => ((a -> f b) -> s -> f t) -> (a -> f b) -> s -> f t
- over :: ASetter s t a b -> (a -> b) -> s -> t
- view :: Getting a s a -> s -> a
- _1 :: Functor f => (a -> f b) -> (a, c) -> f (b, c)
- fstOf3 :: (a, b, c) -> a
- sndOf3 :: (a, b, c) -> b
- trdOf3 :: (a, b, c) -> c
Control.Arrow
(&&&) :: Arrow a => a b c -> a b c' -> a b (c, c') infixr 3 #
Fanout: send the input to both argument arrows and combine their output.
The default definition may be overridden with a more efficient version if desired.
Control.Monad
when :: Applicative f => Bool -> f () -> f () #
Conditional execution of Applicative expressions. For example,
Examples
when debug (putStrLn "Debugging")
will output the string Debugging if the Boolean value debug
is True, and otherwise do nothing.
>>>putStr "pi:" >> when False (print 3.14159)pi:
unless :: Applicative f => Bool -> f () -> f () #
The reverse of when.
Examples
>>>do x <- getLineunless (x == "hi") (putStrLn "hi!") comingupwithexamplesisdifficult hi!
>>>unless (pi > exp 1) NothingJust ()
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 NothingNothing
>>>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
Data.Bifunctor
Data.Char
Convert a letter to the corresponding lower-case letter, if any. Any other character is returned unchanged.
Data.Either
partitionEithers :: [Either a b] -> ([a], [b]) #
Partitions a list of Either into two lists.
All the Left elements are extracted, in order, to the first
component of the output. Similarly the Right elements are extracted
to the second component of the output.
Examples
Basic usage:
>>>let list = [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]>>>partitionEithers list(["foo","bar","baz"],[3,7])
The pair returned by should be the same
pair as partitionEithers x(:lefts x, rights x)
>>>let list = [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]>>>partitionEithers list == (lefts list, rights list)True
Data.Foldable
toList :: Foldable t => t a -> [a] #
List of elements of a structure, from left to right. If the entire list is intended to be reduced via a fold, just fold the structure directly bypassing the list.
Examples
Basic usage:
>>>toList Nothing[]
>>>toList (Just 42)[42]
>>>toList (Left "foo")[]
>>>toList (Node (Leaf 5) 17 (Node Empty 12 (Leaf 8)))[5,17,12,8]
For lists, toList is the identity:
>>>toList [1, 2, 3][1,2,3]
@since base-4.8.0.0
traverse_ :: (Foldable t, Applicative f) => (a -> f b) -> t a -> f () #
Map each element of a structure to an Applicative action, evaluate these
actions from left to right, and ignore the results. For a version that
doesn't ignore the results see traverse.
traverse_ is just like mapM_, but generalised to Applicative actions.
Examples
Basic usage:
>>>traverse_ print ["Hello", "world", "!"]"Hello" "world" "!"
asum :: (Foldable t, Alternative f) => t (f a) -> f a #
The sum of a collection of actions using (<|>), generalizing concat.
asum is just like msum, but generalised to Alternative.
Examples
Basic usage:
>>>asum [Just "Hello", Nothing, Just "World"]Just "Hello"
for_ :: (Foldable t, Applicative f) => t a -> (a -> f b) -> f () #
for_ is traverse_ with its arguments flipped. For a version
that doesn't ignore the results see for. This
is forM_ generalised to Applicative actions.
for_ is just like forM_, but generalised to Applicative actions.
Examples
Basic usage:
>>>for_ [1..4] print1 2 3 4
Data.Function
on :: (b -> b -> c) -> (a -> b) -> a -> a -> c infixl 0 #
runs the binary function on b u x yb on the results of applying
unary function u to two arguments x and y. From the opposite
perspective, it transforms two inputs and combines the outputs.
(op `on` f) x y = f x `op` f y
Examples
>>>sortBy (compare `on` length) [[0, 1, 2], [0, 1], [], [0]][[],[0],[0,1],[0,1,2]]
>>>((+) `on` length) [1, 2, 3] [-1]4
>>>((,) `on` (*2)) 2 3(4,6)
Algebraic properties
(&) :: a -> (a -> b) -> b infixl 1 #
& is a reverse application operator. This provides notational
convenience. Its precedence is one higher than that of the forward
application operator $, which allows & to be nested in $.
This is a version of , where flip idid is specialized from a -> a to (a -> b) -> (a -> b)
which by the associativity of (->) is (a -> b) -> a -> b.
flipping this yields a -> (a -> b) -> b which is the type signature of &
Examples
>>>5 & (+1) & show"6"
>>>sqrt $ [1 / n^2 | n <- [1..1000]] & sum & (*6)3.1406380562059946
@since base-4.8.0.0
Data.List
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.
Laziness
intercalate has the following properties:
>>>take 5 (intercalate undefined ("Lorem" : undefined))"Lorem"
>>>take 6 (intercalate ", " ("Lorem" : undefined))"Lorem*** Exception: Prelude.undefined
Examples
>>>intercalate ", " ["Lorem", "ipsum", "dolor"]"Lorem, ipsum, dolor"
>>>intercalate [0, 1] [[2, 3], [4, 5, 6], []][2,3,0,1,4,5,6,0,1]
>>>intercalate [1, 2, 3] [[], []][1,2,3]
sortOn :: Ord b => (a -> b) -> [a] -> [a] #
Sort a list by comparing the results of a key function applied to each
element. is equivalent to sortOn f, but has the
performance advantage of only evaluating sortBy (comparing f)f once for each element in the
input list. This is called the decorate-sort-undecorate paradigm, or
Schwartzian transform.
Elements are arranged from lowest to highest, keeping duplicates in the order they appeared in the input.
The argument must be finite.
Examples
>>>sortOn fst [(2, "world"), (4, "!"), (1, "Hello")][(1,"Hello"),(2,"world"),(4,"!")]
>>>sortOn length ["jim", "creed", "pam", "michael", "dwight", "kevin"]["jim","pam","creed","kevin","dwight","michael"]
Performance notes
This function minimises the projections performed, by materialising the projections in an intermediate list.
For trivial projections, you should prefer using sortBy with
comparing, for example:
>>>sortBy (comparing fst) [(3, 1), (2, 2), (1, 3)][(1,3),(2,2),(3,1)]
Or, for the exact same API as sortOn, you can use `sortBy . comparing`:
>>>(sortBy . comparing) fst [(3, 1), (2, 2), (1, 3)][(1,3),(2,2),(3,1)]
@since base-4.8.0.0
sortBy :: (a -> a -> Ordering) -> [a] -> [a] #
The sortBy function is the non-overloaded version of sort.
The argument must be finite.
The supplied comparison relation is supposed to be reflexive and antisymmetric,
otherwise, e. g., for _ _ -> GT, the ordered list simply does not exist.
The relation is also expected to be transitive: if it is not then sortBy
might fail to find an ordered permutation, even if it exists.
Examples
>>>sortBy (\(a,_) (b,_) -> compare a b) [(2, "world"), (4, "!"), (1, "Hello")][(1,"Hello"),(2,"world"),(4,"!")]
\(\mathcal{O}(n^2)\). The nub function removes duplicate elements from a
list. In particular, it keeps only the first occurrence of each element. (The
name nub means `essence'.) It is a special case of nubBy, which allows
the programmer to supply their own equality test.
If there exists instance Ord a, it's faster to use nubOrd from the containers package
(link to the latest online documentation),
which takes only \(\mathcal{O}(n \log d)\) time where d is the number of
distinct elements in the list.
Another approach to speed up nub is to use
map Data.List.NonEmpty.head . Data.List.NonEmpty.group . sort,
which takes \(\mathcal{O}(n \log n)\) time, requires instance Ord a and doesn't
preserve the order.
Examples
>>>nub [1,2,3,4,3,2,1,2,4,3,5][1,2,3,4,5]
>>>nub "hello, world!""helo, wrd!"
Data.Maybe
catMaybes :: [Maybe a] -> [a] #
The catMaybes function takes a list of Maybes 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 GHC.Internal.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 a Maybe
value. If the Maybe is Nothing, it returns the default value;
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 GHC.Internal.Text.Read ( readMaybe )>>>fromMaybe 0 (readMaybe "5")5>>>fromMaybe 0 (readMaybe "")0
Packages
bytestring
data ByteString #
A space-efficient representation of a Word8 vector, supporting many
efficient operations.
A ByteString contains 8-bit bytes, or by using the operations from
Data.ByteString.Char8 it can be interpreted as containing 8-bit
characters.
Instances
Cabal
fromUTF8BS :: ByteString -> String #
Decode String from UTF8-encoded ByteString
Invalid data in the UTF8 stream (this includes code-points U+D800
through U+DFFF) will be decoded as the replacement character (U+FFFD).
toUTF8BS :: String -> ByteString #
Encode String to UTF8-encoded ByteString
Code-points in the U+D800-U+DFFF range will be encoded
as the replacement character (i.e. U+FFFD).
containers
A set of values a.
Instances
| 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 |
| Foldable Set | Folds in order of increasing key. |
Defined in Data.Set.Internal Methods fold :: Monoid m => Set m -> m # foldMap :: Monoid m => (a -> m) -> Set a -> m # foldMap' :: Monoid m => (a -> m) -> Set a -> m # foldr :: (a -> b -> b) -> b -> Set a -> b # foldr' :: (a -> b -> b) -> b -> Set a -> b # foldl :: (b -> a -> b) -> b -> Set a -> b # foldl' :: (b -> a -> b) -> b -> Set a -> b # foldr1 :: (a -> a -> a) -> Set a -> a # foldl1 :: (a -> a -> a) -> Set a -> a # elem :: Eq a => a -> Set a -> Bool # maximum :: Ord a => Set a -> a # | |
| Lift a => Lift (Set a :: Type) | Since: containers-0.6.6 |
| Structured k => Structured (Set k) | |
Defined in Distribution.Utils.Structured | |
| NFData a => NFData (Set a) | |
Defined in Data.Set.Internal | |
| Ord a => Monoid (Set a) | |
| Ord a => Semigroup (Set a) | Since: containers-0.5.7 |
| (Data a, Ord a) => Data (Set a) | |
Defined in Data.Set.Internal Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Set a -> c (Set a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Set a) # dataTypeOf :: Set a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Set a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Set a)) # gmapT :: (forall b. Data b => b -> b) -> Set a -> Set a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Set a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Set a -> r # gmapQ :: (forall d. Data d => d -> u) -> Set a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Set a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Set a -> m (Set a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Set a -> m (Set a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Set a -> m (Set a) # | |
| Ord a => IsList (Set a) | Since: containers-0.5.6.2 |
| (Read a, Ord a) => Read (Set a) | |
| Show a => Show (Set a) | |
| Eq a => Eq (Set a) | |
| Ord a => Ord (Set a) | |
| Newtype (Set a) (Set' sep wrapper a) | |
| type Item (Set a) | |
Defined in Data.Set.Internal | |
directory
dropExtension :: FilePath -> FilePath #
Remove last extension, and the "." preceding it.
dropExtension "/directory/path.ext" == "/directory/path" dropExtension x == fst (splitExtension x)
splitDirectories :: FilePath -> [FilePath] #
Just as splitPath, but don't add the trailing slashes to each element.
splitDirectories "/directory/file.ext" == ["/","directory","file.ext"]
splitDirectories "test/file" == ["test","file"]
splitDirectories "/test/file" == ["/","test","file"]
Windows: splitDirectories "C:\\test\\file" == ["C:\\", "test", "file"]
Valid x => joinPath (splitDirectories x) `equalFilePath` x
splitDirectories "" == []
Windows: splitDirectories "C:\\test\\\\\\file" == ["C:\\", "test", "file"]
splitDirectories "/test///file" == ["/","test","file"]exceptions
catchError :: MonadError e m => m a -> (e -> m a) -> m a #
A handler function to handle previous errors and return to normal execution. A common idiom is:
do { action1; action2; action3 } `catchError` handlerwhere the action functions can call throwError.
Note that handler and the do-block must have the same return type.
throwError :: MonadError e m => e -> m a #
Is used within a monadic computation to begin exception processing.
Extras
Lens
traverseOf :: Applicative f => ((a -> f b) -> s -> f t) -> (a -> f b) -> s -> f t #