37 lines
1.1 KiB
Haskell
37 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
|