chunksOf a string

Filed under: programming

I love this function chunksOf! It comes from Data.Text and Data.List.Split

Example of its usage in GHCI:

⇒  ghci
GHCi, version 7.8.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> import Prelude
Prelude> import Data.Text
Prelude Data.Text> chunksOf 2 (pack "0123456789")
Loading package array-0.5.0.0 ... linking ... done.
Loading package deepseq-1.3.0.2 ... linking ... done.
Loading package bytestring-0.10.4.0 ... linking ... done.
Loading package text-1.2.0.3 ... linking ... done.
["01","23","45","67","89"]
Prelude Data.Text>

The function from Data.List takes any type list, which makes it less confusing.

Prelude Data.Text> import Data.List.Split
Prelude Data.Text Data.List.Split> Data.List.Split.chunksOf 2 "0123456789"
Loading package split-0.2.2 ... linking ... done.
["01","23","45","67","89"]
Prelude Data.Text Data.List.Split>