Common sorted list: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: Added a Haskell version) |
(→{{header|JavaScript}}: Added a version in JavaScript) |
||
Line 189: | Line 189: | ||
{{Out}} |
{{Out}} |
||
<pre>[1,3,4,5,7,8,9]</pre> |
<pre>[1,3,4,5,7,8,9]</pre> |
||
=={{header|JavaScript}}== |
|||
<lang javascript>(() => { |
|||
"use strict"; |
|||
// --------------- COMMON SORTED LIST ---------------- |
|||
// commonSorted :: Ord a => [[a]] -> [a] |
|||
const commonSorted = xs => |
|||
sort(nub(concat(xs))); |
|||
// ---------------------- TEST ----------------------- |
|||
const main = () => |
|||
commonSorted([ |
|||
[5, 1, 3, 8, 9, 4, 8, 7], |
|||
[3, 5, 9, 8, 4], |
|||
[1, 3, 7, 9] |
|||
]); |
|||
// --------------------- GENERIC --------------------- |
|||
// concat :: [[a]] -> [a] |
|||
const concat = xs => [].concat(...xs); |
|||
// nub :: [a] -> [a] |
|||
const nub = xs => [...new Set(xs)]; |
|||
// sort :: Ord a => [a] -> [a] |
|||
const sort = xs => |
|||
// An (ascending) sorted copy of xs. |
|||
xs.slice().sort(); |
|||
return main(); |
|||
})();</lang> |
|||
{{Out}} |
|||
<pre>[1, 3, 4, 5, 7, 8, 9]</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |