Jump to content

Permutations: Difference between revisions

→‎{{header|AppleScript}}: Simplified closures, updated library functions
No edit summary
(→‎{{header|AppleScript}}: Simplified closures, updated library functions)
Line 442:
(Functional ES5 version)
 
<lang AppleScript>on run
on run
permutations({"aardvarks", "eat", "ants"})
Line 451 ⟶ 450:
-- permutations :: [a] -> [[a]]
on permutations(xs)
script mffirstElement
on lambdaXlambda(x)
setscript mf to mf of my closuretailElements
concatMap(mClosure(mf's lambdaYs, {x:x}), ¬ on lambda(ys)
permutations(|delete|(x, xs of my closure))){{x} & ys}
end lambdaXlambda
end script
on lambdaYs(ys)
{{my closure'sconcatMap(tailElements, permutations(|delete|(x} &, ys}xs)))
end lambdaYslambda
end script
if length of xs > 0 then
concatMap(mClosure(mf's lambdaX, {mf:mf, xs:xs})firstElement, xs)
else
{{}}
end if
end permutations
 
 
 
Line 475:
-- concatMap :: (a -> [b]) -> [a] -> [b]
on concatMap(f, xs)
script mfappend
on appendlambda(a, b)
a & b
end appendlambda
end script
foldl(mf's append, {}, map(f, xs))
end concatMap
 
Line 508 ⟶ 509:
-- delete :: a -> [a] -> [a]
on |delete|(x, xs)
script mfEq
on Eqlambda(a, b)
a = b
end Eqlambda
end script
deleteBy(mf's Eq, x, xs)
end |delete|
 
Line 520 ⟶ 521:
on deleteBy(fnEq, x, xs)
if length of xs > 0 then
set mf to mReturn(fnEq)
set {h, t} to uncons(xs)
if mf's lambda(x, h) of mReturn(fnEq) then
t
else
Line 546:
end uncons
 
-- 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 lambda : f
end script
end if
end mReturn
 
-- 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.