Category talk:Wren-sort: Difference between revisions

m
→‎Source code: Now uses Wren S/H lexer.
(→‎Source code: Made several methods 'chaining' friendly.)
m (→‎Source code: Now uses Wren S/H lexer.)
 
(4 intermediate revisions by the same user not shown)
Line 1:
===Source code===
 
<langsyntaxhighlight ecmascriptlang="wren">/* Module "sort.wren" */
import "./trait" for Comparable
 
Line 298:
static isSorted(a) { isSorted(a, false) }
static isSortedDesc(a) { isSorted(a, true) }
 
// Reverses a list in place and returns it.
static reverse(a) {
var c = a.count
if (c < 2) return a
var i = 0
var j = a.count - 1
while (i < j) {
var t = a[i]
a[i] = a[j]
a[j] = t
i = i + 1
j = j - 1
}
return a
}
}
 
Line 370 ⟶ 354:
return (t[1] > 0) ? t[2].to : -1
}
 
// Works similarly to 'all' but only returns the index of the first match
// or the index at which it would need to be inserted if there were no matches at all.
static nearest(a, value, cmp) { all(a, value, cmp)[2].from }
 
// Finds the lowest value in an unsorted list according to 'cmp' but without sorting.
Line 470 ⟶ 458:
 
// Convenience versions of the above which use default values for the 'cmp' parameter.
static all(a, value) { all(a, value, false) }
static first(a, value) { first(a, value, false) }
static last(a, value) { last(a, value, false) }
static lowestnearest(a, value) { lowestnearest(a, value, false) }
static highestlowest(a) { highestlowest(a, false) }
static quickhighest(a, k) { quickhighest(a, k, false) }
static quick(a, k) { quick(a, k, false) }
 
// Finds the median element(s) of a sorted list.
Line 695 ⟶ 684:
// in the sorted list. Returns -1 if not found.
lastIndexOf(value) { Find.last(_lst, value, _cmp) }
 
// Uses binary search to find the index of the first occurence of 'value'
// in the sorted list. Returns the index at which it would need to be inserted
// if not found.
nearestIndexOf(value) { Find.nearest(_lst, value, _cmp) }
 
// Iterator protocol methods.
Line 718 ⟶ 712:
// Returns the string representation of 'this'.
toString { _lst.toString }
}</langsyntaxhighlight>
9,476

edits