« Home » « Learn » « Download » « Github »

logo

Cello High Level C

Methods

map

#define map(I, F)

Construct a Map object on the stack over iterable I applying function F.

Examples

Usage

var convert_to_int(var x) {
  var y = new(Int);
  look_from(y, x, 0);
  return y;
}

var x = tuple($S("1"), $S("2"), $S("3"));

foreach (y in map(x, $(Function, convert_to_int))) {
  show(y); /* 1, 2, 3 */
};

Usage 2

var print_object(var x) {
  println("Object %$ is of type %$", x, type_of(x));
  return NULL;
}

var x = tuple($I(0), $S("Hello!"), $F(2.4));

call(map(x, $(Function, print_object)));

Map


Apply Function to Iterable

The Map type is an iterable that applies some callable to to each item in another iterable and returns the result. This can be useful to make more concise iteration when there are callback functions available.

If the mapping callable is a purely side-effect callable it is possible to use the call function on the Map object directly for a quick way to perform the iteration.

One downside of Map is that the iter_type becomes unknown (there is no way to know what type the callable will return so some objects such as Arrays may revert to using Ref as the object type when assigned a Map.

Definition

struct Map {
  var iter;
  var curr;
  var func;
};

Derives

Implements

  • Callcall
  • Docname brief description definition
  • Getget set mem rem key_type val_type
  • Iterforeach iter_init iter_next iter_type
  • Lenlen
  • Newnew del construct destruct

Back