Created
May 18, 2020 14:05
-
-
Save maoe/8c8e38a604d3ec01621c431220412a43 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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