Skip to content

Instantly share code, notes, and snippets.

@deanwampler
Last active December 27, 2020 16:11
Show Gist options
  • Save deanwampler/cfcde0c669280d05630974442b5c49c4 to your computer and use it in GitHub Desktop.
Save deanwampler/cfcde0c669280d05630974442b5c49c4 to your computer and use it in GitHub Desktop.
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