Common sorted list: Difference between revisions
Content added Content deleted
(→{{header|Python}}: Added a Python version.) |
|||
Line 212: | Line 212: | ||
{1,3,4,5,7,8,9,"an","everything","integer","is","not"} |
{1,3,4,5,7,8,9,"an","everything","integer","is","not"} |
||
</pre> |
</pre> |
||
=={{header|Python}}== |
|||
<lang python>'''Common sorted list''' |
|||
from itertools import chain |
|||
# ------------------------- TEST ------------------------- |
|||
# main :: IO () |
|||
def main(): |
|||
'''Sorted union of lists''' |
|||
print( |
|||
sorted(nub(concat( |
|||
[ |
|||
[5, 1, 3, 8, 9, 4, 8, 7], |
|||
[3, 5, 9, 8, 4], |
|||
[1, 3, 7, 9] |
|||
] |
|||
))) |
|||
) |
|||
# ----------------------- GENERIC ------------------------ |
|||
# concat :: [[a]] -> [a] |
|||
# concat :: [String] -> String |
|||
def concat(xs): |
|||
'''The concatenation of all the elements in a list. |
|||
''' |
|||
return list(chain(*xs)) |
|||
# nub :: [a] -> [a] |
|||
def nub(xs): |
|||
'''A list containing the same elements as xs, |
|||
without duplicates, in the order of their |
|||
first occurrence. |
|||
''' |
|||
return list(dict.fromkeys(xs)) |
|||
# MAIN --- |
|||
if __name__ == '__main__': |
|||
main()</lang> |
|||
{{Out}} |
|||
<pre>[1, 3, 4, 5, 7, 8, 9]</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |