deep, deepr

Prev Next

Function Names

deep, deepr

Description

These functions complement the __deep operators (e.g. +^) where the specified function will be applied on all elements in the parameter sets provided. The depth is also adjustable and is by default 1 (corresponding to operators like +^), but can also be increased (e.g. 2 corresponding to +^^) to process contents in 2-dimensinoal matrices.

Restrictions:

  • No flow control functions
  • Functions taking code pieces must take them as strings. The variables in the code piece are used as given (no deep operations).
  • For functions with I/O parameters, e.g. exchange(), the function itself will be called successfully, but resulting parameters will not be written back.

Allowed: User defined functions.

The differences in the functions deep and deepr come into effect if the size of the parameter sets are different so the remaining elements in the shorter sets need to be repeated or padded with 0 or 1 as long they are not treated as scalars:

deep: Elements are padded with 0 or, if 4th parameter (padding) is available, with the padding value. Example: {1,2} --> {1,2,0,0,0,0} if largest parameter has 6 elements
deepr: Elements are repeated over and over again^. Example: {1,2} --> {1,2,1,2,1,2} if largest parameter has 6 elements. If the number of elements is 0, then padding is used (see deep).

Call as: procedure or function

Restrictions

Indirect parameter passing is disabled
Limited choice of function names, see above

Parameter count

2, 4

Parameters

No.TypeDescription
1
input
string Function name

The name must refer to an existing B4P or user-defined function.

2
input
parameter set Function parameters

At least one element in this parameter set must in turn be a parameter set which will be processed individually.

Opt. 3
input
numeral Depth

The name must refer to an existing B4P or user-defined function.

Opt. 4
input
valid types Padding value

This value will be used to fill up parameter sets so the number of elements are the same before processing them

Default value: 0

Return value

TypeDescription
all types Return value

It is the return value as provided by the specified target function

Examples

  echo(new line, "Processing 2 vectors of equal sizes:");
  a[] = {1..6};  b[] = 7 -^ a[];                  // Output: b[] contains {6,5,4,3,2,1}

  echo( deep ( sum, {a[], b[]} ) );               // {7,7,7,7,7,7}
  echo( deepr( sum, {a[], b[]} ) );               // {7,7,7,7,7,7}

  echo(new line, "Processing 1 vector and 1 scalar:");
  a[] = {1..6};                                   // Output:
  echo( deep( sum, {a[], 10} ) );                 // {11,12,13,14,15,16}, same also for deepr
  echo( deep( sum, {20, a[]} ) );                 // {21,22,23,24,25,26}, same also for deepr


  echo(new line, "Processing with 2 vectors of different sizes:", new line,
                 "Shorter vectors are extended to match length of longest one:");
  a[] = {1..6};  b[] = {10,20};                   // Output:

  echo( deep ( sum, {a[], b[]} ) );               // {11,22,3,4,5,6}  (shorter vector padded with 0)
  echo( deep ( sum, {a[], b[]}, 1, 0) );          // {11,22,3,4,5,6}  (shorter vector padded with 0)
  echo( deep ( sum, {a[], b[]}, 1, 1) );          // {11,22,4,5,6,7}  (shorter vector padded with 1)
  echo( deepr( sum, {a[], b[]} ) );               // {11,22,13,24,15,26} (shorter vector repeated)

  echo( deep ( product, {a[], b[]} ) );           // {10,40,0,0,0,0}
  echo( deep ( product, {a[], b[]}, 1, 0 ) );     // {10,40,0,0,0,0}
  echo( deep ( product, {a[], b[]}, 1, 1 ) );     // {10,40,3,4,5,6}
  echo( deepr( product, {a[], b[]} ) );           // {10,40,30,80,50,120}

  echo(new line, "Example with 3 vectors of different sizes:");
  a[]={1}; b[]={10,11}; c[]={100,101,102};        // Output
  echo( deep ( sum, {a[],b[],c[]} ) );            // {111,112,102}
  echo( deep ( sum, {a[],b[],c[]}, 1, 1 ) );      // {111,113,104}
  echo( deepr( sum, {a[],b[],c[]} ) );            // {111,113,113}

  echo(new line, "Example with 2-dimensional Vectors:");
  a[] = {{1..5},{11..15},{21..25}};
  b[] = a[]*^^2;                                  // b[] contains {{2,4,6,8,10},… } all values doubled
  echo( deep( sum, {a[], 1}, 2 ) );               // {{2,3,4,5,6},{12,13,14,15,16},{22,23,24,25,26}}
  echo( deep( sum, {a[], {1,2,3}},2 ) );          // {{2,3,4,5,6},{13,14,15,16,17},{24,25,26,27,28}}
  echo( deep( sum, {a[],b[]}, 2) );               // {{3,6,9,12,15},{33,36,39,42,45},{63,66,69,72,75}}

Output

Processing 2 vectors of equal sizes:
{7,7,7,7,7,7}
{7,7,7,7,7,7}

Processing 1 vector and 1 scalar:
{11,12,13,14,15,16}
{21,22,23,24,25,26}

Processing with 2 vectors of different sizes:
Shorter vectors are extended to match length of longest one:
{11,22,3,4,5,6}
{11,22,3,4,5,6}
{11,22,4,5,6,7}
{11,22,13,24,15,26}
{10,40,0,0,0,0}
{10,40,0,0,0,0}
{10,40,3,4,5,6}
{10,40,30,80,50,120}

Example with 3 vectors of different sizes:
{111,112,102}
{111,113,104}
{111,113,113}

Example with 2-dimensional Vectors:
{{2,3,4,5,6},{12,13,14,15,16},{22,23,24,25,26}}
{{2,3,4,5,6},{13,14,15,16,17},{24,25,26,27,28}}
{{3,6,9,12,15},{33,36,39,42,45},{63,66,69,72,75}}
Try it yourself: Open LIB_Function_deep.b4p in B4P_Examples.zip. Decompress before use.

See also

call