As I understand it, what you are trying to define is an adverb 
(aka higher order function) where one of the arguments is your 
boolean verb and the other argument is the noun which is the 
argument to the filter.  In J the analysis is divided into two parts:  
an adverb which takes the verb as its argument to derive a new verb, 
and that derived verb is then applied to the array argument to 
produce whatever.

Confusion arises from the failure to acknowledge that the 
boolean verb is to be an argument, to be changed from one 
expression to the next.

filter =: 1 : 'x # ]'  NB. adverb
filter1=: 1 : '#~ x'   NB. alternative adverb defn
filter2=: adverb define  NB. another alternative
x # ]
)

   odd=: 2&|
   x=: i.10
   odd x
0 1 0 1 0 1 0 1 0 1

So "odd" is your boolean verb.

   odd filter x
1 3 5 7 9

J grammar is such that  odd filter x  is parsed as (odd filter) x, and :

   odd filter
odd # ]

That is,   odd filter   derives the new (anonymous) verb  odd # ] .

   isprime=: 1&p:
   isprime filter x
2 3 5 7

   perfectsquare=: = <.&.%:
   perfectsquare filter x
0 1 4 9

   (0 = 2&|) filter x
0 2 4 6 8

The last example illustrates the fact that the argument to
the adverb need not be named.

Now suppose you want to change your filter slightly, so
that it always applies to i.n, with n to be supplied as an
argument.  Then:

   filtera=: 1 : '(x # ])@i.'
   odd filtera
(odd # ])@i.

   odd filtera 10
1 3 5 7 9
   odd filtera 20
1 3 5 7 9 11 13 15 17 19

   perfectsquare filtera 100
0 1 4 9 16 25 36 49 64 81

   isprime filtera 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97



----- Original Message -----
From: Alex Gian <[email protected]>
Date: Tuesday, August 24, 2010 9:19
Subject: Re: [Jprogramming] Implementing a tacit "filter" function
To: Programming forum <[email protected]>

> I was "chatting" to someone off-list and they expressed the 
> opinion that
> perhaps my insistence on implementing "filter" the way I 
> described (with
> a boolean verb as one of the args) was influenced by the way Higher
> Order Procedures are implemented in most other common functional
> languages - which rely heavily on recursion - and that this may have
> skewed my view.
> 
> (Reading between the lines) it seems that the experts' view here is
> "don't do it that way" and that, in fact writing
>   "(#~ odd) x"
> is no big deal, rather than insisting on the "traditional"
>   "odd filter x"
> on which I was so intent.
> 
> In fact, it would seem that the natural J way would be not to try
> shoehorn "filter" into a dyadic verb that way at all, which would
> account for some of the more experienced members' bemusement at my
> insistence.
> 
> Would that be a fair comment?
> All comments welcome, hey, I _am_ trying to change gear here...
> 
> 
> 
> On Tue, 2010-08-24 at 08:40 -0400, Raul Miller wrote:
> > On Tue, Aug 24, 2010 at 8:19 AM, Alex Gian 
> <[email protected]> wrote:
> > > The way you did it with the fork was instructive to me, 
> R.E., really
> > > nice to see all the different approaches!
> > >
> > > It's still not a "filter" verb on its own, though, as 
> specified (left
> > > arg - boolean verb, right arg - list to filter)
> > 
> > If you want the left arg to be a boolean verb, you have two
> > possibilities:
> > 
> > 1) use an adverb
> > 2) require that the left arg be the gerund of the boolean verb.
> > 
> > For an english example: if the boolean verb were "choose"
> > the gerund would be "choosing" and we could form sentences
> > where we talk of choosing, or we could have words which modify
> > "choose".  J's grammar follow similar constraints.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to