Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
37
Task/Multisplit/Haskell/multisplit-1.hs
Normal file
37
Task/Multisplit/Haskell/multisplit-1.hs
Normal 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
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue