Thank you, Dan! I do browse through the vocabulary but apparently don't take in the functionality until I need it! Key (/.) does the job nicely and I was unaware of it.
Yes, I know Dyalog APL quite well, but haven't used it for two years since I moved to J. Shouldn't ardent APLers be populating the Rosetta Code with APL? I feel like a pupa being asked to climb out of its chrysalis to have a quick munch on a cabbage leaf. I'd have a distinct problem with motivation. Regards Graham Dan Bron wrote: > > Dyalog APL allows f <- where f is a dyadic function, as in > > How do I do this in J? > > You need to roll up all repeated elements, then do the amendment. In > this case, you need to sum all the contributions at the same index, > then use the nub of the indices to amend the sums back into the array. > > a =: 10$0 > > destinations =: 2 2 5 6 2 > contributions =: 3 4 5 6 7 > > a =: (destinations +//. contributions) > (~.destinations)} a > > a > 0 0 14 0 0 5 6 0 0 0 > > The adverb /. is a general tool to roll up repeats. Its verb > argument will depend on the function f and will normally be f/ . > In this case, f was + and "roll up" meant sum, so the argument to > /. was +/ resulting in +//. . > > Amend isn't used as much in J as it is in APL (in my experience). > > -Dan > > PS: Dyalog only permits this for certain f . J generalizes this to > any function, using the method described above. > PPS: If you know APL, I could use your help fleshing out the APL > examples at Rosetta Code. To review the examples I've already > submitted, check out http://rosettacode.org/wiki/Category:APL . To > see tasks that would benefit from APL solutions, see > http://rosettacode.org/wiki/Category:Programming_Tasks . ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
