Skip to content

Instantly share code, notes, and snippets.

@pkese
Created September 11, 2021 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkese/2e4a593f1ff18c668ea0afd3bab6a5c5 to your computer and use it in GitHub Desktop.
Save pkese/2e4a593f1ff18c668ea0afd3bab6a5c5 to your computer and use it in GitHub Desktop.
Join two tables from Wikipedia to render list of counties sorted by kilometres of paved roads per capita.
#!/usr/bin/dotnet fsi
#r "nuget: FSharp.Data, 4.2.2"
open FSharp.Data
open System
type RoadsHtml = HtmlProvider<"https://en.wikipedia.org/wiki/List_of_countries_by_road_network_size">
let roadsHtml = RoadsHtml.GetSample()
let roads =
[ for row in roadsHtml.Tables.Table1.Rows do
let cname, pavedRoads = row.Column1.Replace("(more)","").Trim(), row.Column4
if not (Double.IsNaN pavedRoads) then
yield cname, pavedRoads
]
|> Map.ofList
type CountriesHtml = HtmlProvider<"https://en.wikipedia.org/wiki/List_of_countries_by_population_(United_Nations)">
let countriesHtml = CountriesHtml.GetSample()
printfn "paved-roads/cap, country, population, paved-roads, region, subregion"
[ for row in countriesHtml.Tables.Contents.Rows ->
let cname, population = row.``Country/Area``, row.``Population (1 July 2019)``
let region, subregion = row.``UN continental region[4]``, row.``UN statistical subregion[4]``
let cname = cname.Split('[').[0].Split('(').[0].Trim()
match roads |> Map.tryFind cname with
| None -> 0.0, cname, population, None, region, subregion
| Some kilometres -> (float kilometres / float population), cname, population, Some kilometres, region, subregion
]
|> List.sortBy (fun (kpc,c,p,k,r,sr) -> -kpc)
|> List.iter (printfn "%A")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment