Range extraction: Difference between revisions
→{{header|Haskell}}: (updated generic splitBy function)
(→{{header|JavaScript}}: ES6 Trans Haskell (Defining the range format in terms of a reusable splitBy function)) |
(→{{header|Haskell}}: (updated generic splitBy function)) |
||
Line 1,934:
:: (Integral a, Ord a, Show a)
=> [a] -> String
rangeFormat = intercalate "," . (rangeString <$>) . splitBy (\a b -> b - a > 1)▼
▲ intercalate "," . (rangeString <$>) . splitBy (\a b -> b - a > 1)
rangeString
Line 1,945 ⟶ 1,944:
where
ps = show <$> xs
-- GENERIC
Line 1,951 ⟶ 1,949:
-- between two consecutive items
splitBy :: (a -> a -> Bool) -> [a] -> [[a]]
splitBy
splitBy _
splitBy f
where
(acc, active) =
Line 1,960 ⟶ 1,958:
if f x prev
then (active : acc, [x])
else (
-- TEST
main :: IO ()
|