{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Data.Measure (
module Data.Measure.Class,
(<=),
(>=),
drop,
splitAt,
take,
)
where
import Data.Measure.Class
import qualified Prelude
infix 4 <=, >=
(<=) :: Measure a => a -> a -> Prelude.Bool
a
x <= :: forall a. Measure a => a -> a -> Bool
<= a
y = a
x forall a. Eq a => a -> a -> Bool
Prelude.== forall a. Measure a => a -> a -> a
min a
x a
y
(>=) :: Measure a => a -> a -> Prelude.Bool
a
x >= :: forall a. Measure a => a -> a -> Bool
>= a
y = a
x forall a. Eq a => a -> a -> Bool
Prelude.== forall a. Measure a => a -> a -> a
max a
x a
y
splitAt :: Measure a => (e -> a) -> a -> [e] -> ([e], [e])
splitAt :: forall a e. Measure a => (e -> a) -> a -> [e] -> ([e], [e])
splitAt e -> a
measure a
limit =
a -> [e] -> [e] -> ([e], [e])
go forall a. Measure a => a
zero []
where
go :: a -> [e] -> [e] -> ([e], [e])
go !a
tot [e]
acc = \case
[] -> (forall a. [a] -> [a]
Prelude.reverse [e]
acc, [])
e
e : [e]
es ->
if a
tot' forall a. Measure a => a -> a -> Bool
<= a
limit
then a -> [e] -> [e] -> ([e], [e])
go a
tot' (e
e forall a. a -> [a] -> [a]
: [e]
acc) [e]
es
else (forall a. [a] -> [a]
Prelude.reverse [e]
acc, e
e forall a. a -> [a] -> [a]
: [e]
es)
where
tot' :: a
tot' = forall a. Measure a => a -> a -> a
plus a
tot (e -> a
measure e
e)
take :: Measure a => (e -> a) -> a -> [e] -> [e]
take :: forall a e. Measure a => (e -> a) -> a -> [e] -> [e]
take e -> a
measure a
limit =
a -> [e] -> [e]
go forall a. Measure a => a
zero
where
go :: a -> [e] -> [e]
go !a
tot = \case
[] -> []
e
e : [e]
es ->
if a
tot' forall a. Measure a => a -> a -> Bool
<= a
limit
then e
e forall a. a -> [a] -> [a]
: a -> [e] -> [e]
go a
tot' [e]
es
else []
where
tot' :: a
tot' = forall a. Measure a => a -> a -> a
plus a
tot (e -> a
measure e
e)
drop :: Measure a => (e -> a) -> a -> [e] -> [e]
drop :: forall a e. Measure a => (e -> a) -> a -> [e] -> [e]
drop e -> a
measure a
limit =
a -> [e] -> [e]
go forall a. Measure a => a
zero
where
go :: a -> [e] -> [e]
go !a
tot = \case
[] -> []
e
e : [e]
es ->
if a
tot' forall a. Measure a => a -> a -> Bool
<= a
limit
then a -> [e] -> [e]
go a
tot' [e]
es
else e
e forall a. a -> [a] -> [a]
: [e]
es
where
tot' :: a
tot' = forall a. Measure a => a -> a -> a
plus a
tot (e -> a
measure e
e)