Copyright | (c) Eric Mertens 2018-2021 |
---|---|
License | ISC |
Maintainer | emertens@gmail.com |
Safe Haskell | None |
Language | Haskell2010 |
Various helper functions for common operations needed in Advent of Code problems.
Synopsis
- count :: (Foldable f, Eq a) => a -> f a -> Int
- countBy :: Foldable f => (a -> Bool) -> f a -> Int
- same :: Foldable t => Eq a => t a -> Bool
- pickOne :: [a] -> [(a, [a])]
- ordNub :: Ord a => [a] -> [a]
- minimumMaybe :: Ord a => [a] -> Maybe a
- maximumMaybe :: Ord a => [a] -> Maybe a
- counts :: (Foldable f, Ord a) => f a -> Map a Int
- compose :: [a -> a] -> a -> a
- chunks :: Int -> [a] -> [[a]]
- löb :: Functor f => f (f a -> a) -> f a
- möb :: (((a -> b) -> b) -> c -> a) -> c -> a
- arrIx :: (IArray a e, Ix i) => a i e -> i -> Maybe e
- times :: Int -> (a -> a) -> a -> a
- uniqueAssignment :: (Traversable t, Ord a) => t (Set a) -> [t a]
- fromDigits :: Integral a => a -> [a] -> a
- toDigits :: Integral a => a -> a -> [a]
- power :: (a -> a -> a) -> a -> Integer -> a
- scanlM :: (Traversable t, Monad m) => (b -> a -> m (c, a)) -> a -> t b -> m (t c, a)
Documentation
count :: (Foldable f, Eq a) => a -> f a -> Int Source #
Count the number of elements in a foldable value that satisfy a predicate.
countBy :: Foldable f => (a -> Bool) -> f a -> Int Source #
Count the number of elements in a foldable value that satisfy a predicate.
same :: Foldable t => Eq a => t a -> Bool Source #
Return true when the whole list is comprised of equal elements.
>>>
same [1,1,1]
True>>>
same []
True>>>
same [1]
True>>>
same [1,1,2]
False
pickOne :: [a] -> [(a, [a])] Source #
Returns a list of ways to select an element from a list without replacement.
>>>
pickOne []
[]>>>
pickOne [1]
[(1,[])]>>>
pickOne [1,2,3]
[(1,[2,3]),(2,[1,3]),(3,[1,2])]
minimumMaybe :: Ord a => [a] -> Maybe a Source #
Compute the minimum element of a list or return Nothing if it is empty.
>>>
minimumMaybe []
Nothing>>>
minimumMaybe [2,1,3]
Just 1
maximumMaybe :: Ord a => [a] -> Maybe a Source #
Compute the maximum element of a list or return Nothing if it is empty.
>>>
maximumMaybe []
Nothing>>>
maximumMaybe [2,1,3]
Just 3
counts :: (Foldable f, Ord a) => f a -> Map a Int Source #
Compute the number of occurrences of the elements in a given list.
>>>
counts "bababc"
fromList [('a',2),('b',3),('c',1)]
compose :: [a -> a] -> a -> a Source #
Compose a list of functions together
>>>
compose [ (1:), (2:), (3:) ] []
[1,2,3]
chunks :: Int -> [a] -> [[a]] Source #
Split list into chunks. The last chunk might be incomplete.
>>>
chunks 3 [1..9]
[[1,2,3],[4,5,6],[7,8,9]]
>>>
chunks 3 [1..7]
[[1,2,3],[4,5,6],[7]]
>>>
chunks 3 []
[]
arrIx :: (IArray a e, Ix i) => a i e -> i -> Maybe e Source #
Index an array returning Nothing
if the index is out of bounds.
:: (Traversable t, Ord a) | |
=> t (Set a) | element must map to one of the corresponding set members |
-> [t a] | possible assignments |
Given a list of constraints such that each constraint identifies a unique variable and the set of assignments it can have, this computes assignments of those variables such that no two input variables are assigned the same value.
>>>
uniqueAssignment [Set.fromList "ab", Set.fromList "bc"]
["ab","ac","bc"]
fromDigits :: Integral a => a -> [a] -> a Source #
Convert a big-endian list of digits to a single number.
>>>
fromDigits 10 [1,2,3,4]
1234
>>>
fromDigits 2 [12]
12
>>>
fromDigits 10 []
0
toDigits :: Integral a => a -> a -> [a] Source #
Convert a number to a list of digits in a given radix.
>>>
toDigits 2 12
[1,1,0,0]
>>>
toDigits 10 1234
[1,2,3,4]
>>>
toDigits 10 0
[]
power :: (a -> a -> a) -> a -> Integer -> a Source #
Efficient exponentiation using an associative operator
>>>
power (+) 1 10
10
>>>
power (*) 2 10
1024
scanlM :: (Traversable t, Monad m) => (b -> a -> m (c, a)) -> a -> t b -> m (t c, a) Source #