Symmetric difference: Difference between revisions
m
→{{header|AppleScript}}: Updated primitives
m (formatting of task description) |
m (→{{header|AppleScript}}: Updated primitives) |
||
Line 149:
{{Trans|JavaScript}} (ES6 Functional JS)
<lang AppleScript>-- UNION AND DIFFERENCE
Line 163 ⟶ 162:
on lambda(a, y)
if a contains y then
else
a
Line 192 ⟶ 191:
-- GENERIC LIBRARY FUNCTIONS
-- foldl :: (a -> b -> a) -> a -> [b] -> a▼
on foldl(f, startValue, xs)▼
set v to startValue▼
set lng to length of xs▼
repeat with i from 1 to lng▼
end repeat▼
return v▼
end tell
end foldl▼
-- unique set of members of xs
Line 198 ⟶ 209:
if (length of xs) > 1 then
set x to item 1 of xs
[x] & nub(
else
xs
end if
end nub
-- uncons :: [a] -> Maybe (a, [a])
on uncons(xs)
if length of xs > 0 then
missing value
end uncons
-- deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a]
on deleteBy(fnEq, x, xs)
if length of xs > 0 then
set {h, t} to uncons(xs)
if lambda(x, h) of mReturn(fnEq) then
else
{h} & deleteBy(fnEq, x, t)
end if
else▼
{}
end if
end deleteBy
-- delete :: a -> [a] -> [a]
on
script
on lambda(a,
▲ a
▲ else
▲ a & y
▲ end if
end lambda
end script
end
▲-- foldl :: (a -> b -> a) -> a -> [b] -> a
▲on foldl(f, startValue, xs)
▲ set mf to mReturn(f)
▲ set v to startValue
▲ set lng to length of xs
▲ repeat with i from 1 to lng
▲ set v to mf's lambda(v, item i of xs, i, xs)
▲ end repeat
▲ return v
▲end foldl
-- Lift 2nd class handler function into 1st class script wrapper
Line 242 ⟶ 260:
end script
end if
end mReturn</lang>
{{Out}}
|