Ink by Example — Sorting

Ink by Example: Sorting

The quicksort library provides a minimal Quicksort implementation that uses Hoare's partitioning.

std := load('../vendor/std')
log := std.log

qs := load('../vendor/quicksort')
sortBy := qs.sortBy
sort! := qs.sort!
sort := qs.sort


We can sort a list in ascending order with quicksort.sort.

numbers := [921, 14, 0, 0.002]
log(sort(numbers))

Function definitions that end in ! mutate their arguments — like quicksort.sort!.

temperatures := [33, 1, 5, 22]
sort!(temperatures)

` the existing list has changed `
log(temperatures)

quicksort.sortBy allows us to pass a predicate — a function that each item should be measured with.

menu := {
    apples: 1.50
    oranges: 2.00
    coconut: 3.50
    grapes: 1.75
}
items := ['apples', 'oranges', 'coconut', 'grapes']

` sort items by their menu price `
log(sortBy(items, item => menu.(item)))

$ ink sorting.ink
{1: 0.00200000, 2: 14, 3: 921, 0: 0}
{0: 1, 1: 5, 2: 22, 3: 33}
{1: 'grapes', 2: 'oranges', 3: 'coconut', 0: 'apples'}

Next example: Execing Processes.