sourcelibs/Csv/demos/schema.xtl
1⍝!/usr/bin/env xetal
2⍝# A table's columns as named, typed variables: c_olumns< reads the
3⍝# schema when the program is compiled and writes a definition per
4⍝# column, so the program speaks of city and population, and their
5⍝# kinds are checked like any variable's. Run with "just demo Csv schema".
6
7ᶜˢ⁼u̲se< "Csv"
8ˢ⁼u̲se< "Stats"
9t ← ᶜˢr̲ows @ i̲nclude< "cities.csv" ⍝ the data, built in from the file beside this program
10"city:text population:number area:number" ᶜˢc̲olumns< "t"
11city
12population ÷ area ⍝ people per square kilometre
13d̲isclose (f̲irst g̲rade n̲eg population ÷ area) s̲elect city ⍝ the densest
14ˢm̲ean population
i̲nclude< expands to
("city,country,population,area\nOslo,Norway,709037,454\n\"Washington, D.C.\",USA,678972,177\nLima,Peru,10092000,2672\nPerth,Australia,2192229,6418\nTurin,Italy,848885,130\n")ᶜˢc̲olumns< expands to
city ← (1 d̲rop ((f̲irst t) i̲ndexOf e̲nclose "city") s̲elect₂ t) population ← n̲umbers d̲isclose '{ e̲nclose (d̲isclose ⍺) c̲at " " c̲at d̲isclose ⍵ } r̲/ (1 d̲rop ((f̲irst t) i̲ndexOf e̲nclose "population") s̲elect₂ t) area ← n̲umbers d̲isclose '{ e̲nclose (d̲isclose ⍺) c̲at " " c̲at d̲isclose ⍵ } r̲/ (1 d̲rop ((f̲irst t) i̲ndexOf e̲nclose "area") s̲elect₂ t)