User:Igor Zhuravlov/Extended forks

From J Wiki
Jump to: navigation, search


Very often a need arises to overcome trains dataflow restrictions. Usual workaround is excessive boxing, i.e. aggregating pieces of data by Link (;), then dealing with boxed data: either extracting it before operating or operating directly inside boxes. This problem may be partially worked around by using generalized forks. Following code may be downloaded as single J script File:Fork.ijs.

2-fork

First of all, let's define traditional fork with 2-depth execution tree, just to eludicate generalized forks idea:

Execution tree

     b
    / \
   a0  a1

Definition

NB. Description:
NB.   2-fork, the traditional fork with 2-depth execution
NB.   graph
NB.
NB. Syntax:
NB.   vapp=. a0`b`a1 fork2
NB.   vapp=. a0`b`a1`:6
NB.   vapp=. a0 b a1
NB. where
NB.   ax   - ambivalent verbs to define input nodes of
NB.          execution graph
NB.   b    - dyad to define output node of execution graph
NB.   vapp - 2-fork, is evoked as:
NB.            out=.   vapp y
NB.            out=. x vapp y

fork2=: 1 : 0
  '`a0 b a1'=. m
  a0o=.     a0 y
  a1o=.     a1 y
  bo=.  a0o b  a1o
:
  '`a0 b a1'=. m
  a0o=. x   a0 y
  a1o=. x   a1 y
  bo=.  a0o b  a1o
)

3-fork

Execution graph

       c
      / \
     b0  b1
    / \ / \
   a0  a1  a2

Definition

NB. Description:
NB.   3-fork, generalized fork with 3-depth execution graph
NB.
NB. Syntax:
NB.   vapp=. a0`b0`a1`c`b1`a2 fork3
NB. where
NB.   ax   - ambivalent verbs to define input nodes of
NB.          execution graph
NB.   bx   - dyads to define intermediate nodes of execution
NB.          graph
NB.   c    - dyad to define output node of execution graph
NB.   vapp - 3-fork, is evoked as:
NB.            out=.   vapp y
NB.            out=. x vapp y
NB.
NB. Notes:
NB. - another 2-fork compatible traverse order is possible:
NB.     a0 b0 c b1 a2 a1

fork3=: 1 : 0
  '`a0 b0 a1 c b1 a2'=. m
  a0o=.     a0 y
  a1o=.     a1 y
  a2o=.     a2 y
  b0o=. a0o b0 a1o
  b1o=. a1o b1 a2o
  co=.  b0o c  b1o
:
  '`a0 b0 a1 c b1 a2'=. m
  a0o=. x   a0 y
  a1o=. x   a1 y
  a2o=. x   a2 y
  b0o=. a0o b0 a1o
  b1o=. a1o b1 a2o
  co=.  b0o c  b1o
)

4-fork

Execution graph

         d
        / \
       c0  c1
      / \ / \
     b0  b1  b2
    / \ / \ / \
   a0  a1  a2  a3

Definition

NB. Description:
NB.   4-fork, generalized fork with 4-depth execution graph
NB.
NB. Syntax:
NB.   vapp=. a0`b0`a1`c0`b1`a2`d`c1`b2`a3 fork4
NB. where
NB.   ax    - ambivalent verbs to define input nodes of
NB.           execution graph
NB.   bx cx - dyads to define intermediate nodes of execution
NB.           graph
NB.   d     - dyad to define output node of execution graph
NB.   vapp  - 4-fork, is evoked as:
NB.            out=.   vapp y
NB.            out=. x vapp y
NB.
NB. Notes:
NB. - another 2-fork compatible traverse order is possible:
NB.     a0 b0 c0 d c1 b2 a3 a1 b1 a2

fork4=: 1 : 0
  '`a0 b0 a1 c0 b1 a2 d c1 b2 a3'=. m
  a0o=.     a0 y
  a1o=.     a1 y
  a2o=.     a2 y
  a3o=.     a3 y
  b0o=. a0o b0 a1o
  b1o=. a1o b1 a2o
  b2o=. a2o b2 a3o
  c0o=. b0o c0 b1o
  c1o=. b1o c1 b2o
  do=.  c0o d  c1o
:
  '`a0 b0 a1 c0 b1 a2 d c1 b2 a3'=. m
  a0o=. x   a0 y
  a1o=. x   a1 y
  a2o=. x   a2 y
  a3o=. x   a3 y
  b0o=. a0o b0 a1o
  b1o=. a1o b1 a2o
  b2o=. a2o b2 a3o
  c0o=. b0o c0 b1o
  c1o=. b1o c1 b2o
  do=.  c0o d  c1o
)

5-fork

Execution graph

           e
          / \
         d0  d1
        / \ / \
       c0  c1  c2
      / \ / \ / \
     b0  b1  b2  b3
    / \ / \ / \ / \
   a0  a1  a2  a3  a4

Definition

NB. Description:
NB.   5-fork, generalized fork with 5-depth execution graph
NB.
NB. Syntax:
NB.   vapp=. a0`b0`a1`c0`b1`a2`d0`c1`b2`a3`e`d1`c2`b3`a4 fork5
NB. where
NB.   axx      - ambivalent verbs to define input nodes of
NB.              execution graph
NB.   bx cx dx - dyads to define intermediate nodes of
NB.              execution graph
NB.   e        - dyad to define output node of execution
NB.              graph
NB.   vapp     - 5-fork, is evoked as:
NB.                out=.   vapp y
NB.                out=. x vapp y
NB.
NB. Notes:
NB. - another 2-fork compatible traverse order is possible:
NB.     a0 b0 c0 d0 e d1 c2 b3 a4 a1 b1 c1 b2 a3 a2

fork5=: 1 : 0
  '`a0 b0 a1 c0 b1 a2 d0 c1 b2 a3 e d1 c2 b3 a4'=. m
  a0o=.     a0 y
  a1o=.     a1 y
  a2o=.     a2 y
  a3o=.     a3 y
  a4o=.     a4 y
  b0o=. a0o b0 a1o
  b1o=. a1o b1 a2o
  b2o=. a2o b2 a3o
  b3o=. a3o b3 a4o
  c0o=. b0o c0 b1o
  c1o=. b1o c1 b2o
  c2o=. b2o c2 b3o
  d0o=. c0o d0 c1o
  d1o=. c1o d1 c2o
  eo=.  d0o e  d1o
:
  '`a0 b0 a1 c0 b1 a2 d0 c1 b2 a3 e d1 c2 b3 a4'=. m
  a0o=. x   a0 y
  a1o=. x   a1 y
  a2o=. x   a2 y
  a3o=. x   a3 y
  a4o=. x   a4 y
  b0o=. a0o b0 a1o
  b1o=. a1o b1 a2o
  b2o=. a2o b2 a3o
  b3o=. a3o b3 a4o
  c0o=. b0o c0 b1o
  c1o=. b1o c1 b2o
  c2o=. b2o c2 b3o
  d0o=. c0o d0 c1o
  d1o=. c1o d1 c2o
  eo=.  d0o e  d1o
)

n-fork

Cases n>5 are left to the reader.

References

1. [Jprogramming] Handling "deep" values in tacit form.