# Applications of fold primitives in J

The introduction of fold primitives in J 9.01, later modified in 9.02, increased the expressive power of the language making it easier to write loopless (efficient) code in J. The primer page on Fold primitives puts the new Fold primitives to work and demonstrates how they can be deployed to create elegant solutions to interesting problems.

This article offers more examples on how to apply the fold primitives to create idiomatic and efficient J code.

## Background: Fold, the first primitive of functional code

Map and Reduce, often taking center stage in functional programs, specialize Fold. Therefore, we can construct our own versions of Map and Reduce, to better fit the problem at hand.

## What do the Fold primitives offer over existing primitives?

The table below demonstrates how J's new fold primitives enable new forms for writing concise, loopless code.

## Replicating k's fold primitives

At the August 2020 meeting of NYCJUG, Devon McCormick compared the recent changes in the fold primitives in K9 to J and suggested adding J equivalents. The examples below use k9 (build Aug 2 2020) and J.902.

```   what (k9)           k9 form       k9 example     what (J)             J form          J example          result
---------------------------------------------------------------------------------------------------------------------
each-left            a f\b         (!3)*\!2     table                 x u/ y         (i.3)*/i.2          (0 0;0 1;0 2)
each-right           a f/b         (!3)*/!2     table                 x u/ y         (i.3)(*/~)i.2       (0 0 0;0 1 2)
fold                   f/v            */6 7     insert                  u/ y         */6 7               42
fold w/initial       a f/:v        7*/:11 13    fold single           x u F.. v y    7[F..*11 13         1001
scan                   f\v          -\1 1 1     fold multiple         x u F:. v y    1,[F:.(-~)1 1 1     1 0 -1
scan w/initial       a f\:v        3-\:1 1 1    fold multiple         x u F:. v y    3[F:.(-~)1 1 1      2 1 0
fixpoint               f/:x       (1+1%)/:1     fold single           x u F.. v y    [F..(1+1%])15#1     1.618034
fixpoint scan          f\:x       3 4 2 1\:0    fold multiple cont.   x u F:  v y                        0 3 1 4       ***
do-n               (n;f)/:x     (2;"ha",)/:"!"  fold single rev.      x u F.: v y    '!'[F.:,;2#<'ha'    "haha!"
do-n scan          (n;f)\:x      (3;:x*x)\:2    fold multiple         x u F:. v y     2,2[F:.(]*:)3#1     2 4 16 256
while              (c;f)/:x     (1e3>;2*)/:1    fold single cont.     x u F.  v y     (1e3,2)[F. w 1      1024         **
while scan         (c;f)\:x     (5`mod';2+)\:4  fold multiple cont.   x u F:  v y     4,(5,2)[F: s 4      4 6 8 10     **
e.g. flatten           ,//:     ,//:(1;(2;3 4)) fold single cont.     x u F.  v y     0 [F. f 1;<2;<3 4   1 2 3 4      ***

sv                     b/v        16/2 10       base                    x #. y        16#.2 10           42
vs                     b\x         2\42         antibase                  #: y          #:42             1 0 1 0 1 0
```

** verbs w, s defined:

```w=: dyad define
z [1 Z: *0{x<z=.(1{x)*y
)