Split a character string based on change of character: Difference between revisions
Split a character string based on change of character (view source)
Revision as of 20:48, 15 May 2017
, 7 years ago→{{header|AppleScript}}: Updated primitives
(Adds Clojure solution) |
(→{{header|AppleScript}}: Updated primitives) |
||
Line 64:
=={{header|AppleScript}}==
{{Trans|JavaScript}}
<lang AppleScript>
▲ map(curry(intercalate)'s lambda(""), ¬
▲ group("gHHH5YY++///\\")))
--> "g, HHH, 5, YY, ++, ///, \\"▼
end run▼
-- GENERIC FUNCTIONS ----------------------------------------------------------
-- curry :: (Script|Handler) -> Script▼
on curry(f)▼
script▼
script▼
end script▼
end script▼
-- 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▼
return v▼
end tell▼
end foldl▼
-- group :: Eq a => [a] -> [[a]]
on group(xs)
script eq
on
a = b
end
end script
Line 93 ⟶ 113:
script enGroup
on
set h to item 1 of active of a
set h to missing value
end if▼
if h is not missing value and mf's
{active:(active of a) & x, sofar:sofar of a}
else
{active:{x}, sofar:(sofar of a) & {active of a}}
end if
end
end script
if length of xs > 0 then
its sofar & its active
else▼
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
▲ f
▲ else
▲ g
▲ end if
-- intercalate :: Text -> [Text] -> Text
Line 147 ⟶ 155:
set lst to {}
repeat with i from 1 to lng
set end of lst to
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
-- Lift 2nd class handler function into 1st class script wrapper
Line 173 ⟶ 168:
else
script
property
end script
end if
Line 186 ⟶ 181:
end if
end tail</lang>
{{Out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
|