Skip to content
Snippets Groups Projects
Commit dd43f7e1 authored by Bodigrim's avatar Bodigrim
Browse files

Avoid partial functions Data.List.init and Data.List.last

parent 5923da3f
Branches master
No related tags found
1 merge request!47Avoid partial functions Data.List.init and Data.List.last
Pipeline #111924 passed
......@@ -2,6 +2,9 @@
-- Module : Trace.Hpc.Draft
-- Description : The subcommand @hpc draft@
-- License : BSD-3-Clause
{-# LANGUAGE CPP #-}
module Trace.Hpc.Draft (draftPlugin) where
import qualified Data.Map as Map
......@@ -14,6 +17,13 @@ import Trace.Hpc.Tix
import Trace.Hpc.Util
import Trace.Hpc.Utils
#if MIN_VERSION_base(4,19,0)
import Data.List (unsnoc)
#else
unsnoc :: [a] -> Maybe ([a], a)
unsnoc = foldr (\x -> Just . maybe ([], x) (\(~(a, b)) -> (x : a, b))) Nothing
#endif
------------------------------------------------------------------------------
draftOptions :: FlagOptSeq
......@@ -89,7 +99,7 @@ makeDraft hpcflags tix = do
showPleaseTick d (TickFun str pos) =
spaces d
++ "tick function \""
++ last str
++ maybe "" snd (unsnoc str)
++ "\" "
++ "on line "
++ show (firstLine pos)
......
......@@ -7,6 +7,9 @@
module Trace.Hpc.Utils where
import Data.Char (isControl)
import Data.List (intercalate)
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.NonEmpty as NE
import qualified Data.Map as Map
import qualified Data.Set as Set
import Numeric (showHex)
......@@ -30,9 +33,11 @@ grabHpcPos hsMap srcspan =
[ln] ->
take ((c2 - c1) + 1) $ drop (c1 - 1) ln
hd : tl ->
let lns1 = drop (c1 - 1) hd : tl
lns2 = init lns1 ++ [take (c2 + 1) (last lns1)]
in foldl1 (\xs ys -> xs ++ "\n" ++ ys) lns2
let lns1 :: NonEmpty String
lns1 = drop (c1 - 1) hd :| tl
lns2 :: [String]
lns2 = NE.init lns1 ++ [take (c2 + 1) (NE.last lns1)]
in intercalate "\n" lns2
where
(l1, c1, l2, c2) = fromHpcPos srcspan
lns =
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment