Category talk:Wren-sort: Difference between revisions

→‎Source code: Removed Comparable class which is now being imported from new Wren-trait module.
m (→‎Source code: Changed a method name.)
(→‎Source code: Removed Comparable class which is now being imported from new Wren-trait module.)
Line 1:
===Source code===
 
<lang ecmascript>/* Module "sortcmp.wren" */
import "/trait" for Comparable
 
/*
Line 419 ⟶ 420:
static highest(a) { highest(a, false) }
 
// Finds the middlemedian element(s) of a sorted list.
// Returns a list of three items:
// The first item is a list of the middlemedian element(s).
// The second item is the number of middlemedian element(s).
// The third item is the range of indices at which the middlemedian element(s) occur.
static middlemedian(a) {
Sort.isList_(a)
var c = a.count
if (c == 0) Fiber.abort("An empty list does not have a middlemedian element.")
var hc = (c/2).floor
return (c%2 == 1) ? [[a[hc]], 1, hc..hc] : [[a[hc-1], a[hc]], 2, hc-1..hc]
}
}
 
/*
Comparable is an abstract class which enables child classes to automatically
inherit the comparison operators by just overriding the 'compare' method.
*/
class Comparable {
compare(other) {
// This should be overridden in child classes to return -1, 0 or +1
// depending on whether this < other, this == other or this > other.
}
 
< (other) { compare(other) < 0 }
> (other) { compare(other) > 0 }
<=(other) { compare(other) <= 0 }
>=(other) { compare(other) >= 0 }
==(other) { compare(other) == 0 }
!=(other) { compare(other) != 0 }
}
 
Line 454 ⟶ 437:
var Cmp_Cmp = Cmp
var Cmp_Sort = Sort
var Cmp_Find = Find</lang>
var Cmp_Comparable = Comparable</lang>
9,485

edits