What is the fill() in D language?

Overview

The fill() method is built into the D language. It helps fill a D tuple or range value with a provided value. It does this by assigning values to each element of the input range.

We can use a range or a single value as the filler value. We may also use a range of values, instead of repeatedly using a single value to replace each input range value.

Syntax

fill(range, filler_value)

Parameter

  • range: This is the input range which will be filled with new values replacing its old content. It can’t be empty.

  • filler_value :This is the value assigned to each of the range member.

Return value

Returns an input range that has been filled with the indicated values.

Code

The code snippet below contains some variables used to perform the fill() operation.

import std.algorithm.mutation;
import std.stdio: write, writeln, writef, writefln;
void main() {
int[] a = [ 2, 5, 7, 9, 10 ];
int[] b = [ 4, 1 ];
fill(a, b);
writeln(a); // [4, 1, 4, 1, 4]
}

Explanation

  • Lines 2–3: We import some modules to call some functions for our code.
  • Lines 5–13: We start the primary function and end it.
  • Lines 7–8: We declare some variables.
  • Line 10: We call the fill() method to carry out the fill operation.
  • Line 12: We display the result of the operation.

Free Resources