Range extraction: Difference between revisions

m
m (→‎Haskell splitBy: Edited some comment text, simplified two function expressions)
Line 1,908:
-- RANGE FORMAT --------------------------------------------------------------
 
rangeFormat :: [Int] -> String
rangeFormat = intercalate "," . (rangeString <$>) . splitBy ((/=) . succ)
Line 1,917:
where
ps@(x:t) = show <$> xs
-- GENERIC FUNCTION ----------------------------------------------------------
Line 1,922 ⟶ 1,923:
-- Split wherever a supplied predicate matches the relationship
-- between two consecutive items.
 
-- E.G. at boundaries between vowels and consonants:
-- splitBy (on (/=) (flip elem "aeiouAEIOU")) "Constantinople"
-- -> ["C","o","nst","a","nt","i","n","o","pl","e"]
 
-- At boundaries between non-successive integers:
-- splitBy (\a b -> b /= succ a) [0, 1, 2, 4, 6, 7, 8, 11, 12, 14]
-- -> [[0,1,2],[4],[6,7,8],[11,12],[14]]
 
splitBy :: (a -> a -> Bool) -> [a] -> [[a]]
splitBy _ [] = []
Line 1,949 ⟶ 1,950:
(zip xs t)
-- TEST ----------------------------------------------------------------------
-- TEST
main :: IO ()
main =
9,655

edits