module Monomer.Helper where
import Data.Sequence (Seq(..))
import qualified Data.Sequence as Seq
maybeConcat :: Monoid a => [a] -> Maybe a
maybeConcat :: [a] -> Maybe a
maybeConcat [] = Maybe a
forall a. Maybe a
Nothing
maybeConcat [a]
lst = a -> Maybe a
forall a. a -> Maybe a
Just ([a] -> a
forall a. Monoid a => [a] -> a
mconcat [a]
lst)
seqLast :: Seq a -> a
seqLast :: Seq a -> a
seqLast Seq a
seq
| Bool -> Bool
not (Seq a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Seq a
seq) = Seq a -> Int -> a
forall a. Seq a -> Int -> a
Seq.index Seq a
seq (Seq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Seq a
seq Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
| Bool
otherwise = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"Invalid sequence provided to seqLast"
seqStartsWith :: Eq a => Seq a -> Seq a -> Bool
seqStartsWith :: Seq a -> Seq a -> Bool
seqStartsWith Seq a
prefix Seq a
seq = Int -> Seq a -> Seq a
forall a. Int -> Seq a -> Seq a
Seq.take (Seq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Seq a
prefix) Seq a
seq Seq a -> Seq a -> Bool
forall a. Eq a => a -> a -> Bool
== Seq a
prefix
seqCatMaybes :: Seq (Maybe a) -> Seq a
seqCatMaybes :: Seq (Maybe a) -> Seq a
seqCatMaybes Seq (Maybe a)
Empty = Seq a
forall a. Seq a
Empty
seqCatMaybes (Maybe a
x :<| Seq (Maybe a)
xs) = case Maybe a
x of
Just a
val -> a
val a -> Seq a -> Seq a
forall a. a -> Seq a -> Seq a
:<| Seq (Maybe a) -> Seq a
forall a. Seq (Maybe a) -> Seq a
seqCatMaybes Seq (Maybe a)
xs
Maybe a
_ -> Seq (Maybe a) -> Seq a
forall a. Seq (Maybe a) -> Seq a
seqCatMaybes Seq (Maybe a)
xs
maxNumericValue :: (RealFloat a) => a
maxNumericValue :: a
maxNumericValue = a
x where
n :: Int
n = a -> Int
forall a. RealFloat a => a -> Int
floatDigits a
x
b :: Integer
b = a -> Integer
forall a. RealFloat a => a -> Integer
floatRadix a
x
(Int
_, Int
u) = a -> (Int, Int)
forall a. RealFloat a => a -> (Int, Int)
floatRange a
x
x :: a
x = Integer -> Int -> a
forall a. RealFloat a => Integer -> Int -> a
encodeFloat (Integer
bInteger -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^Int
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1) (Int
u Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n)
clamp :: (Ord a) => a -> a -> a -> a
clamp :: a -> a -> a -> a
clamp a
mn a
mx = a -> a -> a
forall a. Ord a => a -> a -> a
max a
mn (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a -> a
forall a. Ord a => a -> a -> a
min a
mx