Jump to content

Symmetric difference: Difference between revisions

→‎{{header|AppleScript}}: Some simplifications
(→‎{{header|AppleScript}}: Some simplifications)
Line 163:
-- union :: [a] -> [a] -> [a]
on union(xs, ys)
set sx to nub(xs & ys)
sx & foldl(flip(my delete_), nub(ys), sx)
end union
 
-- difference :: [a] -> [a] -> [a]
on difference(xs, ys)
script mfexcept
on exceptlambda(a, y)
if a contains y then
my delete_(y, a)
else
a
end if
end exceptlambda
end script
foldl(except of mf, xs, ys)
end difference
 
Line 215 ⟶ 214:
-- delete :: a -> [a] -> [a]
on delete_(x, xs)
script mfnotX
on lambda(a, y)
if y = my closure's x then
a
else
Line 225 ⟶ 224:
end script
foldl(mClosure(lambda of mf, {x:x})notX, [], xs)
end delete_
 
Line 240 ⟶ 239:
return v
end foldl
 
-- Function with reversed pair of arguments
-- flip :: (a -> b -> c) -> (b -> a -> c)
on flip(f)
script
property g : f
on lambda(x, y)
my g(y, x)
end lambda
end script
end flip
 
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then return f
script f
else
property lambda : f
end script
property glambda : f
end script
end lambdaif
end mReturn
 
-- mClosure :: Handler -> Record -> Script
on mClosure(f, recBindings)
script
property closure : recBindings
property lambda : f
end script
end mClosure
</lang>
 
9,659

edits

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