Split a character string based on change of character: Difference between revisions
Content added Content deleted
(→JS ES6: updated groupBy primitive ( redrafted as a fold, rather than an explicit recursion with 'span' )) |
(→{{header|AppleScript}}: ( updated groupBy primitive – using a fold, rather than explicit recursion with span)) |
||
Line 52:
-- groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
on groupBy(f, xs)
set
script enGroup
set x to item 1 of xs▼
set h to cond(length of (active of a) > 0, item 1 of active of a, missing value)
{active:{x}, sofar:(sofar of a) & {active of a}}
end script▼
end if
end lambda
if length of xs
set dct to foldl(enGroup,
sofar of dct & cond(length of (active of dct) > 0, {active of dct}, {})
▲ else
else
{}
Line 76 ⟶ 74:
end groupBy
on foldl(f, startValue, xs)
▲-- span :: (a -> Bool) -> [a] -> ([a],[a])
tell mReturn(f)
repeat while i < lng and lambda(item (i + 1) of xs)▼
repeat with i from 1 to lng
end repeat
▲ return v
end tell
end foldl
-- cond :: Bool -> a -> a
on cond(bool, f, g)
if bool
else
end if
end
-- intercalate :: Text -> [Text] -> Text
|