Jun 13, 2015

Iterations and loops in EasyMorph

Loops in EasyMorph are done in a way typical for functional programming – by iterating through a list of values. In our case – by passing values of a column to another EasyMorph project as parameters, and running that project once per each value.

Example: let's assume that we have Project B that loads data from a file and the file name is defined by a parameter Param1. In order to run Project B for several files, we create Project A with a list of files and iterate (loop) through this list, passing every file name from Project A to Project B as parameter Param1. The picture below illustrates such scenario:

As you can see from this example, Project B is run as many times as the number of values in Table 1 of Project A, i.e. 3 values = 3 iterations.

Transformation "Iterate"

In EasyMorph iterations are arranged using Iterate transformation.

 This transformation has two modes:
  • Iterate – run another project N times, return nothing (the other project is supposed to have at least one export transformation)
  • Iterate and Append – run another project N times,  return N final states of the specified table in that project, and append all of them into one table which becomes a result of the transformation
In our example we passed one field as one parameter, but you can assign multiple fields to multiple parameters. Not assigned parameters have default values.

Design patterns

Iterations is a very versatile mechanism, useful in various cases:
  • Processing a list of files
  • Looping through a range of dates
  • FOR .. NEXT style loops
  • Nested iterations (A runs B that runs C)
  • Reusable logic (A runs C, B runs C)
Note also that the project to run can be specified by a parameter as well. This allows building iterations, where different parts of a list are processed using different projects (requires two levels of iteration).

For those who used to traditional FOR .. NEXT loops iterations might look unusual at first. General design pattern with iterations is to build a list first, and only then iterate through it. For instance: take a list of dates, filter out weekends, then iterate.

Iteration lists can be loaded from an external file or a database. Also, there are several transformations in EasyMorph that generate lists:
  • Calendar – generates a list of dates for specified time period, including optional columns such as day of week, weekend flag, etc.
  • File List – generates a list of files in a specified folder, including optional columns such as file size or date created
  • Folder List – generates a list of folders in a specified folder
  • Sequence – generates a table with one column with numbers from 1 to N
Iterations are available in EasyMorph starting from version 1.8.