There's no clean way to do it. If the result of the first invocation of
f has the same type as the rest of the list elements, you can do

f/ _2 (}. , f0/@:{.) l

and if it has a different type you can check whether the right argument
has this type (like your "signature structure", but implicit).

One of my most missed features in J is a reduce with a specified initial
element. Thus (l f/ e) would be equivalent to (> f&.>/ (<"_1 l),<e) but
without the very large inefficiency of boxing everything. Of course
dyadic f/ is already taken, so it would need another symbol. With this
feature, you could use (_2 (}. f/ f0/@:{.) l) regardless of the
properties of f.

If your initialization comes before any of the processing and doesn't
change the arguments (so that the first function invocation f0 is
equivalent to (f[i0) for some initializer i0), then you can use
(f/ l [ i0/ _2{.l), that is, separate the initialization from the
function application completely.

Marshall

On Fri, Mar 02, 2018 at 01:51:39PM +0530, Arnab Chakraborty wrote:
> Hi,
> 
>   If f is a dyad, then
> 
> f/ 2 4 5
> 
> means
> 
> 2 f 4 f 5
> 
> where the rightmost occurence of f is invoked first. I am wondering if
> there is a smart way to recognize this first invocation separately from the
> rest. This will enable me to perform some initialization inside f. By
> "smart" I mean something smarter than the followung two:
> 
> 1) using side effect like setting a global flag.
> 2) adding some signature structure to the output of f (eg, 'inited';
> result),
>     and then checking the absence of this signature.
> 
> Thanks.
> ----------------------------------------------------------------------
> 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