-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqsort.ts
25 lines (23 loc) · 828 Bytes
/
qsort.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import {input} from '.';
import {SList, toArray} from './slist';
export function filter<T>(slist: SList<T>, pred: (x: T) => boolean): SList<T> {
return slist.read(list => {
if (!list) {
return input(null);
}
const rest = filter(list.tail, pred);
return pred(list.value) ? input({value: list.value, tail: rest}) : rest;
});
}
export function qsort<T>(slist: SList<T>, rest?: SList<T>): SList<T> {
return slist.read(list => {
if (list === null) {
return rest ? rest : input(null);
}
const pivot = list.value;
const left = filter(list.tail, x => x < pivot);
const right = filter(list.tail, x => x >= pivot);
const half = input({value: pivot, tail: qsort(right, rest)});
return qsort(left, half);
});
}