Permutations: Difference between revisions
Content added Content deleted
No edit summary |
(→{{header|AppleScript}}: Simplified closures, updated library functions) |
||
Line 442: | Line 442: | ||
(Functional ES5 version) |
(Functional ES5 version) |
||
<lang AppleScript> |
<lang AppleScript>on run |
||
on run |
|||
permutations({"aardvarks", "eat", "ants"}) |
permutations({"aardvarks", "eat", "ants"}) |
||
Line 451: | Line 450: | ||
-- permutations :: [a] -> [[a]] |
-- permutations :: [a] -> [[a]] |
||
on permutations(xs) |
on permutations(xs) |
||
script |
script firstElement |
||
on |
on lambda(x) |
||
script tailElements |
|||
on lambda(ys) |
|||
{{x} & ys} |
|||
end |
end lambda |
||
end script |
|||
concatMap(tailElements, permutations(|delete|(x, xs))) |
|||
end |
end lambda |
||
end script |
end script |
||
if length of xs > 0 then |
if length of xs > 0 then |
||
concatMap( |
concatMap(firstElement, xs) |
||
else |
else |
||
{{}} |
{{}} |
||
end if |
end if |
||
end permutations |
end permutations |
||
Line 475: | Line 475: | ||
-- concatMap :: (a -> [b]) -> [a] -> [b] |
-- concatMap :: (a -> [b]) -> [a] -> [b] |
||
on concatMap(f, xs) |
on concatMap(f, xs) |
||
script |
script append |
||
on |
on lambda(a, b) |
||
a & b |
a & b |
||
end |
end lambda |
||
end script |
end script |
||
foldl( |
foldl(append, {}, map(f, xs)) |
||
end concatMap |
end concatMap |
||
Line 508: | Line 509: | ||
-- delete :: a -> [a] -> [a] |
-- delete :: a -> [a] -> [a] |
||
on |delete|(x, xs) |
on |delete|(x, xs) |
||
script |
script Eq |
||
on |
on lambda(a, b) |
||
a = b |
a = b |
||
end |
end lambda |
||
end script |
end script |
||
deleteBy( |
deleteBy(Eq, x, xs) |
||
end |delete| |
end |delete| |
||
Line 520: | Line 521: | ||
on deleteBy(fnEq, x, xs) |
on deleteBy(fnEq, x, xs) |
||
if length of xs > 0 then |
if length of xs > 0 then |
||
set mf to mReturn(fnEq) |
|||
set {h, t} to uncons(xs) |
set {h, t} to uncons(xs) |
||
if |
if lambda(x, h) of mReturn(fnEq) then |
||
t |
t |
||
else |
else |
||
Line 546: | Line 546: | ||
end uncons |
end uncons |
||
-- Lift 2nd class function into 1st class wrapper |
-- Lift 2nd class handler function into 1st class script wrapper |
||
-- mReturn :: Handler -> Script |
-- mReturn :: Handler -> Script |
||
on mReturn(f) |
on mReturn(f) |
||
if class of f is script then |
if class of f is script then |
||
f |
|||
else |
|||
⚫ | |||
script |
|||
⚫ | |||
⚫ | |||
end if |
|||
end mReturn |
end mReturn |
||
-- Handler -> Record -> Script |
|||
on mClosure(f, recBindings) |
|||
script |
|||
property closure : recBindings |
|||
property lambda : f |
|||
⚫ | |||
end mClosure |
|||
</lang> |
</lang> |
||