cli-utils-4.1.0
LicenseBSD3
Maintainertbidne@gmail.com
Safe HaskellNone
LanguageHaskell2010

Common.Utils

Description

Exports utility functions

Synopsis

Documentation

diffTime :: TimeSpec -> TimeSpec -> RNonNegative Int Source #

For given \(x, y\), returns the absolute difference \(|x - y|\).

divWithRem :: Integral a => RNonNegative a -> RPositive a -> (a, a) Source #

For \(n \ge 0, d > 0\), returns non-negative \((e, r)\) such that

\[ \begin{align} de + r = n \\ r < n \\ \end{align} \]

eitherCompose :: (a -> Either b c) -> (c -> Either d e) -> a -> Either () e Source #

Composes Either functions.

eitherComposeMay :: (a -> Either b c) -> (c -> Either d e) -> a -> Maybe e Source #

Composition of eitherToMaybe to eitherCompose.

eitherJoin :: Either a (Either b c) -> Either () c Source #

Joins a composed Either in the natural way.

eitherToMaybe :: Either a b -> Maybe b Source #

Natural transformation from Either a to Maybe.

formatSeconds :: RNonNegative Int -> Text Source #

For \(n \ge 0\) seconds, returns a Text description of the minutes and seconds.

matchAndStrip :: Eq a => [a] -> [a] -> Maybe [a] Source #

Flipped version of startsWith. Useful with ViewPatterns, e.g.

   :set -XViewPatterns

   parseArg :: String -> Either String SomeType
   parseArg (matchAndStrip "--val=" -> Just rest) = parseVal rest
   parseArg (matchAndStrip "--other=" -> Just rest) = parseOther rest
   parseArg _ = Left "did not match!"

monoBimap :: Bifunctor f => (a -> b) -> f a a -> f b b Source #

Convenience function for mapping the same function over a monomorphic bifunctor.

showToText :: Show a => a -> Text Source #

Transforms a showable to Text.

startsWith :: Eq a => [a] -> [a] -> Maybe [a] Source #

Determines if the second parameter is a prefix of the first, returns the rest if so. That is,

\[ \newcommand\doubleplus{+\kern-1.3ex+\kern0.8ex} \mathrm{startsWith}(xs, ys) = \begin{cases} \mathrm{Just\,} zs, &xs = ys \doubleplus zs \\ \mathrm{Nothing}, &\mathrm{otherwise} \end{cases} \]

Can be called infix, e.g.

  "hello world" `startsWith` "hello" --> Just " world"