Permutations: Difference between revisions

→‎{{header|AppleScript}}: Directly defined delete,and concatMap, dropped foldl
(→‎JS ES6: (defining delete directly, rather than in terms of deleteBy))
(→‎{{header|AppleScript}}: Directly defined delete,and concatMap, dropped foldl)
Line 440:
 
{{trans|JavaScript}}
(Functional ES5ES6 version)
 
<lang AppleScript>-- permutations :: [a] -> [[a]]
Line 470:
end run
 
 
 
Line 477 ⟶ 476:
-- concatMap :: (a -> [b]) -> [a] -> [b]
on concatMap(f, xs)
scriptset appendlst to {}
set lng to length onof lambda(a, b)xs
tell a & bmReturn(f)
endrepeat lambdawith i from 1 to lng
set vlst to (lst & lambda(v,contents of item i of xs, i, xs))
end script
end repeat
end scripttell
foldl(append, {}, map(f, xs))
return vlst
end concatMap
 
Line 497:
end tell
end map
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to lambda(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
 
-- delete :: a -> [a] -> [a]
on |delete|(x, xs)
script Eq
on lambda(a, b)
a = b
end lambda
end script
deleteBy(Eq, x, xs)
end |delete|
 
-- 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
t
else
{h} & deleteBy|delete|(fnEq, x, t)
end if
else
{}
end if
end deleteBy|delete|
 
-- uncons :: [a] -> Maybe (a, [a])
9,659

edits