F-Flat: Quicksort

node v8.17.0
version: 1.0.2
endpointsharetweet
const { Stack } = require('@hypercubed/f-flat@0.0.6'); const f = new Stack(); f.eval(` core: rcl use types: rcl use shuffle: rcl use `).stack;
First we define quicksort and some helpers. These will be part of the core lib in the next release.
f.eval(` lfilter: [ [ keepif ] >> map ] ; sfilter: [ lfilter '' << sum ] ; filter: [ over string? [sfilter] [lfilter] branch ] ;; xxs: [ [ first ] [ rest ] bi ] ; quicksort: [ dup length 1 <= // if length < 1 [ ] // do nothing [ // otherwise: xxs // split into first, rest over dup // shuffle [ [ <= ] >> filter ] >> // filter values less than or equal to pivot swap [ [ > ] >> filter ] >> // filter values greater than pivot bi [ quicksort ] bi@ // quicksort each sublist [ swap + ] dip + // merge results ] branch ] ; `).stack;
Works with arrays:
f.eval(` [ 10 2 5 3 1 6 7 4 2 3 4 8 9 ] quicksort `).stack.toString();
And strings:
f.eval(` clr 'the quick brown fox jumps over the lazy dog' quicksort `).stack.toString();
Loading…

no comments

    sign in to comment