2019-09-07 20:49:59 +02:00
|
|
|
{-# LANGUAGE BangPatterns #-}
|
|
|
|
|
2019-08-31 19:17:27 +02:00
|
|
|
module Xanthous.Util
|
|
|
|
( EqEqProp(..)
|
|
|
|
, EqProp(..)
|
2019-09-07 20:49:59 +02:00
|
|
|
, foldlMapM
|
|
|
|
, foldlMapM'
|
|
|
|
, between
|
2019-08-31 19:17:27 +02:00
|
|
|
) where
|
|
|
|
|
2019-09-07 20:49:59 +02:00
|
|
|
import Xanthous.Prelude hiding (foldr)
|
2019-08-31 19:17:27 +02:00
|
|
|
|
|
|
|
import Test.QuickCheck.Checkers
|
2019-09-07 20:49:59 +02:00
|
|
|
import Data.Foldable (foldr)
|
2019-08-31 19:17:27 +02:00
|
|
|
|
|
|
|
newtype EqEqProp a = EqEqProp a
|
|
|
|
deriving newtype Eq
|
|
|
|
|
|
|
|
instance Eq a => EqProp (EqEqProp a) where
|
|
|
|
(=-=) = eq
|
2019-09-07 20:49:59 +02:00
|
|
|
|
|
|
|
foldlMapM :: forall g b a m. (Foldable g, Monoid b, Applicative m) => (a -> m b) -> g a -> m b
|
|
|
|
foldlMapM f = foldr f' (pure mempty)
|
|
|
|
where
|
|
|
|
f' :: a -> m b -> m b
|
|
|
|
f' x = liftA2 mappend (f x)
|
|
|
|
|
|
|
|
-- Strict in the monoidal accumulator. For monads strict
|
|
|
|
-- in the left argument of bind, this will run in constant
|
|
|
|
-- space.
|
|
|
|
foldlMapM' :: forall g b a m. (Foldable g, Monoid b, Monad m) => (a -> m b) -> g a -> m b
|
|
|
|
foldlMapM' f xs = foldr f' pure xs mempty
|
|
|
|
where
|
|
|
|
f' :: a -> (b -> m b) -> b -> m b
|
|
|
|
f' x k bl = do
|
|
|
|
br <- f x
|
|
|
|
let !b = mappend bl br
|
|
|
|
k b
|
|
|
|
|
|
|
|
between
|
|
|
|
:: Ord a
|
|
|
|
=> a -- ^ lower bound
|
|
|
|
-> a -- ^ upper bound
|
|
|
|
-> a -- ^ scrutinee
|
|
|
|
-> Bool
|
|
|
|
between lower upper x = x >= lower && x <= upper
|