-
Notifications
You must be signed in to change notification settings - Fork 1
Map
Note: the syntax described on this page is available from GeoDMS version 20.9.0 and later.
higher-order functions map combinator
- map(F, src)
- map(F(a,
_), src) or map(F(_, a), src)
map(F, src) applies the one-argument function F to each subitem of the container src, producing a container of results. Each child of src is passed to F in turn, and the corresponding result becomes a child of the same name in the output container:
container out := map(Halve, src);
Here F (Halve) is a function taking a single argument; src is a container whose data-item children are mapped over. The children of out are first-class attributes: out/a, out/b, ... can be read directly or copied into a declared attribute.
A generic function may be mapped as well; the type variable is resolved per child:
function DoubleG<V: numerics>(attribute<V> y (Road)) -> attribute<V> (Road) := y + y;
container dbl := map(DoubleG, src);
When F takes more than one argument, supply a partial application as the first argument to map. Fix all arguments but one, and write _ for the hole that each mapped child fills. The fixed argument may be an item reference or a literal. The hole may be last or first:
container scaled := map(Scale(k, _), src); // hole LAST, fixed arg = an item reference
container scaledR := map(ScaleR(_, k), src); // hole FIRST
container scaledL := map(Scale(3.0, _), src); // fixed arg = a literal
with
function Scale (parameter<float64> factor; attribute<float64> x (Road)) -> attribute<float64> (Road) := x * factor;
function ScaleR(attribute<float64> x (Road); parameter<float64> factor) -> attribute<float64> (Road) := x * factor;
A partial application must leave exactly one _ hole for the mapped element. Two holes is rejected:
container bad := map(Scale(_, _), src); // error: "must leave exactly one '_' hole"
-
F: a one-argument function, or a partial application of a multi-argument function with a single
_hole. - src: a container whose subitems are passed, one at a time, to F.
container src
{
attribute<float64> a (Road) := Road/flow;
attribute<float64> b (Road) := Road/flow * 2.0;
}
function Halve(attribute<float64> x (Road)) -> attribute<float64> (Road) := x / 2.0;
container out := map(Halve, src);
attribute<float64> out_a (Road) := out/a; // = [50, 100, 25, 200], each flow value halved
attribute<float64> out_b (Road) := out/b;
out has children a and b, each the halved counterpart of the matching child in src.
- Function-definition
- Closure
- Generic-function
- Container
-
MetaScript functions (the older
for_eachapproach)
20.9.0
GeoDMS ©Object Vision BV. Source code distributed under GNU GPL-3. Documentation distributed under CC BY-SA 4.0.