Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a verb that expects a scalar with a vector

Tags:

j

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)

like image 260
Matthew Gilliard Avatar asked Mar 23 '23 07:03

Matthew Gilliard


1 Answers

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
like image 144
Dan Bron Avatar answered May 16 '23 08:05

Dan Bron