changed "@array::sorted" to something that makes more sense
@@ -398,17 +398,18 @@ $.assert(arr2.sum(a => a.length) == 9)
return truthy.sort(comp = comp) + [pivot] + falsy.sort(comp = comp) // heskell quicksort goes brr
},
- sorted: #[desc("Returns a sorted verison of the array") example("
-arr = [5, 1, 5, 3, 2]
-$.assert(arr.sorted() == [1, 2, 3, 5, 5])
-$.assert(arr.sorted(begin = 2, end = 4) == [5, 1, 2, 3, 5])
-$.assert(arr.sorted(comp = (a, b) => a >= b) == [5, 5, 3, 2, 1])
+ sorted: #[desc("Returns true if the array is sorted.") example(u"
+ $.assert([5, 1, 5, 3, 2].sorted() == false)
+ $.assert([1, 2, 3, 5, 5].sorted() == true)
+ $.assert([5, 5, 3, 2, 1].sorted(comp = (a, b) => a >= b) == true)
")]
- (self, begin: @number = 0, end: @number = -1, comp: (_, _) -> @bool = (a, b) => a <= b) -> @array {
- let new_arr = self
- new_arr.sort(begin,end,comp)
-
- return new_arr
+ (self, comp: (_, _) -> @bool = (a, b) => a <= b) -> @bool {
+ for i in 1..self.length {
+ if !comp(self[i-1], self[i]) {
+ return false
+ }
+ }
+ return true
},
shift: #[desc("Removes the first index from the array and returns it.") example("
