User:Oleg Kobchenko/Ways to Scan

From J Wiki
Jump to navigation Jump to search

There is more than one way to scan a kit.


Referred to as data amalgamation [1], these operations apply a verb to consecutive subsets of the argument.

This is a follow up on some ideas about scanning and order visualization by Jose Mario Quintana JForum:programming/2006-December/004368.

Overview

We are going to use verb c for concatenation (append) with parentheses to see order of application.

   c=. '(' , [ , ')' ,~ ]
   'a' c 'b' c 'c'
(a(bc))

This immediately demonstrates the order of execution in J.

Insert

Adverb insert is called reduce in APL [2].

Here are basic transformations to affect the order of execution and the position of the arguments: passive and reverse.

   c/ 'abcde'       c~/ 'abcde'       c/@|. 'abcde'        c~/@|. 'abcde'
(a(b(c(de))))    ((((ed)c)b)a)     (e(d(c(ba))))        ((((ab)c)d)e)

Prefix

Accummulating results of insert is done with additional adverb prefix applied to insert to produce the effect of APL scan [3].

   c/\ 'abcde'      c~/\ 'abcde'      c/\&.|. 'abcde'      c~/\&.|. 'abcde'
a                a                 (e(d(c(ba))))        ((((ab)c)d)e)
(ab)             (ba)              (e(d(cb)))           (((bc)d)e)
(a(bc))          ((cb)a)           (e(dc))              ((cd)e)
(a(b(cd)))       (((dc)b)a)        (ed)                 (de)
(a(b(c(de))))    ((((ed)c)b)a)     e                    e

Suffix

In J, it is also possible to scan backwards with adverb suffix. Note how the results are nested or reusable.

   c/\. 'abcde'     c~/\. 'abcde'     c/\.&.|. 'abcde'     c~/\.&.|. 'abcde'
(a(b(c(de))))    ((((ed)c)b)a)     a                    a
(b(c(de)))       (((ed)c)b)        (ba)                 (ab)
(c(de))          ((ed)c)           (c(ba))              ((ab)c)
(de)             (ed)              (d(c(ba)))           (((ab)c)d)
e                e                 (e(d(c(ba))))        ((((ab)c)d)e)

Infix

Another way to traverse a list if infix.

   2 c/\ 'abcde'    2 c~/\ 'abcde'    2 c/\|. 'abcde'      2 c~/\|. 'abcde'
(ab)             (ba)              (ed)                 (de)
(bc)             (cb)              (dc)                 (cd)
(cd)             (dc)              (cb)                 (bc)
(de)             (ed)              (ba)                 (ab)

Non-overlapping Infix

A negative window parameter applies to non-overlapping (disjoint) subsets.

   _2 c/\ 'abcdef'
(ab)
(cd)
(ef)

Further reduction on this result combines the pairs, grouping from bottom to top:

   c/_2 c/\ 'abcdef'
((ab)((cd)(ef)))

References

1. C. Burke, C. Reiter. A Brief J Reference. 2002, 2004. 1. C. Burke. Operators - Reduce, Scan, Outer Product. In APL and J, Vector, Vol 13 No 3, January 1997. 1. C. Burke, R. Hui, J for the APL Programmer. APL Quote-Quad, Volume 27, Number 1, September 1996.


Contributed by Oleg Kobchenko with additions by Devon McCormick.