>To pull out the even numbers. So filter takes a predicate and a 
>collection basically. I was thinking
 
You can define filter as an adverb explicitly,
 
   filter=. '#~ x &>' (1 :)
   
   (2&|) filter i.11
1 3 5 7 9
or tacitly,
   
   filter=. &> (#~`) (`:6)
   
   (0=2&|) filter i.11
0 2 4 6 8 10

This version also deals with boxed lists:
   
   palindrome=. -: |.
   
   ]WORDS=. ;:'Yesterday dad did buy mom a madam reviver 4-rotor engine racecar 
with a radar detector'
┌─────────┬───┬───┬───┬───┬─┬─────┬───────┬─┬─┬─────┬──────┬───────┬────┬─┬─────┬────────┐
│Yesterday│dad│did│buy│mom│a│madam│reviver│4│-│rotor│engine│racecar│with│a│radar│detector│
└─────────┴───┴───┴───┴───┴─┴─────┴───────┴─┴─┴─────┴──────┴───────┴────┴─┴─────┴────────┘
   
   palindrome filter WORDS
┌───┬───┬───┬─┬─────┬───────┬─┬─┬─────┬───────┬─┬─────┐
│dad│did│mom│a│madam│reviver│4│-│rotor│racecar│a│radar│
└───┴───┴───┴─┴─────┴───────┴─┴─┴─────┴───────┴─┴─────┘
   
   palindrome filter (;:'blessed are they that belive that they are 
blessed');'palindrome';'rats live on no evil star'
┌────────────────────────────────────────────────────┬─────────────────────────┐
│┌───────┬───┬────┬────┬──────┬────┬────┬───┬───────┐│rats live on no evil star│
││blessed│are│they│that│belive│that│they│are│blessed││                         │
│└───────┴───┴────┴────┴──────┴────┴────┴───┴───────┘│                         │
└────────────────────────────────────────────────────┴─────────────────────────┘
  
   palindrome filter
#~ palindrome&>

 



________________________________
From: Lau B. Jensen <[email protected]>
To: Programming forum <[email protected]>
Sent: Wed, December 2, 2009 6:28:08 AM
Subject: Re: [Jprogramming] newbie question



W.Ch Lin wrote:
> Does J have filter function like clojure's ?
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm

I definitely want to hear what the J crowd says, but just to be sure, in 
Clojure you could say

(filter #(zero? (rem % 2)) (range 1 11))
> 2 4 6 8 10

To pull out the even numbers. So filter takes a predicate and a 
collection basically. I was thinking
in J I'd to something like

,.((|~ & 2) 1 + i. 10) ;, 1 + i.10
+--------------------+
|1 0 1 0 1 0 1 0 1 0 |
+--------------------+
|1 2 3 4 5 6 7 8 9 10|
+--------------------+

Where as you can see all even values get a 0 return from (|~ & 2), but 
that leaves 2 questions

1) Is that how you would do it with J at all?
2) Disregarding 1, how would you pick those 0 values out from the 
original list 1 + i.10 ?

Thanks
Lau




----------------------------------------------------------------------
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