Skip to content

Instantly share code, notes, and snippets.

@maoe
Created May 18, 2020 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maoe/8c8e38a604d3ec01621c431220412a43 to your computer and use it in GitHub Desktop.
Save maoe/8c8e38a604d3ec01621c431220412a43 to your computer and use it in GitHub Desktop.
{-# LANGUAGE BangPatterns #-}
module Main where
import Data.Char
import Data.List
import Data.List.Split
import Criterion.Main
commafyFoldr :: String -> String
commafyFoldr = snd . foldr f (0 :: Int, "")
where
f c (!m, cs)
| m `mod` 3 == 0
, m /= 0 = (m + 1, c:',':cs)
| otherwise = (m + 1, c:cs)
commafySplit :: String -> String
commafySplit = reverse . intercalate "," . chunksOf 3 . reverse
main :: IO ()
main = defaultMain
[ env setup $ \input -> bgroup "commafy"
[ bench "foldr" $ nf commafyFoldr input
, bench "split" $ nf commafySplit input
]
]
setup :: IO String
setup = return "0123456789"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment