{-# 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 <= :: a -> a -> Bool
<= a
y = a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
Prelude.== a -> a -> a
forall a. Measure a => a -> a -> a
min a
x a
y
(>=) :: Measure a => a -> a -> Prelude.Bool
a
x >= :: a -> a -> Bool
>= a
y = a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
Prelude.== a -> a -> a
forall a. Measure a => a -> a -> a
max a
x a
y
splitAt :: Measure a => (e -> a) -> a -> [e] -> ([e], [e])
splitAt :: (e -> a) -> a -> [e] -> ([e], [e])
splitAt e -> a
measure a
limit =
a -> [e] -> [e] -> ([e], [e])
go a
forall a. Measure a => a
zero []
where
go :: a -> [e] -> [e] -> ([e], [e])
go !a
tot [e]
acc = \case
[] -> ([e] -> [e]
forall a. [a] -> [a]
Prelude.reverse [e]
acc, [])
e
e:[e]
es ->
if a
tot' a -> a -> Bool
forall a. Measure a => a -> a -> Bool
<= a
limit
then a -> [e] -> [e] -> ([e], [e])
go a
tot' (e
e e -> [e] -> [e]
forall a. a -> [a] -> [a]
: [e]
acc) [e]
es
else ([e] -> [e]
forall a. [a] -> [a]
Prelude.reverse [e]
acc, e
ee -> [e] -> [e]
forall a. a -> [a] -> [a]
:[e]
es)
where
tot' :: a
tot' = a -> a -> a
forall a. Measure a => a -> a -> a
plus a
tot (e -> a
measure e
e)
take :: Measure a => (e -> a) -> a -> [e] -> [e]
take :: (e -> a) -> a -> [e] -> [e]
take e -> a
measure a
limit =
a -> [e] -> [e]
go a
forall a. Measure a => a
zero
where
go :: a -> [e] -> [e]
go !a
tot = \case
[] -> []
e
e:[e]
es ->
if a
tot' a -> a -> Bool
forall a. Measure a => a -> a -> Bool
<= a
limit
then e
e e -> [e] -> [e]
forall a. a -> [a] -> [a]
: a -> [e] -> [e]
go a
tot' [e]
es
else []
where
tot' :: a
tot' = a -> a -> a
forall a. Measure a => a -> a -> a
plus a
tot (e -> a
measure e
e)
drop :: Measure a => (e -> a) -> a -> [e] -> [e]
drop :: (e -> a) -> a -> [e] -> [e]
drop e -> a
measure a
limit =
a -> [e] -> [e]
go a
forall a. Measure a => a
zero
where
go :: a -> [e] -> [e]
go !a
tot = \case
[] -> []
e
e:[e]
es ->
if a
tot' a -> a -> Bool
forall a. Measure a => a -> a -> Bool
<= a
limit
then a -> [e] -> [e]
go a
tot' [e]
es
else e
ee -> [e] -> [e]
forall a. a -> [a] -> [a]
:[e]
es
where
tot' :: a
tot' = a -> a -> a
forall a. Measure a => a -> a -> a
plus a
tot (e -> a
measure e
e)