How to use the moveSome() function in D

Overview

The moveSome() function in the D language is part of the std.algorithm.mutation module.

This function moves some value from a tuple value into another tuple value. It does this by simply making a copy of the value in a tuple and storing it inside another tuple.

Example

Let’s suppose we have two tuples, A and B, and wish to move some values of A into B. We can do this using the moveSome() function. The tuple A tuple retains its value, but B now contains a copy of all or some values of A in it.

If the length of B is less than that of A, the moveSome() function returns the leftover values of A after the copy.

Syntax

moveSome(source,target)

Parameters

  • source: This is the tuple value from which we create a copy. Some or all of the values will be copied and moved into another tuple.

  • target: This contains the values that will be moved.

Return value

The moveSome() function returns the leftovers of source after the copy if the length of target is less than that of source.

Code

import std.algorithm.mutation;
import std.stdio: write, writeln, writef, writefln;
void main()
{
// Declare integer arrays.
int[5] alm = [ 1, 2, 3, 4, 5 ];
int[3] balm ;
// Declare string arrays.
string[5] calm = [ "week","young","scala","house","rippen" ];
string[3] palm;
//Move some of alm into balm. Move as much as the length of balm
moveSome(alm[], balm[]);
//Move some of calm into balm. Move as much as the length of palm
moveSome(calm[], palm[]);
writeln(alm[]); // [1, 2, 3, 4, 5] After the move, the value of alm is still the same.
writeln(balm); // [1, 2, 3, 4, 5]
writeln(palm); // ["week", "young", "scala"]
}

Explanation

  • Lines 2–3: We import the modules we need to use in the code.
  • Lines 5–27: We start the main function block.
  • Lines 8–9: We declare some integer variables.
  • Lines 12–13: We declare string array variables.
  • Line 16: We use the moveSome() function to move some of alm into balm. We move as much as the length of the balm array.
  • Line 19: We use moveSome() function to move some of the calm values into palm. We move as much as the length of the palm array.

Free Resources