diff --git a/core/Heap.carp b/core/Heap.carp index 084c633b..14919bb0 100644 --- a/core/Heap.carp +++ b/core/Heap.carp @@ -163,10 +163,16 @@ ; grow our tail, shrinking our head (set! tail (- tail 1))))))) - (doc sort "Perform a heapsort in a new copy of given array.") - (defn sort [arr] + (doc sorted "Perform a heapsort in a new copy of given array.") + (defn sorted [arr] (let-do [narr (Array.copy arr)] (sort! &narr) narr)) + + (doc sort "Perform an in-place heapsort of a given owned array.") + (defn sort [arr] + (do + (sort! &arr) + arr)) ) diff --git a/core/Sort.carp b/core/Sort.carp index c9dd1ba1..c582057c 100644 --- a/core/Sort.carp +++ b/core/Sort.carp @@ -3,7 +3,11 @@ (defn sort! [arr] (HeapSort.sort! arr)) - (doc sort "Perform a heapsort in a new copy of given array.") + (doc sorted "Perform a heapsort in a new copy of given array.") + (defn sorted [arr] + (HeapSort.sorted arr)) + + (doc sort "Perform an in-place heapsort of a given owned array.") (defn sort [arr] (HeapSort.sort arr)) ) diff --git a/test/heap.carp b/test/heap.carp index df4b4400..b6de822c 100644 --- a/test/heap.carp +++ b/test/heap.carp @@ -77,23 +77,29 @@ &exp &arr "HeapSort.sort! works")) - - (let-do [arr [1 3 4 2 6 1] - exp [1 1 2 3 4 6] - res (HeapSort.sort &arr)] + (let-do [res (HeapSort.sort [1 3 4 2 6 1]) + exp [1 1 2 3 4 6]] (assert-equal test &exp &res "HeapSort.sort works")) - ; Check that HeapSort.sort does not modify input array (let-do [arr [1 3 4 2 6 1] exp [1 1 2 3 4 6] - _ (HeapSort.sort &arr)] + res (HeapSort.sorted &arr)] + (assert-equal test + &exp + &res + "HeapSort.sorted works")) + + ; Check that HeapSort.sorted does not modify input array + (let-do [arr [1 3 4 2 6 1] + exp [1 1 2 3 4 6] + _ (HeapSort.sorted &arr)] (assert-equal test &arr &[1 3 4 2 6 1] - "HeapSort.sort does not modify array")) + "HeapSort.sorted does not modify array")) ; walk through HeapSort.sort! step by step (let-do [arr [1 3 4 2 6 1] diff --git a/test/sort.carp b/test/sort.carp index e34cd177..6fcc6004 100644 --- a/test/sort.carp +++ b/test/sort.carp @@ -15,11 +15,19 @@ (let-do [arr [1 3 4 2 6 1] exp [1 1 2 3 4 6] - res (Sort.sort &arr)] + res (Sort.sorted &arr)] + (assert-equal test + &exp + &res + "Sort.sorted works")) + + (let-do [res (Sort.sort [1 3 4 2 6 1]) + exp [1 1 2 3 4 6]] (assert-equal test &exp &res "Sort.sort works")) + (print-test-results test)))