Permutations: Difference between revisions

m
→‎{{header|AppleScript}}: Slight reorganisation, updated primitives
(→‎{{header|Scala}}: Simplify collections library example)
m (→‎{{header|AppleScript}}: Slight reorganisation, updated primitives)
Line 442:
(Functional ES5 version)
 
<lang AppleScript>on-- runpermutations :: [a] -> [[a]]
permutations({"aardvarks", "eat", "ants"})
end run
 
-- permutations :: [a] -> [[a]]
on permutations(xs)
script firstElement
Line 468 ⟶ 462:
end if
end permutations
 
 
-- TEST
on run
permutations({"aardvarks", "eat", "ants"})
end run
 
 
Line 486 ⟶ 488:
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
set mf totell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to mf's lambda(item i of xs, i, xs)
end repeat
return lst
end tell
end map
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
set mf totell mReturn(f)
set v to startValue
set vlng to startValuelength of xs
repeat with i from 1 to lng
set lng to length of xs
set v to mf's lambda(v, item i of xs, i, xs)
repeat with i from 1 to lng
end repeat
set v to mf's lambda(v, item i of xs, i, xs)
end repeat return v
returnend vtell
end foldl
 
Line 556 ⟶ 559:
end script
end if
end mReturn</lang>
 
</lang>
 
{{Out}}
9,655

edits