-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a function to list implemented in dictu which uses quickSort to allow custom sorting which is suuuper useful for a lot of problems. I would have preferred to implement this is C too but there doesn't seam to be a straight forward way to pass callbacks into c. Using a hybrid approach would have been hacky too, but of course this approach does duplicate the sorting logic.
- Loading branch information
Showing
5 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* sortFunc.du | ||
* | ||
* Testing list sorting with custom callback | ||
*/ | ||
from UnitTest import UnitTest; | ||
class A { | ||
init(var name, var n) {} | ||
} | ||
|
||
class TestListSortFunc < UnitTest { | ||
testListSortCallback() { | ||
var list1 = [[1, "Dictu"], [-1, "SomeValue"], [5, "!"], [4, "Awesome"], [2, "is"]]; | ||
list1.sortFunc(def(a,b) => a[0] - b[0]); | ||
this.assertEquals(list1, [[-1, "SomeValue"], [1, "Dictu"], [2, "is"], [4, "Awesome"], [5, "!"]]); | ||
} | ||
testListSortCallbackClass() { | ||
var list = [A("T", 10), A("U", 15), A("C", 5), A("D", 1), A("I", 3)]; | ||
list.sortFunc(def(a,b) => a.n - b.n); | ||
this.assertEquals(list.map(def (entry) => entry.n), [1, 3, 5, 10, 15]); | ||
this.assertEquals(list.map(def (entry) => entry.name), ["D", "I", "C", "T", "U"]); | ||
} | ||
} | ||
TestListSortFunc().run(); |