13.4.3 Collect statistical information about the region
Our goal is to show the changes in forest area in different regions since 1990. We need to traverse all existing areas to check if the data is available and find the downloaded indicator values. This can be done very easily by using the mappings we created, because the year and region IDs have been used as keys.
But be careful, because there are some data that may be missing, so we want to filter out areas with no data for all years. In addition, we want to show the total forest area, not the percentage, so a simple calculation is done before the data is returned. Although it may sound difficult, the code is not very complex. Listing 13.17 shows the final few commands we want to enter in F # Interactive to get the data we want to collect.
Listing 13.17 Calculating forest area (F # Interactive)
> LetCalculateforests (area:float<km^2Forest:float<percent>) = <--[1] Calculate total forest area LetForestarea = Forest * Area Forestarea/100.0<percent>;ValCalculateforests:float<km ^2> *float<percent>float<km ^2>> Letyears = [1990; -;2005] LetDataavailable (key) = <--[2] For a given key, is the value available? Years |> Seq.forall ( FunY–> (Map.contains (Y, key) areas) && (Map.contains (Y, key) forests));ValYears:int List ValDataavailable:string–>BOOL> LetGetforestdata (key) = <--[3] Get the yearly value [| forYinchYears DoYield Calculateforests (areas.[ Y, key], forests. [Y, Key]) |];;ValGetforestdata:string-float<km ^2>Array> LetStats = seq {<--[4] For all regions, find available data forNameinchRegions Do ifDataavailable (name) ThenYield name, Getforestdata (name)};;Valstats:seq<string*float<km ^2>Array>
Listing 13.17 defines several auxiliary functions, processes the downloaded data, and defines a value named stats (statistics) that contains the final result. With the unit of measurement, it is easy to see what the first function did [1], which calculates the total area of the forest, in square kilometres, using the total area of the area and the percentage of forest area.
The second function [2] examines the data we need for the specified region ID, and whether three years of interest are available. It uses the function map.contains to test whether F # mappings (described by the second argument) contain the key specified by the first parameter value. The last auxiliary function [3] looks similar to the second one, which assumes that the data is available, uses the year and region names as keys, extracts the data from the map for all monitored years, and then uses the first function to calculate the forest area from the original data.
With the last two functions, we can collect statistics for all regions [4]. The return value is a sequence of tuples, the first element of the tuple is a region name, and the second element is an array. The array has three elements that correspond to the values that are monitored for the three year.
After you get the data in F # Interactive, you can see it, but in the interactive window, it's hard to look at various patterns by outputting data. To be able to get more information from the collected data, you must visualize the data in a more friendly way, for example, by using Microsoft Excel.
13.4.3 Collect statistical information about the region