summaryrefslogtreecommitdiffstats
path: root/Utils.hs
blob: a14be89d81f58485b47ae433070badfd553f9c69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module Utils where

import Control.Exception
import System.Directory
import System.IO


withTempFile :: FilePath -> String -> ((FilePath, Handle) -> IO a) -> IO a
withTempFile tmpdir template =
    bracket (openTempFile tmpdir template) (removeFile . fst)


mintercalate :: Monoid b => b -> [b] -> b
mintercalate c (h:t) = foldl (\acc x -> acc <> c <> x) h t
mintercalate _ [] = mempty


padl :: Int -> a -> [a] -> [a]
padl n c s =
    if length s < n
        then padl n c (c:s)
        else s

padr :: Int -> a -> [a] -> [a]
padr n c s =
    if length s < n
        then padr n c (s ++ [c])
        else s