Skip to content

Commit 4d9e181

Browse files
committed
created pctElems; removed getPctExts
1 parent 1db441b commit 4d9e181

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

app/Main.hs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
module Main where
22

3-
import Control.Arrow (Arrow (first, (&&&)))
4-
import Data.Functor ((<&>))
3+
import Control.Arrow (Arrow ((&&&)))
54
import Data.List (genericLength, group, sort)
65
import System.Directory (listDirectory)
76
import System.FilePath (takeExtension)
87
import Text.Printf (printf)
98

109
main :: IO ()
11-
main = printExts . getPctExts =<< listDirectory "."
10+
main = printExts . pctElems . filter (/="") . fmap takeExtension =<< listDirectory "."
1211
where
1312
printExts :: [(Double, String)] -> IO ()
14-
printExts = mapM_ . uncurry . printf $ "%.2f %% %s\n"
13+
printExts = mapM_ . uncurry . printf $ "%6.02f %% %s\n"
1514

16-
-- | Get percentage of file extensions
17-
getPctExts :: [FilePath] -> [(Double, String)]
18-
getPctExts = fmap takeExtension
19-
<&> filter (/="")
20-
<&> group . sort
21-
<&> fmap (genericLength &&& head)
22-
<&> id &&& genericLength
23-
<&> \(ys, len) -> first ((*100) . (/len)) <$> ys
15+
-- | Get percentage of each element in a list
16+
pctElems :: (Ord a, Fractional b) => [a] -> [(b, a)]
17+
pctElems xs = (calcPct . genericLength &&& head) <$> group (sort xs)
18+
where
19+
len = genericLength xs
20+
calcPct = (*100) . (/len)

0 commit comments

Comments
 (0)