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)
rangeFormat =
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 f_ xs[] = []
splitBy _ | length xs < 2[x] = [xs[x]]
splitBy f | otherwisexs@(h:t) = active : acc
where
(acc, active) =
Line 1,960 ⟶ 1,958:
if f x prev
then (active : acc, [x])
else ( acc, x : active))
([], x[last :t])
(ifzip nullxs activet)
then [prev]
else active)))
([], [])
(zip xs (tail xs))
 
-- TEST
main :: IO ()
9,655

edits