-- | This module provides functions to convert between lists of integers and
--   strings suitable for use in CSV files
module RealDice.Convert.CSV
  ( intsToCSV,
    csvToInts,
  )
where

-- | Remove the square brackets `[]` from the string representation of a list
--   so it can be used as a CSV string

-- | ==== __Examples__
--   >>> intsToCSV [1,2,3]
--   "1,2,3"
intsToCSV :: [Int] -> String
intsToCSV :: [Int] -> String
intsToCSV [Int]
xs = Int -> String -> String
forall a. Int -> [a] -> [a]
drop Int
1 (Int -> String -> String
forall a. Int -> [a] -> [a]
take (String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Int] -> String
forall a. Show a => a -> String
show [Int]
xs) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ([Int] -> String
forall a. Show a => a -> String
show [Int]
xs))

-- | Add square brackets `[]` to a CSV string of integers and make it into
--   a list

-- | ==== __Examples__
--   >>> csvToInts "1,2,3"
--   [1,2,3]
csvToInts :: String -> [Int]
csvToInts :: String -> [Int]
csvToInts String
xs = String -> [Int]
forall a. Read a => String -> a
read (String
"[" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
xs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"]")