This commit brings in an HLint configuration file and several recommended modifications such as: * End-of-line extra spaces removal; * Import lines ordering; * Redundant $ removal; * Generalisation of ++ and map to <> and fmap; * Preferring `pure` over `return`; * Removing extraenous extensions. And finally, a `stylish-haskell` helper script that detects if code files are dirty. Can be useful for CI, although manually calling it can be nice if you would rather first implement then beautify.
36 lines
1.1 KiB
Haskell
36 lines
1.1 KiB
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
module Hash2Pub.Utils where
|
|
|
|
import qualified Data.ByteString as BS
|
|
import qualified Data.Set as Set
|
|
|
|
-- |wraps a list into a Maybe, by replacing empty lists with Nothing
|
|
maybeEmpty :: [a] -> Maybe [a]
|
|
maybeEmpty [] = Nothing
|
|
maybeEmpty nonemptyList = Just nonemptyList
|
|
|
|
-- | Chop a list into sublists of i elements. The last sublist might contain
|
|
-- less than i elements.
|
|
chunksOf :: Int -> [a] -> [[a]]
|
|
chunksOf i xs =
|
|
case splitAt i xs of
|
|
(a, []) -> [a]
|
|
(a, b) -> a : chunksOf i b
|
|
|
|
|
|
-- | Chop a 'BS.ByteString' into list of substrings of i elements. The last
|
|
-- substring might contain less than i elements.
|
|
chunkBytes :: Int -> BS.ByteString -> [BS.ByteString]
|
|
chunkBytes i xs =
|
|
case BS.splitAt i xs of
|
|
(a, "") -> [a]
|
|
(a, b) -> a : chunkBytes i b
|
|
|
|
-- | Chop a 'Set.Set' into a list of disjuct subsets of i elements. The last
|
|
-- subset might contain less than i elements.
|
|
chunkSet :: Int -> Set.Set a -> [Set.Set a]
|
|
chunkSet i xs
|
|
| Set.null . snd $ splitSet = [fst splitSet]
|
|
| otherwise = fst splitSet : chunkSet i (snd splitSet)
|
|
where
|
|
splitSet = Set.splitAt i xs
|