Here's some functional idioms that should make you feel at home:

select =: 1 : 'u # ]'        NB. select y items where u y is true
selectI =: 1 : 'I.@:u y'   NB. select indexes where u y is true

    (>&5) select i.9
6 7 8

UpdateWhere=: 2 : 'v^:(0 ~: '''' ($,) :: 1: u)' NB. if. u y do. v y else. y 
end. ("_ def)
update =: 1 : 0            NB. for indexes x, update y to u y
:
(u L:0 (x{y)) x } y
)
deleteI =: 13 : 'y #~ (=&(#x)) x i. i.#y'    NB. deletes indexes x from y


   (>&5) UpdateWhere +:"0 i.9
0 1 2 3 4 5 12 14 16
   6 7 8 +: update i.9
0 1 2 3 4 5 12 14 16

scanl =: \.(&.|.)        NB. Adv.  scan left (\ is scanr)

   < \ p: i.6
+-+---+-----+-------+----------+-------------+
|2|2 3|2 3 5|2 3 5 7|2 3 5 7 11|2 3 5 7 11 13|
+-+---+-----+-------+----------+-------------+
   < scanl p: i.6
+-+---+-----+-------+----------+-------------+
|2|3 2|5 3 2|7 5 3 2|11 7 5 3 2|13 11 7 5 3 2|
+-+---+-----+-------+----------+-------------+
   -/ scanl p: i.6
2 1 4 3 8 5


----- Original Message ----
From: "Miller, Raul D" <[EMAIL PROTECTED]>
To: Programming forum <[email protected]>
Sent: Sunday, November 19, 2006 10:17:41 AM
Subject: RE: [Jprogramming] Thanks re: explicit to tacit converter comprehension

Arved Sandstrom wrote:

> I like the simplicity of Haskell's

> filter pred list

> or

> [ f x | x <- list, pred x ]

 

I've never gotten very far with Haskell, myself, so I don't know what

that means.  (The answer probably doesn't go in this forum, but if

you could describe, in maybe 10 lines or less, what to download and

install, and what to type in, to see the above in action, that would be a

great post for jchat.)

 

That said, I'm going to guess that in filter pred list, pred is either

a selection vector or a selection function, and filter pred list

somehow produces  a new x where the pred selected items

in x have been replaced by the result of applying a function

inherent in filter to those items.  (And the non-selected items

remain unchanged and retain their relative positions in the result.) 

 

In J,

filter=:adverb define

:

 (u x#y) (I.x)} y

)

 

   i. 7

0 1 2 3 4 5 6

   (1 0 1 0 1 0 1) (10*])filter i. 7

0 1 20 3 40 5 60

 

Or, if you know that y is numeric, you might instead use

filtern=:adverb define

:

 (y*-.x)+u y*x

)

 

In the above example, filtern is a drop-in replacement

for filter.

 

Note that, in both cases, u must be a function which

produces a result the same size as its argument.  Typically, this

means that each element in u y corresponds to a single element

in y.

 

Also note that in J, a predicate function probably best corresponds

to a function which generates a boolean list (indicating what's

selected and what's not).  If you actually eliminate items from

your argument, so your result is a different size from the argument,

you wind up having to use something like e. to get the boolean list.

 

As for [ f x | x <- list, pred x ], does this correspond to what

you were thinking about?

   (7 8 9, 1 0 1 0 1 0 1#]) i. 7

7 8 9 0 2 4 6

 

Finally, a reminder: J is interpreted and has per-primitive

overhead where in analogous contexts Haskell's compiler

might eliminate overhead.  However, in most contexts J's

nouns can be thought of as having almost no cost (just the

cost of the memory it takes to store them -- they're already

computed).  So algorithms expressed in terms of large

regular nouns tend to work much better in J than algorithms

expressed in terms of minute or irregular nouns.



Thanks,

 

-- 

Raul

 


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



__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to