Common sorted list: Difference between revisions

Content added Content deleted
(Added AutoHotkey)
(→‎{{header|AppleScript}}: Added a composition of atomic functions)
Line 22: Line 22:
{{output}}
{{output}}
<lang applescript>{1, 3, 4, 5, 7, 8, 9}</lang>
<lang applescript>{1, 3, 4, 5, 7, 8, 9}</lang>


Or, as a composition of slightly more commonly-used generic functions
(given that ''distinctUnionOfArrays'' is a relatively specialised function,
while ''concat/flatten'' and ''nub/distinct'' are more atomic, and more frequently reached for):

<lang applescript>use AppleScript version "2.4"
use framework "Foundation"


------------------- COMMON SORTED ARRAY ------------------
on run
sort(nub(concat({¬
{5, 1, 3, 8, 9, 4, 8, 7}, ¬
{3, 5, 9, 8, 4}, ¬
{1, 3, 7, 9}})))
end run


-------------------- GENERIC FUNCTIONS -------------------

-- concat :: [[a]] -> [a]
on concat(xs)
set ca to current application
((ca's NSArray's arrayWithArray:xs)'s ¬
valueForKeyPath:"@unionOfArrays.self") as list
end concat


-- nub :: [a] -> [a]
on nub(xs)
set ca to current application
((ca's NSArray's arrayWithArray:xs)'s ¬
valueForKeyPath:"@distinctUnionOfObjects.self") as list
end nub


-- sort :: Ord a => [a] -> [a]
on sort(xs)
((current application's NSArray's arrayWithArray:xs)'s ¬
sortedArrayUsingSelector:"compare:") as list
end sort</lang>
{{Out}}
<pre>{1, 3, 4, 5, 7, 8, 9}</pre>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==