Range consolidation: Difference between revisions
Content added Content deleted
m (→{{header|Haskell}}: Tidied) |
|||
Line 645: | Line 645: | ||
<lang haskell>import Data.List (intercalate, maximumBy, sort) |
<lang haskell>import Data.List (intercalate, maximumBy, sort) |
||
import Data.Ord (comparing) |
import Data.Ord (comparing) |
||
------------------- RANGE CONSOLIDATION ------------------ |
|||
consolidated :: [(Float, Float)] -> [(Float, Float)] |
consolidated :: [(Float, Float)] -> [(Float, Float)] |
||
consolidated xs = |
consolidated xs = |
||
let go xy [] = [xy] |
let go xy [] = [xy] |
||
go xy@(x, y) abetc@((a, b):etc) |
go xy@(x, y) abetc@((a, b) : etc) |
||
| y >= b = xy : etc |
| y >= b = xy : etc |
||
| y >= a = (x, b) : etc |
| y >= a = (x, b) : etc |
||
Line 656: | Line 658: | ||
| a <= b = (a, b) |
| a <= b = (a, b) |
||
| otherwise = (b, a) |
| otherwise = (b, a) |
||
in foldr go [] (sort . fmap ab $ xs) |
in foldr go [] (sort . fmap ab $ xs) |
||
- |
--------------------------- TEST ------------------------- |
||
tests :: [[(Float, Float)]] |
tests :: [[(Float, Float)]] |
||
tests = |
tests = |
||
[ [] |
[ [], |
||
[(1.1, 2.2)], |
|||
[(6.1, 7.2), (7.2, 8.3)], |
|||
[(4, 3), (2, 1)], |
|||
[(4, 3), (2, 1), (-1, -2), (3.9, 10)], |
|||
[(1, 3), (-6, -1), (-4, -5), (8, 2), (-6, -6)] |
|||
] |
] |
||
Line 673: | Line 675: | ||
main = |
main = |
||
putStrLn $ |
putStrLn $ |
||
tabulated |
|||
tabulated "Range consolidations:" showPairs showPairs consolidated tests |
|||
"Range consolidations:" |
|||
showPairs |
|||
showPairs |
|||
consolidated |
|||
tests |
|||
- |
-------------------- DISPLAY FORMATTING ------------------ |
||
tabulated :: |
|||
tabulated :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String |
|||
String -> |
|||
(a -> String) -> |
|||
(b -> String) -> |
|||
(a -> b) -> |
|||
[a] -> |
|||
String |
|||
tabulated s xShow fxShow f xs = |
tabulated s xShow fxShow f xs = |
||
let w = |
|||
⚫ | |||
length $ |
|||
maximumBy |
|||
⚫ | |||
⚫ | |||
⚫ | |||
(xShow <$> xs) |
|||
rjust n c s = drop (length s) (replicate n c <> s) |
|||
⚫ | |||
s : |
|||
fmap |
|||
( ((<>) . rjust w ' ' . xShow) |
|||
⚫ | |||
) |
|||
xs |
|||
showPairs :: [(Float, Float)] -> String |
showPairs :: [(Float, Float)] -> String |
||
showPairs xs |
showPairs xs |
||
| null xs = "[]" |
| null xs = "[]" |
||
| otherwise = |
| otherwise = |
||
'[' : |
|||
intercalate |
|||
", " |
|||
(showPair <$> xs) |
|||
<> "]" |
|||
showPair :: (Float, Float) -> String |
showPair :: (Float, Float) -> String |
||
showPair (a, b) = |
showPair (a, b) = |
||
'(' : |
|||
showNum a |
|||
<> ", " |
|||
<> showNum b |
|||
<> ")" |
|||
showNum :: Float -> String |
showNum :: Float -> String |