Range consolidation: Difference between revisions
m
→{{header|Haskell}}: Tidied
m (→{{header|Haskell}}: Tidied) |
|||
Line 645:
<lang haskell>import Data.List (intercalate, maximumBy, sort)
import Data.Ord (comparing)
------------------- RANGE CONSOLIDATION ------------------
consolidated :: [(Float, Float)] -> [(Float, Float)]
consolidated xs =
let go xy [] = [xy]
go xy@(x, y) abetc@((a, b) : etc)
| y >= b = xy : etc
| y >= a = (x, b) : etc
Line 656 ⟶ 658:
| a <= b = (a, b)
| otherwise = (b, a)
in foldr go [] (sort . fmap ab $ xs)
-
tests :: [[(Float, Float)]]
tests =
[ [],
]
Line 673 ⟶ 675:
main =
putStrLn $
tabulated
"Range consolidations:"
showPairs
showPairs
consolidated
tests
-
tabulated ::
String ->
(a -> String) ->
(b -> String) ->
(a -> b) ->
[a] ->
String
tabulated s xShow fxShow f xs =
let w =
let w = length $ maximumBy (comparing length) (xShow <$> xs)▼
maximumBy
in unlines $▼
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs▼
(xShow <$> xs)
rjust n c s = drop (length s) (replicate n c <> s)
▲ in unlines $
s :
fmap
( ((<>) . rjust w ' ' . xShow)
)
xs
showPairs :: [(Float, Float)] -> String
showPairs xs
| null xs = "[]"
| otherwise =
'[' :
intercalate
", "
(showPair <$> xs)
<> "]"
showPair :: (Float, Float) -> String
showPair (a, b) =
'(' :
showNum a
<> ", "
<> showNum b
<> ")"
showNum :: Float -> String
|