I have created a dyadic verb which expects a number and a vector and returns the vector filtered to contain those which divide the number, like this:

divs =. 4 : '((=<.)y%x)#y'

So, for example, 4 divs i.20 returns 0 4 8 12 16 as expected.

Now I'd like to modify/wrap this verb so that the first argument can be a vector as well, and return either a 2-dimensional vector or a single long one. I'm interested in how to implement both. So I'd like to be able to do this:

4 5 divs2 i.20

and have my verb return:

0 4 8  16 20
0 5 10 15

or:

0 4 8 16 20 0 5 10 15

Something like map or mapcat or flatmap from FP languages. How can I achieve this?

EDIT: to be clear, I'm hoping for 2 new verbs (not a single one which can produce both results)

有帮助吗?

解决方案

I'll come back later and edit this with rationale, but just as an immediate answer, you want:

   divs2=.divs"0 1  NB. Parcel left args into scalars, right into vectors

   4 5 divs2 i.20  NB. Note fill element (trailing zero)
0 4  8 12 16
0 5 10 15  0

   divs3=.[: ; <@divs2  NB.  Like divs2, but single flat list

   4 5 divs3 i.20   NB. Note the <@ prevents the fills (no trailing zero)
0 4 8 12 16 0 5 10 15

其他提示

J does have an Residue primitive (|) that makes this a little simpler (it saves me from having to divide and then compare the result is an integer for identifying factors).

In Tacit form

   divt=: ((0=|)#])"0 1
   4 5 divt i. 20
0 4  8 12 16
0 5 10 15  0

In Explicit form

   divE=: 4 : '((0 = x|y) # y)'"0 1
   4 5  divE i. 20
0 4  8 12 16
0 5 10 15  0

I often find that J already has a primitive that does at least part of the work for me when I code.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top