Split a character string based on change of character: Difference between revisions
Content added Content deleted
(Adds Clojure solution) |
(→{{header|AppleScript}}: Updated primitives) |
||
Line 64: | Line 64: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
{{Trans|JavaScript}} |
{{Trans|JavaScript}} |
||
<lang AppleScript> |
<lang AppleScript>intercalate(", ", ¬ |
||
⚫ | |||
⚫ | |||
intercalate(", ", ¬ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
-- GENERIC FUNCTIONS |
|||
-- GENERIC FUNCTIONS ---------------------------------------------------------- |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
-- group :: Eq a => [a] -> [[a]] |
-- group :: Eq a => [a] -> [[a]] |
||
on group(xs) |
on group(xs) |
||
script eq |
script eq |
||
on |
on |λ|(a, b) |
||
a = b |
a = b |
||
end |
end |λ| |
||
end script |
end script |
||
Line 93: | Line 113: | ||
script enGroup |
script enGroup |
||
on |
on |λ|(a, x) |
||
if length of (active of a) > 0 then |
|||
set h to item 1 of active of a |
|||
⚫ | |||
set h to missing value |
|||
⚫ | |||
if h is not missing value and mf's |
if h is not missing value and mf's |λ|(h, x) then |
||
{active:(active of a) & x, sofar:sofar of a} |
{active:(active of a) & x, sofar:sofar of a} |
||
else |
else |
||
{active:{x}, sofar:(sofar of a) & {active of a}} |
{active:{x}, sofar:(sofar of a) & {active of a}} |
||
end if |
end if |
||
end |
end |λ| |
||
end script |
end script |
||
if length of xs > 0 then |
if length of xs > 0 then |
||
tell foldl(enGroup, {active:{item 1 of xs}, sofar:{}}, tail(xs)) |
|||
if length of (its active) > 0 then |
|||
its sofar & its active |
|||
⚫ | |||
⚫ | |||
end if |
|||
end tell |
|||
else |
else |
||
{} |
{} |
||
end if |
end if |
||
end groupBy |
end groupBy |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
-- cond :: Bool -> a -> a -> a |
|||
on cond(bool, f, g) |
|||
if bool then |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end cond |
|||
-- intercalate :: Text -> [Text] -> Text |
-- intercalate :: Text -> [Text] -> Text |
||
Line 147: | Line 155: | ||
set lst to {} |
set lst to {} |
||
repeat with i from 1 to lng |
repeat with i from 1 to lng |
||
set end of lst to |
set end of lst to |λ|(item i of xs, i, xs) |
||
end repeat |
end repeat |
||
return lst |
return lst |
||
end tell |
end tell |
||
end map |
end map |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end curry |
|||
-- Lift 2nd class handler function into 1st class script wrapper |
-- Lift 2nd class handler function into 1st class script wrapper |
||
Line 173: | Line 168: | ||
else |
else |
||
script |
script |
||
property |
property |λ| : f |
||
end script |
end script |
||
end if |
end if |
||
Line 186: | Line 181: | ||
end if |
end if |
||
end tail</lang> |
end tail</lang> |
||
{{Out}} |
{{Out}} |
||
<pre>g, HHH, 5, YY, ++, ///, \</pre> |
<pre>g, HHH, 5, YY, ++, ///, \</pre> |