Last active
December 27, 2020 16:11
-
-
Save deanwampler/cfcde0c669280d05630974442b5c49c4 to your computer and use it in GitHub Desktop.
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
given evenOdd: Ordering[Int] = new Ordering[Int]: | |
def compare(i: Int, j: Int): Int = i%2 compare j%2 match | |
case 0 => i compare j | |
case c => -c | |
// Equivalent to the following form: | |
// given evenOdd2: Ordering[Int] with | |
// def compare(i: Int, j: Int): Int = i%2 compare j%2 match | |
// case 0 => i compare j | |
// case c => -c | |
val seq = SortableSeq(Seq(1,3,5,2,4)) | |
val expected = SortableSeq(Seq(4, 2, 5, 3, 1)) | |
assert(seq.sortBy1a(i => -i) == expected) | |
assert(seq.sortBy1b(i => -i) == expected) | |
assert(seq.sortBy2(i => -i) == expected) | |
assert(seq.sortBy1a(i => -i)(using evenOdd) == expected) | |
assert(seq.sortBy1b(i => -i)(using evenOdd) == expected) | |
assert(seq.sortBy2(i => -i)(using evenOdd) == expected) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment