Your diagram isn't right.  Rather than a column y{UV, you need a column y, the first entry in which would be 3.

The first application of foo is

3 foo 0 0 0

which gives a result of shape 3 3 .

The next application of foo is

2 foo (3x3 array)

giving shape 3 3 3, etc.


The y in foo is NOT the items of the y input to Fold, but rather the initialization/feedback value.
The x in foo is the items of the y input to Fold.


Henry Rich

On 9/7/2021 12:04 PM, R.E. Boss wrote:
In my example

UV=:0,(,-@|.)=i.3
foo=: {{ x+ y{UV }}

and with the fold-diagram as guide, I thought the working of 0 0 0 ]F:. foo  3 
2 _3  would be

  y{UV   x= 0 0 0
   v          v
0 0 1 ---> 0 0 1
   v          v
0 1 0 ---> 0 1 1
   v          v
0 0 -1 --> 0 1 0

but it is not. The output produced is completely incomprehensible to me, with a 
shape of 3 3 3 3 3.

With some reverse engineering I found out that swapping the y and x gives what 
I want, so
    0 0 0 ]F:. (foo~) 3 2 _3
0 0 1
0 1 1
0 1 0

Memo to self: swap arguments (of  inner verb) before fold.


R.E. Boss


-----Original Message-----
From: Programming <[email protected]> On Behalf Of Henry 
Rich
Sent: dinsdag 7 september 2021 16:44
To: [email protected]
Subject: Re: [Jprogramming] Fold experience

Fold principles:

1. The items of y [and x] are applied to the x argument of v. This is 
analogousto u/ y and u/\. y.  The underlying J principle is that when a verb 
operates multiple times, it operates repeatedly on its y argument. Thus the 
result of each application of v is the y argument to the next v.

2. The same verb could be used for Fold Forward or Fold Reverse, with the only 
difference being the order in which items are processed.

3. The optional x argument should be thought of as an additional item of y, or 
if you like an initial value for the iteration.  If given, it is the first y 
argument to v.

Henry Rich

On 9/7/2021 7:15 AM, R.E. Boss wrote:
OK, I thought that x was always on the left of a verb, and y on the right. Did 
not expect the diagram would have priority over that J axiom.
Never too old to learn, although it will be quite confusing. Now I have normal, 
old-fashioned verbs, and verbs which will be folded.
Apart from that, why does swapping the parameters (with ~) not work?

Thanks.


R.E. Boss


-----Original Message-----
From: Programming <[email protected]> On Behalf
Of Raul Miller
Sent: dinsdag 7 september 2021 11:28
To: Programming forum <[email protected]>
Subject: Re: [Jprogramming] Fold experience

The diagram shows the y argument to the fold on the left and the x argument to 
the fold on the right. So that cannot be the issue you are talking about.

The diagram shows y0 being used first, y1 second and so on, which matches what 
we saw happening in action. So that cannot be the issue you are talking about.

But that leaves me confused (an all-too-common event) -- what is the behavior 
which you are talking about which does not conform to that diagram?

Thanks,

--
Raul


On Tue, Sep 7, 2021 at 5:19 AM R.E. Boss <[email protected]> wrote:
The main point I was trying to make was that F:. showed behaviour not conform 
the diagram on https://code.jsoftware.com/wiki/Vocabulary/fcap.
The role of x and y seem to be interchanged.
However, then u F:.v~  should work, which does not.

The advantage of fold is just that x can differ essentially from an item of y, 
contrary to /\. , where all items are equal, and you are forced to use boxes.


R.E.Boss


-----Original Message-----
From: Programming <[email protected]> On
Behalf Of Raul Miller
Sent: dinsdag 7 september 2021 10:16
To: Programming forum <[email protected]>
Subject: Re: [Jprogramming] Fold experience

Swapping the argument does not appear to influence the shape of the result.

The results themselves are different,

UV=:0,(,-@|.)=i.3
foo=: {{ x+ y{UV }}
oof=: {{ y+ x{UV }}

     0 0 0 (]F:.foo -: ]F:.foo~) 3 2 _3
0
     0 0 0 (]F:.oof -: ]F:.oof~) 3 2 _3
0

That said, focusing on the variant which you described as providing the desired 
behavior:

     0 0 0 ]F:.oof  3 2 _3
0 0 1
0 1 1
0 1 0

This corresponds to:
     3 oof 0 0 0
0 0 1
     2 oof 3 oof 0 0 0
0 1 1
     _3 oof 2 oof 3 oof 0 0 0
0 1 0

Which, in turn, somewhat matches the value selection mechanism of J's scan:
     <\ 3 2 _3
+-+---+------+
|3|3 2|3 2 _3|
+-+---+------+

The difference between fold and scan being that fold reuses the result of the 
previous evaluation while scan does not.

I hope this makes sense,

--
Raul

On Tue, Sep 7, 2021 at 2:09 AM R.E. Boss <[email protected]> wrote:
[UV=:0,(,-@|.)=i.3

0 0 0

1 0 0

0 1 0

0 0 1

0 0 _1

0 _1 0

_1 0 0


foo=: {{ x+ y{UV }}            NB. constructed conform the diagram on 
https://code.jsoftware.com/wiki/Vocabulary/fcap



$0 0 0 (]F:. foo) 3 2 _3

3 3 3 3 3

$0 0 0 (]F:. foo)~ 3 2 _3

3 3 3 3 3


foo=: {{ y+ x{UV }}            NB. y and x are swapped, counterintuitive



$0 0 0 (]F:. foo) 3 2 _3

3 3

0 0 0 (]F:. foo) 3 2 _3       NB. this is the desired behaviour

0 0 1

0 1 1

0 1 0

$0 0 0 (]F:. foo)~ 3 2 _3

3 3

Also remarkable is that swapping the arguments seems not to influence the 
result.
Any clarification?


R.E. Boss

--------------------------------------------------------------------
-- For information about J forums see
http://www.jsoftware.com/forums.htm
---------------------------------------------------------------------
- For information about J forums see
http://www.jsoftware.com/forums.htm
---------------------------------------------------------------------
- For information about J forums see
http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

--
This email has been checked for viruses by AVG.
https://www.avg.com

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to