high-cities-sorted2.taq

$ java -jar taq.jar high-cities-sorted2

Running query high_cities in global scope 
high_cities(name=denver, altitude=5280)
high_cities(name=flagstaff, altitude=6970)
high_cities(name=addis ababa, altitude=8000)
high_cities(name=leadville, altitude=10200)

Description

high-cities-sorted2.taq shows a cursor working to perform an insertion sort, The “high_cities” query produces a list of high cities sorted by elevation. The most notable aspect of cursor usage here is the referencing of the underlying list by relative index

city[1] = city–

The assignment here shuffles the city at the current location to one place higher on the list and then decrements the cursor index. This outcome shows the index [] notation is taken as being relative to the current cursor index.

The bracket notation appears again at the end where, post shuffle, the last city read from the axiom source is inserted in the right location

city[1] = inserted

This works even if the cursor has decremented beyond the start of the list, in which case, the value of it’s index is = -1.


axiom list city (name, altitude)
{"bilene", 1718}
{"addis ababa", 8000}
{"denver", 5280}
{"flagstaff", 6970}
{"jacksonville", 8}
{"leadville", 10200}
{"madrid", 1305}
{"richmond",19}
{"spokane", 1909}
{"wichita", 1305}

// Performs insert sort on high_cities using a cursor
flow insert_sort
{
export list<axiom> high_cities,
cursor city(high_cities)
}
(
// Filter out cities below 5,000 feet
altitude? > 5000,
// Append next city from source
high_cities += axiom high_city { name, altitude },
// Point cursor at city just inserted
-city,
// Save city and point to preceding city in list
inserted = city--,
// Skip case only one city is in the list
? fact city,
// Remember start position
start = city.index(),
// Shuffle list until sort order restored
{
? altitude < city->altitude,
city[1] = city--,
? fact city
},
// All done if inserted city was in sort order
?: city.index() == start,
// Place latest city in correct position
city[1] = inserted
)

query high_cities (city : insert_sort)