Skip to content

Sub

Name - Description Default Type
<input> The input value passed to this Sub flow (and hence to the shard or sequence of shards in this Sub flow). Any
<output> The output of this Sub flow (which is the same as its input). Any
Shards The shard or sequence of shards to execute in the Sub flow. None Shard[ Shard ]

Activates a shard or a sequence of shards independently, without consuming the input. I.e. the input of the Sub flow will also be its output regardless of the shards activated in this Sub flow.

Details

In Shards a shard receives an input, processes it, and produces an output. Usually this output is different from the input that the shard processed.

However, in some cases, we want a shard (or a sequence of shards) to return the same value as its output that it received as an input. For example, when we want a shard to process the same input that its previous shard (or sequence of shards) processed, irrespective of the output created by the previous shard(s).

There is a pass-through parameter available for some shards (like Match) that can allows these shards to pass-through their own input as their output. For other shards, we can use the Sub shard (also called the Sub flow) to acheive the same effect by wrapping the target shard(s) within the Sub .

When a Sub shard recieves an input, it passes that to the first shard contained within it. This shard processes the input and creates its own ouput that is then passed to the next shard in the sequence inside the Sub flow. This continues till the last shard in the flow is reached. The output of this last shard is not used by the Sub flow. Instead the Sub flow outputs the same value that it had received as input.

While Sub may seem to not use any of its internal shards' inputs or outputs, every variable assigned inside the Sub will be available outside as well since the Sub will always be executed. Hence, the inputs and outputs of the shards within the Sub flow can be made available outside the Sub by saving them into Sub variables.

As a result, this method gives a psuedo pass-through capabilty to any shard (if you wrap just that one shard with a Sub shard) or to any sequence of shards (if you wrap a whole sequence of shards within a Sub shard).

By nesting Sub shards you can simulate even more flexible and powerful shard execution paths.

Note

Sub has an alias | which is more convenient to use. | also removes the need to use -> as, unlike Sub, it doesn't require the parameter shards to be grouped together.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
;; Sub container shard + nesting
(int 5)
(Log "input to Sub1") ;; => 5
(Sub ;; Sub1: a set of shard sequences
 ;:Shards
 (->
  (Math.Multiply 2)
  (Assert.Is 10 true)
  (Log "Sub1 inner shard o/p | 5 * 2") ;; => 10
  ))
(Log "Sub1 output => input to Sub2") ;; => 5
(Sub  ;; Sub2: another set of shard sequences
 ;:Shards
 (->
  (Math.Multiply 3)
  (Assert.Is 15 true)
  (Log "Sub2 inner shard o/p | 5 * 3") ;; => 15
  (Log "input to nested-Sub") ;; => 15
  (Sub ;; nesting of Sub shards
   ;:Shards
   (->
    (Math.Multiply 2)
    (Assert.Is 30 true)
    (Log "nested-Sub inner shard o/p | (5 * 3) * 2") ;; => 30
    ))
  (Log "output from nested Sub") ;; => 15
  ))
(Log "Sub2 output => output") ;; => 5
[info] [shards/General/Sub/1.edn] input to Sub1: 5
[info] [shards/General/Sub/1.edn] Sub1 inner shard o/p | 5 * 2: 10
[info] [shards/General/Sub/1.edn] Sub1 output => input to Sub2: 5
[info] [shards/General/Sub/1.edn] Sub2 inner shard o/p | 5 * 3: 15
[info] [shards/General/Sub/1.edn] input to nested-Sub: 15
[info] [shards/General/Sub/1.edn] nested-Sub inner shard o/p | (5 * 3) * 2: 30
[info] [shards/General/Sub/1.edn] output from nested Sub: 15
[info] [shards/General/Sub/1.edn] Sub2 output => output: 5

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
;; Using `Sub`, with `->`
(int 5)                                ;; input to `Sub` shards
(Sub (-> (Math.Multiply 2)
         (Log)))                       ;; 5 * 2 => 10
(Sub (-> (Math.Multiply 3)
         (Log)                         ;; 5 * 3 => 15
         (Sub (-> (Math.Multiply 2)
                  (Log)))))            ;; 15 * 2 => 30
(Log "output of the last `Sub` shard") ;; input is output => 5

;; Using `|`, no need for `->`
(int 100)                              ;; input to `|` shards
(| (Math.Multiply 2)
   (Log))                              ;; 100 * 2 => 200
(| (Math.Multiply 3)
   (Log)                               ;; 100 * 3 => 300
   (| (Math.Multiply 2)
      (Log)))                          ;; 300 * 2 => 600
(Log "output of the last `|` shard")   ;; input is output => 100
[info] [shards/General/Sub/2.edn] 10
[info] [shards/General/Sub/2.edn] 15
[info] [shards/General/Sub/2.edn] 30
[info] [shards/General/Sub/2.edn] output of the last `Sub` shard: 5
[info] [shards/General/Sub/2.edn] 200
[info] [shards/General/Sub/2.edn] 300
[info] [shards/General/Sub/2.edn] 600
[info] [shards/General/Sub/2.edn] output of the last `|` shard: 100