Jump to content

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
delete_|delete|(y, a)
else
a
Line 192 ⟶ 191:
 
-- GENERIC LIBRARY FUNCTIONS
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
set mf totell 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 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(delete_|delete|(x, items 2 thru -1 of xs))
else
xs
end if
end nub
 
-- uncons :: [a] -> Maybe (a, [a])
on uncons(xs)
if length of xs > 0 then
{item 1 of xs, rest of axs}
else
missing value
end if
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
a & yt
else
{h} & deleteBy(fnEq, x, t)
end if
else
{}
end if
end deleteBy
 
-- delete :: a -> [a] -> [a]
on delete_|delete|(x, xs)
script notXEq
on lambda(a, yb)
if ya = x thenb
a
else
a & y
end if
end lambda
end script
foldldeleteBy(notXEq, []x, xs)
end delete_|delete|
 
 
-- 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>
</lang>
 
{{Out}}
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.