Jump to content

Functional coverage tree: Difference between revisions

m
→‎{{header|Haskell}}: Used Data.Bifunctor in place of Control.Arrow
(Add Swift)
m (→‎{{header|Haskell}}: Used Data.Bifunctor in place of Control.Arrow)
Line 354:
import qualified Data.Text.IO as T
import qualified Data.Text as T
import Control.Arrow ((&&&), first)
import Numeric (showFFloat)
import ControlData.ArrowBifunctor ((&&&), first)
import Data.Char (isSpace)
import Data.Bool (bool)
Line 395:
weightedCoverage :: Coverage -> Forest Coverage -> Tree Coverage
weightedCoverage x xs =
let cws = ((,) . coverage &&&<*> weight) . rootLabel) <$> xs
totalWeight = foldr ((+) . snd) 0 cws
in Node
Line 409:
let go fraction node =
let forest = subForest node
weights = (weight . rootLabel) <$> forest
weightTotal = sum weights
nodeRoot = rootLabel node
Line 416:
{ share = fraction * (1 - coverage nodeRoot)
})
(zipWith go (((fraction *) . (/ weightTotal)) <$> weights) forest)
in go shareOfTotal tree
 
Line 422:
parseTreeFromOutline :: T.Text -> [T.Text] -> Tree Coverage
parseTreeFromOutline delimiter indentedLines =
(partialRecord . tokenizeWith delimiter) <$>
head (forestFromLineIndents $ indentLevelsFromLines $ tail indentedLines)
 
Line 474:
delimiter
([T.append indent (name x), T.pack (showN 0 (weight x))] ++
((T.pack . showN 4) <$> ([coverage, share] <*> [x])))
 
tabulation :: T.Text -> [T.Text] -> T.Text
Line 485:
 
showN :: Int -> Float -> String
showN p n = justifyRight 7 ' ' (showFFloat (Just p) n "")</lang>
 
-- GENERIC ------------------------------------------------
foldTree :: (a -> [b] -> b) -> Tree a -> b
foldTree f = go
where
go (Node x ts) = f x (map go ts)</lang>
{{Out}}
<pre>NAME_HIERARCHY | WEIGHT | COVERAGE | SHARE OF RESIDUE
9,655

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.