Dart Runes map()
Syntax & Examples

Runes.map() method

The `map` method in Dart returns a new lazy iterable with elements that are created by applying a function to each element of the original iterable.


Syntax of Runes.map()

The syntax of Runes.map() method is:

 Iterable<T> map<T>(T f(int e)) 

This map() method of Runes returns a new lazy Iterable with elements that are created by calling f on each element of this Iterable in iteration order.

Parameters

ParameterOptional/RequiredDescription
frequiredA function that takes an integer element as input and returns a new element of type T.

Return Type

Runes.map() returns value of type Iterable<T>.



✐ Examples

1 Map characters to their lengths

In this example,

  1. We create a sequence of Unicode code points runes from the string 'Hello'.
  2. We use the map method to apply a function that returns the length of each character as a new element.
  3. We then print the resulting iterable to standard output.

Dart Program

void main() {
  Runes runes = Runes('Hello');
  Iterable<int> lengths = runes.map((element) => element.toString().length);
  print('Lengths of each character: $lengths');
}

Output

Lengths of each character: (2, 3, 3, 3, 3)

2 Map numbers to their doubled values as strings

In this example,

  1. We create a sequence of Unicode code points numbers from the string '12345'.
  2. We use the map method to apply a function that doubles each number and converts it to a string.
  3. We then print the resulting iterable to standard output.

Dart Program

void main() {
  Runes numbers = Runes('12345');
  Iterable<String> doubledNumbers = numbers.map((element) => (element * 2).toString());
  print('Doubled numbers: $doubledNumbers');
}

Output

Doubled numbers: (98, 100, 102, 104, 106)

Summary

In this Dart tutorial, we learned about map() method of Runes: the syntax and few working examples with output and detailed explanation for each example.