Apply function to every combination of list elements

Hi,

I am searching an easy way to to apply a function to all combinations of two list entries (not self, not repeating, ignore order).

My best effort as of now is this helper function:

k)e:{i:0;r:();l:-1+#y;while[i<l;r,:x[y[i-1];]'(i+:1)_y];r}

for example



q)e[{0N!(`log;x;y); x+y}] 1 2 3 4(`log;1;2)(`log;1;3)(`log;1;4)(`log;2;3)(`log;2;4)(`log;3;4)3 4 5 5 6 7




I know that for trying every combination (including self and with every possible order) I can use


q)l {0N!(`log;x;y); x+y}/:\: l:1 2 3 4(`log;1;1)(`log;1;2)(`log;1;3)(`log;1;4)(`log;2;1)(`log;2;2)(`log;2;3)(`log;2;4)(`log;3;1)(`log;3;2)(`log;3;3)(`log;3;4)(`log;4;1)(`log;4;2)(`log;4;3)(`log;4;4)2 3 4 53 4 5 64 5 6 75 6 7 8



so is there a simpler way than my e function?

thank you, Markus

q)raze x,/:'til each x:til 5

1 0

2 0

2 1

3 0

3 1

3 2

4 0

4 1

4 2

4 3

  Attila

ok thanks, my helper now looks like this:

k)e:{x.‘y@,/a,/:’ !:'a:!#y}

On Monday, December 1, 2014 10:03:51 AM UTC+1, Markus Sieber wrote:

Hi,

I am searching an easy way to to apply a function to all combinations of two list entries (not self, not repeating, ignore order).

My best effort as of now is this helper function:

k)e:{i:0;r:();l:-1+#y;while[i<l;r,:x[y[i-1];]'(i+:1)_y];r}

for example



q)e[{0N!(`log;x;y); x+y}] 1 2 3 4(`log;1;2)(`log;1;3)(`log;1;4)(`log;2;3)(`log;2;4)(`log;3;4)3 4 5 5 6 7




I know that for trying every combination (including self and with every possible order) I can use


q)l {0N!(`log;x;y); x+y}/:\: l:1 2 3 4(`log;1;1)(`log;1;2)(`log;1;3)(`log;1;4)(`log;2;1)(`log;2;2)(`log;2;3)(`log;2;4)(`log;3;1)(`log;3;2)(`log;3;3)(`log;3;4)(`log;4;1)(`log;4;2)(`log;4;3)(`log;4;4)2 3 4 53 4 5 64 5 6 75 6 7 8



so is there a simpler way than my e function?

thank you, Markus

Here is a way to keep it really simple.  This isn’t short, but is easy to follow.

e:{[f;l]c:l cross l;c:flip c where not each[(=)over]c;c[0]f’c[1]}

ah just wanted to note that my solution does have pairs in forward and reverse order

here is a quick fix:

e:{[f;l]c:distinct asc each l cross l;c:flip c where not each[(=)over]c;c[0]f’c[1]}