| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Ouroboros.Consensus.Util
Description
Miscellaneous utilities
Synopsis
- data Dict :: Constraint -> Type where
- class Empty a
- data Some (f :: k -> Type) where
- data SomePair (f :: k -> Type) (g :: k -> Type) where
- data SomeSecond (f :: Type -> Type -> Type) a where
- SomeSecond :: !(f a b) -> SomeSecond f a
- class ShowProxy (p :: k) where
- mustBeRight :: Either Void a -> a
- foldlM' :: forall m a b. Monad m => (b -> a -> m b) -> b -> [a] -> m b
- repeatedly :: (a -> b -> b) -> [a] -> b -> b
- repeatedlyM :: Monad m => (a -> b -> m b) -> [a] -> b -> m b
- nTimes :: forall a. (a -> a) -> Word64 -> a -> a
- nTimesM :: forall m a. Monad m => (a -> m a) -> Word64 -> a -> m a
- chunks :: Int -> [a] -> [[a]]
- pickOne :: [a] -> [([a], a, [a])]
- markLast :: [a] -> [Either a a]
- takeLast :: Word64 -> [a] -> [a]
- dropLast :: Word64 -> [a] -> [a]
- firstJust :: forall a b f. Foldable f => (a -> Maybe b) -> f a -> Maybe b
- allEqual :: Eq a => [a] -> Bool
- takeUntil :: (a -> Bool) -> [a] -> [a]
- groupOn :: forall a b. Eq b => (a -> b) -> [a] -> [(b, [a])]
- groupSplit :: forall a b c. Eq b => (a -> (b, c)) -> [a] -> [(b, [c])]
- splits :: [a] -> [([a], a, [a])]
- lastMaybe :: [a] -> Maybe a
- safeMaximum :: Ord a => [a] -> Maybe a
- safeMaximumBy :: (a -> a -> Ordering) -> [a] -> Maybe a
- safeMaximumOn :: Ord b => (a -> b) -> [a] -> Maybe a
- hashFromBytesE :: forall h a. (HashAlgorithm h, HasCallStack) => ByteString -> Hash h a
- hashFromBytesShortE :: forall h a. (HashAlgorithm h, HasCallStack) => ShortByteString -> Hash h a
- byteStringChunks :: Int -> ByteString -> [ByteString]
- lazyByteStringChunks :: Int -> ByteString -> [ByteString]
- whenJust :: Applicative f => Maybe a -> (a -> f ()) -> f ()
- checkThat :: (Show a, Monad m) => String -> (a -> Bool) -> a -> m ()
- allDisjoint :: forall a. Ord a => [Set a] -> Bool
- (.:) :: (y -> z) -> (x0 -> x1 -> y) -> x0 -> x1 -> z
- (..:) :: (y -> z) -> (x0 -> x1 -> x2 -> y) -> x0 -> x1 -> x2 -> z
- (...:) :: (y -> z) -> (x0 -> x1 -> x2 -> x3 -> y) -> x0 -> x1 -> x2 -> x3 -> z
- (....:) :: (y -> z) -> (x0 -> x1 -> x2 -> x3 -> x4 -> y) -> x0 -> x1 -> x2 -> x3 -> x4 -> z
- (.....:) :: (y -> z) -> (x0 -> x1 -> x2 -> x3 -> x4 -> x5 -> y) -> x0 -> x1 -> x2 -> x3 -> x4 -> x5 -> z
- pairFst :: Product f g a -> f a
- pairSnd :: Product f g a -> g a
- fib :: Word64 -> Word64
- eitherToMaybe :: Either a b -> Maybe b
Type-level utility
data SomePair (f :: k -> Type) (g :: k -> Type) where Source #
Pair of functors instantiated to the same existential
data SomeSecond (f :: Type -> Type -> Type) a where Source #
Hide the second type argument of some functor
SomeSecond f a is isomorphic to Some (f a), but is more convenient in
partial applications.
Constructors
| SomeSecond :: !(f a b) -> SomeSecond f a |
Instances
class ShowProxy (p :: k) where Source #
Minimal complete definition
Nothing
Instances
mustBeRight :: Either Void a -> a Source #
Folding variations
repeatedly :: (a -> b -> b) -> [a] -> b -> b Source #
repeatedlyM :: Monad m => (a -> b -> m b) -> [a] -> b -> m b Source #
nTimes :: forall a. (a -> a) -> Word64 -> a -> a Source #
Apply a function n times. The value of each application is forced.
nTimesM :: forall m a. Monad m => (a -> m a) -> Word64 -> a -> m a Source #
Apply a function n times through a monadic bind. The value of each application is forced.
Lists
pickOne :: [a] -> [([a], a, [a])] Source #
All possible ways to pick on element from a list, preserving order
pickOne [1,2,3] = [ ([], 1, [2, 3])
, ([1], 2, [3])
, ([1,2], 3, [])
]takeUntil :: (a -> Bool) -> [a] -> [a] Source #
Take items until the condition is true. If the condition is true for an item, include that item as the last item in the returned list. If the condition was never true, the original list is returned.
takeUntil (== 3) [1,2,3,4]
- 1,2,3
- > takeUntil (== 2) [0,1,0]
- 0,1,0
- > takeUntil (== 2) [2,2,3]
- 2
groupOn :: forall a b. Eq b => (a -> b) -> [a] -> [(b, [a])] Source #
Variation on groupBy that records the matched element
groupOn signum [-3..3] == [ (-1, [-3, -2,-1]) , ( 0, [0]) , ( 1, [1, 2, 3]) ]
groupSplit :: forall a b c. Eq b => (a -> (b, c)) -> [a] -> [(b, [c])] Source #
Generalization of groupOn where we specify both what to compare
and what to collect
splits :: [a] -> [([a], a, [a])] Source #
Focus on one element in the list
E.g.
splits [1..3] == [ ([] , 1 , [2,3]) , ([1] , 2 , [3] ) , ([1,2] , 3 , [] ) ]
Safe variants of existing base functions
safeMaximum :: Ord a => [a] -> Maybe a Source #
safeMaximumBy :: (a -> a -> Ordering) -> [a] -> Maybe a Source #
safeMaximumOn :: Ord b => (a -> b) -> [a] -> Maybe a Source #
Hashes
hashFromBytesE :: forall h a. (HashAlgorithm h, HasCallStack) => ByteString -> Hash h a Source #
Calls hashFromBytes and throws an error if the input is of the wrong
length.
hashFromBytesShortE :: forall h a. (HashAlgorithm h, HasCallStack) => ShortByteString -> Hash h a Source #
Calls hashFromBytesShort and throws an error if the input is of the
wrong length.
Bytestrings
byteStringChunks :: Int -> ByteString -> [ByteString] Source #
lazyByteStringChunks :: Int -> ByteString -> [ByteString] Source #
Monadic utilities
whenJust :: Applicative f => Maybe a -> (a -> f ()) -> f () Source #
Test code
checkThat :: (Show a, Monad m) => String -> (a -> Bool) -> a -> m () Source #
Assertion
Variation on assert for use in testing code.
Sets
allDisjoint :: forall a. Ord a => [Set a] -> Bool Source #
Check that a bunch of sets are all mutually disjoint
Composition
(....:) :: (y -> z) -> (x0 -> x1 -> x2 -> x3 -> x4 -> y) -> x0 -> x1 -> x2 -> x3 -> x4 -> z Source #
(.....:) :: (y -> z) -> (x0 -> x1 -> x2 -> x3 -> x4 -> x5 -> y) -> x0 -> x1 -> x2 -> x3 -> x4 -> x5 -> z Source #
Product
Miscellaneous
eitherToMaybe :: Either a b -> Maybe b Source #