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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With