Skip to content

Commit 8a830c0

Browse files
Refactoring code to reduce duplication
1 parent 3ef4c59 commit 8a830c0

File tree

4 files changed

+153
-180
lines changed

4 files changed

+153
-180
lines changed

app/generate-timings/Main.hs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,8 @@ generateTruncatedDataFile taxaNumPadder strLenPadder filePath counter taxaSize (
298298
, taxaFilePath
299299
]
300300

301-
_ <- deleteFileIfExists taxaFilePath
302-
_ <- deleteFileIfExists lessFilePath
301+
p <- makeCleanProcess counter [taxaFilePath, lessFilePath] commandStr
303302

304-
let p = makeProcessFromCommand commandStr
305-
306-
printCounter counter
307303
putStrLn $ unwords [ taxaNumPadder taxaSize, strLenPadder strLength ]
308304

309305
(_exitCode, stdOut, _stdErr) <- readCreateProcessWithExitCode p ""
@@ -339,11 +335,8 @@ generateTruncatedTreeFile taxaNumPadder filePath counter (taxaSize, taxaFilePath
339335
, taxaFilePath <.> "deleted"
340336
]
341337

342-
_ <- deleteFileIfExists lessFilePath
338+
p <- makeCleanProcess counter [lessFilePath] commandStr
343339

344-
let p = makeProcessFromCommand commandStr
345-
346-
printCounter counter
347340
putStrLn $ taxaNumPadder taxaSize
348341

349342
(_exitCode, stdOut, _stdErr) <- readCreateProcessWithExitCode p ""
@@ -388,9 +381,8 @@ timeFilePoint taxaNumPadder strLenPadder counter tcmPath fp = do
388381
, "/dev/null"
389382
]
390383

391-
let p = makeProcessFromCommand commandStr
384+
p <- makeCleanProcess counter [] commandStr
392385

393-
printCounter counter
394386
-- putStrLn $ fileDataPath fp
395387
putStrLn $ unwords [ taxaNumPadder $ taxaCount fp
396388
, strLenPadder $ stringLength fp
@@ -402,6 +394,14 @@ timeFilePoint taxaNumPadder strLenPadder counter tcmPath fp = do
402394
pure $ force (taxaCount fp, stringLength fp, postOrder, preOrder)
403395

404396

397+
makeCleanProcess :: IORef (Word, Word) -> [FilePath] -> String -> IO CreateProcess
398+
makeCleanProcess counter files commandStr = do
399+
traverse_ deleteFileIfExists files
400+
let p = makeProcessFromCommand commandStr
401+
printCounter counter
402+
pure p
403+
404+
405405
makeProcessFromCommand :: String -> CreateProcess
406406
makeProcessFromCommand commandStr =
407407
CreateProcess

efficient-implied-alignment.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ library
107107
, mtl
108108
, parser-combinators >=1.2
109109
, pointed
110+
, primitive >=0.7
110111
, QuickCheck
111112
, scientific
112113
, semigroupoids

src/Alignment/Pairwise/Internal.hs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
{-# LANGUAGE FlexibleContexts #-}
2020
{-# LANGUAGE FlexibleInstances #-}
2121
{-# LANGUAGE MultiParamTypeClasses #-}
22-
{-# LANGUAGE TypeSynonymInstances #-}
2322
{-# LANGUAGE TypeFamilies #-}
2423

2524
-- To add Indexable/Lookup instances for Matrix
@@ -424,23 +423,23 @@ deleteGaps gap bvs
424423
gapLen <- newSTRef 0
425424
gapRefs <- newSTRef []
426425

426+
let handleGapBefore op = do
427+
gapBefore <- readSTRef prevGap
428+
when gapBefore $ do
429+
j <- readSTRef nonGaps
430+
g <- readSTRef gapLen
431+
modifySTRef gapRefs ( (j,g): )
432+
op
433+
427434
for_ [0 .. charLen - 1] $ \i ->
428435
if symbolAlignmentMedian (bvs ! i) == gap
429436
then modifySTRef gapLen succ *> writeSTRef prevGap True
430-
else do gapBefore <- readSTRef prevGap
431-
when gapBefore $ do
432-
j <- readSTRef nonGaps
433-
g <- readSTRef gapLen
434-
modifySTRef gapRefs ( (j,g): )
437+
else do handleGapBefore $ do
435438
writeSTRef gapLen 0
436439
writeSTRef prevGap False
437440
modifySTRef nonGaps succ
438-
439-
gapBefore <- readSTRef prevGap
440-
when gapBefore $ do
441-
j <- readSTRef nonGaps
442-
g <- readSTRef gapLen
443-
modifySTRef gapRefs ( (j,g): )
441+
442+
handleGapBefore $ pure ()
444443
readSTRef gapRefs
445444

446445

0 commit comments

Comments
 (0)