haccepted-0.1.0.0: Data structures and algorithms
Safe HaskellNone
LanguageHaskell2010

Misc

Description

Miscellaneous functions/recipes

Synopsis

Documentation

pairs :: [a] -> [(a, a)] Source #

Generates distinct pairs of elements from a list. O(n^2).

fArray :: (IArray a e, Ix i) => (i, i) -> (i -> e) -> a i e Source #

Generates an Array from bounds and a function. O(n) assuming f takes O(1).

chunksOf :: Int -> [a] -> [[a]] Source #

Splits a list into chunks of fixed size. O(n).

replicateL :: Int -> [a] -> [a] Source #

Replicates a list n times. O(nm) where m is the length of the list.

unique :: Eq a => [a] -> [a] Source #

Eliminates consecutive duplicate elements. O(n).

foldExclusive :: (b -> a -> b) -> b -> [a] -> [b] Source #

Folds strictly such that the ith element of the output list contains the fold of all elements in the input list except for the ith element. The fold function f must be commutative, in the sense that (b f a1) f a2 = (b f a2) f a1 f is called O(n log n) times.

modifyArray :: (MArray a e m, Ix i) => a i e -> i -> (e -> e) -> m () Source #

Modifies an element in a mutable array.

modifyArray' :: (MArray a e m, Ix i) => a i e -> i -> (e -> e) -> m () Source #

Modifies an element in a mutable array. Strict version.

ifoldr :: Foldable f => (Int -> a -> b -> b) -> b -> f a -> b Source #

foldr with an index, starting at 0.

ifoldl' :: Foldable f => (b -> Int -> a -> b) -> b -> f a -> b Source #

foldl' with an index, starting at 0.

foldMComp :: Monad m => (b -> a -> b) -> (c -> m a) -> b -> c -> m b Source #

Compose a strict left fold function with a mapM function. Useful for foldMs. foldM (f foldMComp g) z = fmap (foldl' f z) . mapM g

farthest :: (a -> Maybe a) -> a -> a Source #

Repeatedly applies a function to a value and returns the value which gives back Nothing.

foldTree' :: (a -> b -> c) -> (c -> b -> b) -> b -> Tree a -> c Source #

Folds a tree. This does the same job as Data.Tree.foldTree but with different fold functions, which may be preferable in cases such as CPS folds.

class Semigroup a => Commutative a Source #

A semigroup where the operation (<>) is commutative. a <> b = b <> a

Instances

Instances details
Num a => Commutative (Sum a) Source # 
Instance details

Defined in Misc

class Monoid a => Group a where Source #

A monoid where elements have inverses. a <> invert a = mempty invert a <> a = mempty

Methods

invert :: a -> a Source #

Instances

Instances details
Num a => Group (Sum a) Source # 
Instance details

Defined in Misc

Methods

invert :: Sum a -> Sum a Source #

class Semigroup a => Idempotent a Source #

A semigroup where the elements are idempotent. a <> a = a

Instances

Instances details
Ord a => Idempotent (Min a) Source # 
Instance details

Defined in Misc

Ord a => Idempotent (Max a) Source # 
Instance details

Defined in Misc

Idempotent (First a) Source # 
Instance details

Defined in Misc

Idempotent (Last a) Source # 
Instance details

Defined in Misc

class (Monoid u, Monoid a) => Action u a where Source #

A right monoid action of u on a. (x act u1) act u2 = x act (u1 <> u2) x act mempty = x

Methods

act :: a -> u -> a Source #

bitLength :: FiniteBits b => b -> Int Source #

The number of bits required to represent the value.

unsafeBit :: (Bits a, Num a) => Int -> a Source #

Just like bit, but skips the check that the index is in [0 .. size-1].

odds :: [a] -> [a] Source #

The elements at odd positions of a list.

evens :: [a] -> [a] Source #

The elements at even positions of a list.

orM :: Monad m => m Bool -> m Bool -> m Bool Source #

Short-circuiting monadic ||.

andM :: Monad m => m Bool -> m Bool -> m Bool Source #

Short-circuiting monadic &&.

anyM :: (Monad m, Foldable f) => (a -> m Bool) -> f a -> m Bool Source #

Monadic version of any.

allM :: (Monad m, Foldable f) => (a -> m Bool) -> f a -> m Bool Source #

Monadic version of all.

minimumByMaybe :: Foldable f => (a -> a -> Ordering) -> f a -> Maybe a Source #

maximumByMaybe :: Foldable f => (a -> a -> Ordering) -> f a -> Maybe a Source #