mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-01-17 14:18:13 +01:00
add reverse param
This commit is contained in:
parent
c9213aaead
commit
63110174f4
@ -21,10 +21,10 @@ function _sorted(lt, less, elem = identity) =
|
||||
)
|
||||
[each _sorted(b_a[0], less, elem), elem(pivot), each _sorted(b_a[1], less, elem)];
|
||||
|
||||
function _sorted_default(lt) = _sorted(lt, function(a, b) a < b);
|
||||
function _sorted_default(lt, reverse) = _sorted(lt, reverse ? function(a, b) a > b : function(a, b) a < b);
|
||||
|
||||
function _sorted_cmp(lt, cmp) = _sorted(lt, function(a, b) cmp(a, b) < 0);
|
||||
function _sorted_cmp(lt, cmp, reverse) = _sorted(lt, reverse ? function(a, b) cmp(a, b) > 0 : function(a, b) cmp(a, b) < 0);
|
||||
|
||||
function _sorted_key(lt, key) =
|
||||
function _sorted_key(lt, key, reverse) =
|
||||
let(key_elem_lt = [for(elem = lt) [key(elem), elem]])
|
||||
_sorted(key_elem_lt, function(a, b) a[0] < b[0], function(elem) elem[1]);
|
||||
_sorted(key_elem_lt, reverse ? function(a, b) a[0] > b[0] : function(a, b) a[0] < b[0], function(elem) elem[1]);
|
@ -10,8 +10,8 @@
|
||||
|
||||
use <_impl/_sorted_impl.scad>;
|
||||
|
||||
function sorted(lt, cmp = undef, key = undef) =
|
||||
function sorted(lt, cmp = undef, key = undef, reverse = false) =
|
||||
let(is_cmp_undef = is_undef(cmp))
|
||||
is_cmp_undef && is_undef(key) ? _sorted_default(lt) :
|
||||
is_cmp_undef ? _sorted_key(lt, key) :
|
||||
_sorted_cmp(lt, cmp);
|
||||
is_cmp_undef && is_undef(key) ? _sorted_default(lt, reverse) :
|
||||
is_cmp_undef ? _sorted_key(lt, key, reverse) :
|
||||
_sorted_cmp(lt, cmp, reverse);
|
@ -4,8 +4,10 @@ module test_sorted() {
|
||||
echo("==== test_sorted ====");
|
||||
|
||||
assert([1, 2, 3, 4, 5, 6] == sorted([1, 6, 2, 5, 4, 3]));
|
||||
assert([6, 5, 4, 3, 2, 1] == sorted([1, 6, 2, 5, 4, 3], reverse = true));
|
||||
|
||||
assert(["b", "c", "d", "m", "x"] == sorted(["x", "c", "b", "d", "m"]));
|
||||
assert(["x", "m", "d", "c", "b"] == sorted(["x", "c", "b", "d", "m"], reverse = true));
|
||||
|
||||
assert(
|
||||
[[2, 0, 0], [5, 0, 0], [7, 0, 0], [9, 0, 0], [10, 0, 0]] ==
|
||||
@ -20,12 +22,18 @@ module test_sorted() {
|
||||
ascending = function(e1, e2) e1 - e2;
|
||||
descending = function(e1, e2) e2 - e1;
|
||||
assert(sorted([2, 1, 3, 5, 4], ascending) == [1, 2, 3, 4, 5]);
|
||||
assert(sorted([2, 1, 3, 5, 4], ascending, reverse = true) == [5, 4, 3, 2, 1]);
|
||||
assert(sorted([2, 1, 3, 5, 4], descending) == [5, 4, 3, 2, 1]);
|
||||
|
||||
assert(
|
||||
[[2, 0, 0], [5, 0, 0], [7, 0, 0], [9, 0, 0], [10, 0, 0]] ==
|
||||
sorted([[10, 0, 0], [5, 0, 0], [7, 0, 0], [2, 0, 0], [9, 0, 0]], key = function(elem) elem.x)
|
||||
);
|
||||
|
||||
assert(
|
||||
[[10, 0, 0], [9, 0, 0], [7, 0, 0], [5, 0, 0], [2, 0, 0]] ==
|
||||
sorted([[10, 0, 0], [5, 0, 0], [7, 0, 0], [2, 0, 0], [9, 0, 0]], key = function(elem) elem.x, reverse = true)
|
||||
);
|
||||
}
|
||||
|
||||
test_sorted();
|
||||
|
Loading…
x
Reference in New Issue
Block a user