Safe Haskell | None |
---|---|
Language | Haskell2010 |
Common utilities.
- enumerate :: (Num i, Enum i, Traversable t) => t a -> t (i, a)
- indexModulo :: Integral i => [a] -> i -> a
- indexModuloMay :: Integral i => [a] -> i -> Maybe a
- indexedSubList :: Integral i => (i, i) -> [a] -> [(i, a)]
- subList :: Integral i => (i, i) -> [a] -> [a]
- allDistinct :: Ord a => [a] -> Bool
Documentation
enumerate :: (Num i, Enum i, Traversable t) => t a -> t (i, a) Source #
Enumerate function is analogous to python's enumerate. It
takes sequences of values and returns sequence of pairs where the
first element is index and the second one is corresponding value.
It's roughly equivalent to `zip [0..]`.
> enumerate Hello = [(0,H
),(1,e
),(2,l
),(3,l
),(4,o
)]
indexModulo :: Integral i => [a] -> i -> a Source #
Returns element of a list with given index modulo length of list. Raises error if list is empty. Examples: indexModulo [1, 2, 3] 10 = 2 indexModulo [1, 0] 2 = 1 indexModulo [] 199 = error
indexModuloMay :: Integral i => [a] -> i -> Maybe a Source #
Behaves like indexModulo
but uses Maybe to report error
(i. e. empty list).
indexedSubList :: Integral i => (i, i) -> [a] -> [(i, a)] Source #
indexedSubList (lo, hi) returns sublist of given list with indices in [max lo 0, hi). If the lower bound is negative, 0 will in its place. If both indices are negative, the empty list is returned. Examples: indexedSubList (2, 3) [0, 5, 10] = [(2, 10)] indexedSubList (0, 2) [0, 5, 10] = [(0, 0), (1, 5)] indexedSubList (0, 0) [0, 1, 11, 111] = [] indexedSubList (2000, 1000) [55, 47, 0, 1, 11, 111] = [] indexedSubList (-3, 3) [10, 11, 12, 13, 14] = [(0,10),(1,11),(2,12)] indexedSubList (-6, -1) [1,2,3] = []
allDistinct :: Ord a => [a] -> Bool Source #
Determine whether all elements in a list are distinct.
>>>
allDistinct [1,2,3]
True>>>
allDistinct [1,3,3]
False
Naturally, all elements in an empty list are distinct:
>>>
allDistinct []
True