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>on run
<lang AppleScript>intercalate(", ", ¬
map(curry(intercalate)'s |λ|(""), ¬
group("gHHH5YY++///\\")))
intercalate(", ", ¬
map(curry(intercalate)'s lambda(""), ¬
group("gHHH5YY++///\\")))
--> "g, HHH, 5, YY, ++, ///, \\"
end run


--> "g, HHH, 5, YY, ++, ///, \\"



-- GENERIC FUNCTIONS
-- GENERIC FUNCTIONS ----------------------------------------------------------
-- curry :: (Script|Handler) -> Script
on curry(f)
script
on |λ|(a)
script
on |λ|(b)
|λ|(a, b) of mReturn(f)
end |λ|
end script
end |λ|
end script
end curry

-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl


-- group :: Eq a => [a] -> [[a]]
-- group :: Eq a => [a] -> [[a]]
on group(xs)
on group(xs)
script eq
script eq
on lambda(a, b)
on |λ|(a, b)
a = b
a = b
end lambda
end |λ|
end script
end script
Line 93: Line 113:
script enGroup
script enGroup
on lambda(a, x)
on |λ|(a, x)
set h to cond(length of (active of a) > 0, item 1 of active of a, missing value)
if length of (active of a) > 0 then
set h to item 1 of active of a
else
set h to missing value
end if
if h is not missing value and mf's lambda(h, x) then
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 lambda
end |λ|
end script
end script
if length of xs > 0 then
if length of xs > 0 then
set dct to foldl(enGroup, {active:{item 1 of xs}, sofar:{}}, tail(xs))
tell foldl(enGroup, {active:{item 1 of xs}, sofar:{}}, tail(xs))
sofar of dct & cond(length of (active of dct) > 0, {active of dct}, {})
if length of (its active) > 0 then
its sofar & its active
else
{}
end if
end tell
else
else
{}
{}
end if
end if
end groupBy
end groupBy

-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to lambda(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl

-- cond :: Bool -> a -> a -> a
on cond(bool, f, g)
if bool then
f
else
g
end if
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 lambda(item i of xs, i, xs)
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

-- curry :: (Script|Handler) -> Script
on curry(f)
script
on lambda(a)
script
on lambda(b)
lambda(a, b) of mReturn(f)
end lambda
end script
end lambda
end script
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 lambda : f
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>