Split a character string based on change of character: Difference between revisions

→‎{{header|AppleScript}}: Updated primitives
(Adds Clojure solution)
(→‎{{header|AppleScript}}: Updated primitives)
Line 64:
=={{header|AppleScript}}==
{{Trans|JavaScript}}
<lang AppleScript>onintercalate(", run", ¬
map(curry(intercalate)'s lambda|λ|(""), ¬
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 lambda|λ|(a)
script
on lambda|λ|(b)
lambda|λ|(a, b) of mReturn(f)
end lambda|λ|
end script
end repeat|λ|
end script
end runcurry
 
-- 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 lambdarepeat
return v
end tell
end foldl
 
-- group :: Eq a => [a] -> [[a]]
on group(xs)
script eq
on lambda|λ|(a, b)
a = b
end lambda|λ|
end script
Line 93 ⟶ 113:
script enGroup
on lambda|λ|(a, x)
setif h to cond(length of (active of a) > 0, item 1 of active of a, missing value)then
set h to item 1 of active of a
f else
set h to missing value
end if
if h is not missing value and mf's lambda|λ|(h, x) then
{active:(active of a) & x, sofar:sofar of a}
else
{active:{x}, sofar:(sofar of a) & {active of a}}
end if
end lambda|λ|
end script
if length of xs > 0 then
set dct totell foldl(enGroup, {active:{item 1 of xs}, sofar:{}}, tail(xs))
sofar of dct & cond(if length of (its active of dct) > 0, {active of dct}, {})then
its sofar & its active
else
g {}
end if
end tell
else
{}
end if
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
Line 147 ⟶ 155:
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda|λ|(item i of xs, i, xs)
end repeat
return lst
end tell
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
Line 173 ⟶ 168:
else
script
property lambda|λ| : f
end script
end if
Line 186 ⟶ 181:
end if
end tail</lang>
 
{{Out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
9,659

edits