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,37 @@
import Data.List
( genericLength,
intercalate,
isPrefixOf,
stripPrefix,
)
------------------------ MULTISPLIT ----------------------
multisplit :: [String] -> String -> [(String, String, Int)]
multisplit delims = go [] 0
where
go acc pos [] = [(acc, [], pos)]
go acc pos l@(s : sx) =
case trysplit delims l of
Nothing -> go (s : acc) (pos + 1) sx
Just (d, sxx) ->
(acc, d, pos) :
go [] (pos + genericLength d) sxx
trysplit :: [String] -> String -> Maybe (String, String)
trysplit delims s =
case filter (`isPrefixOf` s) delims of
[] -> Nothing
(d : _) -> Just (d, (\(Just x) -> x) $ stripPrefix d s)
--------------------------- TEST -------------------------
main :: IO ()
main = do
let parsed = multisplit ["==", "!=", "="] "a!===b=!=c"
mapM_
putStrLn
[ "split string:",
intercalate "," $ map (\(a, _, _) -> a) parsed,
"with [(string, delimiter, offset)]:",
show parsed
]

View file

@ -0,0 +1,18 @@
import Data.List (find, isPrefixOf, foldl') --'
import Data.Bool (bool)
multiSplit :: [String] -> String -> [(String, String, Int)]
multiSplit ds s =
let (ts, ps, o) =
foldl' --'
(\(tokens, parts, offset) (c, i) ->
let inDelim = offset > i
in maybe
(bool (c : tokens) tokens inDelim, parts, offset)
(\x -> ([], (tokens, x, i) : parts, i + length x))
(bool (find (`isPrefixOf` drop i s) ds) Nothing inDelim))
([], [], 0)
(zip s [0 ..])
in reverse $ (ts, [], length s) : ps
main :: IO ()
main = print $ multiSplit ["==", "!=", "="] "a!===b=!=c"