Nextflow: Can I parameterize similar processes inside a for loop? I have set of ~5 processes which perform very similar tasks such that their differences can be parameterized. Rather than putting 5 items through some channel (which seems complicated), or defining 5 separate processes, it appears to be possible in Nextflow to declare the processes in a for loop, and output into an array of channels.
Below is a prototypical example and it seems to work as intended. (I understand this could be done differently with some channel cross product operator but I am just giving this as an example of a process in a for loop.)
Is there anything wrong with doing this? Is there a better way to do it?
Bonus question - the name of the process below is &quot;foo&quot;. I would like to have the index reflected in the name i.e. &quot;foo_$i&quot; but this doesn't seem possible by default - any way to do this??
some_inputs = ['prot','dna', 'rna']

some_parameters = [5,3,2,7,1]

// Set up an array of channels to output to
receivers = new ArrayList&lt;Channel&gt;()
for (i = 0; i &lt; 5; i++) {
    receivers.add(Channel.create())
}

// Iterate 5 times to create 5 processes each with different parameters
for (i = 0; i &lt; 5; i++) {

    // WOW - process can be defined inside a for loop!!
    process &quot;foo&quot; {

      // Extract parameters here so correct $i value is used
      p = some_parameters[i]

      input:
      val x from Channel.from(some_inputs)

      output:
      val &quot;$x $p&quot; into receivers[i] // Note that correct index $i seems to be used here

      &quot;&quot;&quot;
      &quot;&quot;&quot;
    }
}

// Confirm that each of the processes outputted independently with the correct parameter
receivers[0].view { &quot;Received[0]: $it" }
receivers[1].view { "Received[1]: $it" }
receivers[2].view { "Received[2]: $it" }
receivers[3].view { "Received[3]: $it" }
receivers[4].view { "Received[4]: $it&quot; }

Output - note that the parameter is consistent within each input channel i.e. working correctly:
N E X T F L O W  ~  version 20.10.0
Launching `play.nf` [fabulous_keller] - revision: c89e2b2281
executor &gt;  local (15)
[10/b477ec] process &gt; foo (3) [100%] 3 of 3 ✔
[16/8282ba] process &gt; foo (2) [100%] 3 of 3 ✔
[db/f7327e] process &gt; foo (1) [100%] 3 of 3 ✔
[fb/886f6f] process &gt; foo (1) [100%] 3 of 3 ✔
[46/c34ed3] process &gt; foo (1) [100%] 3 of 3 ✔
Received[1]: rna 3
Received[1]: prot 3
Received[4]: rna 1
Received[2]: rna 2
Received[0]: dna 5
Received[2]: dna 2
Received[2]: prot 2
Received[0]: prot 5
Received[3]: rna 7
Received[0]: rna 5
Received[1]: dna 3
Received[3]: dna 7
Received[4]: dna 1
Received[3]: prot 7
Received[4]: prot 1
Use channels, this is what nextflow's entire design is based on. I'm honestly surprised that spawning 5 processes with the same name in a loop even works.
Turn your parameters into regular channels, use the combine operator (https://www.nextflow.io/docs/latest/operator.html#combine) to transform the 2 parameter channels into a channel that has each combination of params/inputs. Feed this channel into the  foo  process. Afterward use branch (https://www.nextflow.io/docs/latest/operator.html#branch) to split your foo output channel into 5 different channels.
typechan = Channel.from(5,3,2,7,1)
numchan = Channel.from('prot','dna', 'rna')

cartesian = numchan.combine(typechan)

process foo {
  input:
  val numtype from cartesian
  output:
  val numtype into bar 
  script:
  &quot;&quot;&quot;
  echo $numtype
  &quot;&quot;&quot;
}

bar.branch {
  one: it[1] == 1
  two: it[1] == 2
  three: it[1] == 3 
  five: it[1] == 5
  seven : it[1] == 7
}.set {result}
result.one.view()
```