Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,2 @@
> expandRange "-6,-3--1,3-5,7-11,14,15,17-20"
[-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20]

View file

@ -0,0 +1,10 @@
expandRange :: String -> [Int]
expandRange = concatMap f . split ','
where f str@(c : cs) | '-' `elem` cs = [read (c : a) .. read b]
| otherwise = [read str]
where (a, _ : b) = break (== '-') cs
split :: Eq a => a -> [a] -> [[a]]
split delim [] = []
split delim l = a : split delim (dropWhile (== delim) b)
where (a, b) = break (== delim) l

View file

@ -0,0 +1,27 @@
{-# LANGUAGE FlexibleContexts #-}
import Text.Parsec
expandRange :: String -> Maybe [Int]
expandRange =
either
(const Nothing)
Just
. parse rangeParser ""
rangeParser ::
(Enum a, Read a, Stream s m Char) =>
ParsecT s u m [a]
rangeParser = concat <$> (item `sepBy` char ',')
where
item = do
n1 <- num
n2 <- option n1 (char '-' *> num)
return [n1 .. n2]
num =
read `dot` (<>) <$> option "" (string "-")
<*> many1 digit
dot = (.) . (.)
main :: IO ()
main = print $ expandRange "-6,-3--1,3-5,7-11,14,15,17-20"