util 0.1.8.0 → 0.1.9.0
raw patch · 3 files changed
+20/−9 lines, 3 files
Files
- Util.hs +13/−5
- Util/Bits.hs +6/−3
- util.cabal +1/−1
Util.hs view
@@ -7,7 +7,7 @@ import Data.Foldable hiding (maximumBy, minimumBy) import Data.Function (flip) import Data.Functor.Classes-import Data.List.NonEmpty (NonEmpty (..), (<|))+import Data.List.NonEmpty (NonEmpty (..)) import qualified Data.List.NonEmpty as NE import Data.Maybe import Data.Semigroup@@ -42,7 +42,7 @@ apMA f = join ∘ ap f ∘ pure whileJust :: (Alternative f, Monad m) => m (Maybe a) -> (a -> m b) -> m (f b)-whileJust mmx f = mmx >>= maybe (pure empty) (f >=> (<$> whileJust mmx f) ∘ (<|>) ∘ pure)+whileJust mmx f = mmx >>= maybe (pure empty) (\ x -> (<|) <$> f x <*> whileJust mmx f) untilJust :: Monad m => m (Maybe a) -> m a untilJust mmx = mmx >>= maybe (untilJust mmx) pure@@ -86,11 +86,11 @@ replicate :: Alternative f => Natural -> a -> f a replicate 0 _ = empty-replicate n a = pure a <|> replicate (pred n) a+replicate n a = a <| replicate (pred n) a replicateA :: (Applicative p, Alternative f) => Natural -> p a -> p (f a) replicateA 0 _ = pure empty-replicateA n a = (<|>) . pure <$> a <*> replicateA (pred n) a+replicateA n a = (<|) <$> a <*> replicateA (pred n) a mtimesA :: (Applicative p, Semigroup a, Monoid a) => Natural -> p a -> p a mtimesA n = unAp . stimes n . Ap@@ -140,4 +140,12 @@ iterateM :: Monad m => Natural -> (a -> m a) -> a -> m (NonEmpty a) iterateM 0 _ x = pure (x:|[])-iterateM k f x = (x <|) <$> (f x >>= iterateM (pred k) f)+iterateM k f x = (x NE.<|) <$> (f x >>= iterateM (pred k) f)++infixl 3 <|, |>++(<|) :: Alternative f => a -> f a -> f a+x <| xs = pure x <|> xs++(|>) :: Alternative f => f a -> a -> f a+xs |> x = xs <|> pure x
Util/Bits.hs view
@@ -1,13 +1,16 @@ module Util.Bits where +import Control.Applicative import Data.Bits import Data.Bool import Prelude ((==), (+), fromIntegral, id) import qualified Prelude+import Util (.&¬) :: Bits a => a -> a -> a a .&¬ b = a .&. complement b -setBits :: (Bits a, Prelude.Integral n) => a -> [n]-setBits = go 0 where go n a | zeroBits == a `shiftR` fromIntegral n = []- | True = bool id (n:) (testBit a (fromIntegral n)) (go (n+1) a)+setBits :: (Bits a, Prelude.Integral n, Alternative f) => a -> f n+setBits = altMap pure ∘ go 0+ where go n a | zeroBits == a `shiftR` fromIntegral n = []+ | True = bool id (n:) (testBit a (fromIntegral n)) (go (n+1) a)
util.cabal view
@@ -1,5 +1,5 @@ name: util-version: 0.1.8.0+version: 0.1.9.0 synopsis: Utilities -- description: license: BSD3