Last active
July 14, 2020 17:20
-
-
Save Tatsh/d5e9c911f52722520dba2c61c375fa87 to your computer and use it in GitHub Desktop.
Single-expression qsort() implementations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sorted = (xs) => | |
xs.length < 2 | |
? xs | |
: sorted(xs.slice(1).filter((x) => x < xs[0])).concat( | |
[xs[0]], | |
sorted(xs.slice(1).filter((x) => x > xs[0])) | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function sorted($xs) { | |
return count($xs) < 2 ? $xs : array_merge(sorted(array_filter( | |
array_slice($xs, 1), | |
function ($x) { return $x > $xs[0]; } | |
) | |
), | |
[$xs[0]], | |
sorted(array_filter( | |
array_slice($xs, 1), | |
function ($x) { return $x < $xs[0]; } | |
) | |
) | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import List, Union | |
def sorted2(xs: List[Union[float, int]]) -> List[Union[int, float]]: | |
return xs if len(xs) < 2 else sorted2( | |
list(filter(lambda x: x < xs[0], xs[1:]))) + [xs[0]] + sorted2( | |
list(filter(lambda x: x > xs[0], xs[1:]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment