packages feed

serokell-util 0.5.1 → 0.5.2

raw patch · 3 files changed

+49/−6 lines, 3 files

Files

serokell-util.cabal view
@@ -1,5 +1,5 @@ name:                serokell-util-version:             0.5.1+version:             0.5.2 synopsis:            General-purpose functions by Serokell homepage:            https://github.com/serokell/serokell-util license:             MIT
src/Serokell/Util/Common.hs view
@@ -7,12 +7,14 @@        , indexedSubList        , subList        , allDistinct+       , chunksOf        ) where  import           Control.Monad.State (evalState, get, modify)-import           Data.List           (sort, genericDrop, genericIndex, genericLength,-                                      genericTake)+import           Data.List           (genericDrop, genericIndex, genericLength,+                                      genericTake, sort) import           Data.Maybe          (fromMaybe)+import           GHC.Exts            (build)  -- | Enumerate function is analogous to python's enumerate. It -- takes sequences of values and returns sequence of pairs where the@@ -87,3 +89,20 @@ allDistinct xs = and $ zipWith (/=) sorted (drop 1 sorted)   where     sorted = sort xs++-- | @'chunksOf' n@ splits a list into length-n pieces.  The last+--   piece will be shorter if @n@ does not evenly divide the length of+--   the list.  If @n <= 0@, @'chunksOf' n l@ returns an infinite list+--   of empty lists.  For example:+--+-- >>> chunksOf 3 [1..7]+-- [[1,2,3],[4,5,6],[7]]+--+-- >>> chunksOf 3 []+-- []+chunksOf :: Int -> [e] -> [[e]]+chunksOf i ls = map (take i) (build (splitter ls))+  where+    splitter :: [e] -> ([e] -> a -> a) -> a -> a+    splitter [] _ n = n+    splitter l c n  = l `c` splitter (drop i l) c n
src/Serokell/Util/Text.hs view
@@ -1,4 +1,5 @@-{-# LANGUAGE GADTs #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE GADTs            #-}  -- | Utility functions to work with `text` and `text-format`. Feel -- free to add more if you need. Some functions have two versions, `'`@@ -20,6 +21,7 @@        , tripleF        , listJson        , listJsonIndent+       , listChunkedJson        , listCsv        , mapJson @@ -29,6 +31,7 @@        , listBuilder        , listBuilderJSON        , listBuilderJSONIndent+       , listChunkedBuilderJson        , listBuilderCSV        , mapBuilder        , mapBuilderJson@@ -47,6 +50,8 @@        , readUnsignedDecimal        ) where +import qualified Universum                        as U+ import qualified Data.Text                        as T import           Data.Text.Buildable              (Buildable (build)) import qualified Data.Text.Format                 as F@@ -57,9 +62,10 @@ import           Data.Text.Lazy.Builder.RealFloat (FPFormat (Exponent, Fixed, Generic)) import qualified Data.Text.Lazy.Builder.RealFloat as B import qualified Data.Text.Read                   as T-import           Formatting                       (fixed, sformat, later, Format)-import           GHC.Exts                         (IsList(..))+import           Formatting                       (Format, fixed, later, sformat)+import           GHC.Exts                         (IsList (..)) import           Prelude                          hiding (show, showList)+import           Serokell.Util.Common             (chunksOf)  show :: Buildable a      => a -> LT.Text@@ -108,6 +114,11 @@ listJsonIndent :: (Foldable t, Buildable a) => Word -> Format r (t a -> r) listJsonIndent = later . listBuilderJSONIndent +listChunkedJson+    :: (U.Container l, Buildable (U.Element l))+    => Int -> Format r (l -> r)+listChunkedJson chunkSize = later $ listChunkedBuilderJson chunkSize+ listCsv :: (Foldable t, Buildable a) => Format r (t a -> r) listCsv = later listBuilderCSV @@ -164,6 +175,19 @@           LT.replicate (fromIntegral indent)                        " "         delimiter = ",\n" `LT.append` spaces++-- | Like listBuilderJSON. but prints per @chunkSize@ elements on a line.+listChunkedBuilderJson+    :: (U.Container l, Buildable (U.Element l))+    => Int -> l -> B.Builder+listChunkedBuilderJson chunkSize values+    | U.null values = "[]"+    | otherwise =+        _listBuilder "[" "" (newline U.<> "]") $+        _listBuilder newline ", " "" <$>+        chunksOf chunkSize (U.toList values)+  where+    newline = "\n    "  -- | Prints comma separated values listBuilderCSV