/ finds the non-duplicated union of two lists union:{[x;y]; ?x,y} /finds intersection of two lists with duplicate removal intersect:{[x;y]; z: x ?/: y; ii: & z < #x; ?x[z[ii]]} / finds intersection of two lists with duplicate removal / Much faster. / intersect: {[x;y] i: x ?/: y; j: & i < #x; :?y[j] } /finds set difference of two lists x - y / differ:{[x;y]; ?x[&~ x _in\: y]} / A faster difference differ:{[x;y] i: y ?/: x; j: & i = #y; ?x[j] } / Single attribute equi-join where T1.att1 is a key / index1 are the rows of T1 that are still available coming into the join. / Similarly for index2. / Give indexes of T1 and T2 / cardinality is as big as T2.att2[index2] / the jth element says where in T1.att1[index1] / there is a match for the jth element of T2.att2[index2]. / if i[j] = size1 then there is no match. / T1.a: `c`d`e`f`a`b / T2.b: `d`b`h`e`c`b`e`f`e / index1: !#T1.a / index2: !#T2.b / att1: `a / att2: `b / ((T1[att1][index1[i[goods]]]) ; T2[att2][index2[goods]]) fkjoin:{[T1; att1; index1; T2; att2; index2] i: T1[att1][index1] ?/: T2[att2][index2]; goods: & i < #index1; ((index1[i[goods]]) ; index2[goods]) } findindex:{[myvals; othervals; i] x: & myvals[i] = othervals; ((#x) # i) ,' x} / This means: find index pairs in tab1 and tab2 / such that tab1.attsin1 matches via equality tab2.attsin2. / Here we assume the join is on one attribute. / Result is a pair of lists whose ith members match. eqjoinone:{[T1; attsin1; index1; T2; attsin2; index2] att1: *attsin1; att2: *attsin2; size: # ? T1[att1][index1]; if[size = #index1; out:fkjoin[T1;att1;index1; T2;att2;index2]];size: # ? T2[att2][index2]; if[size = #index2; out:| fkjoin[T2;att2;index2; T1;att1;index1] ]; myind: !#T1[att1][index1]; x: findindex[T1[att1][index1]; T2[att2][index2]]'myind; y: ,/ x; (y[;0]; y[;1])} eqjoinone:{[T1; attsin1; index1; T2; attsin2; index2] flag: 0; att1: *attsin1; att2: *attsin2; size: # ? T1[att1][index1]; if[size = #index1; flag: 1; out:fkjoin[T1;att1;index1; T2;att2;index2]];size: # ? T2[att2][index2]; if[size = #index2; flag: 1; out:| fkjoin[T2;att2;index2; T1;att1;index1] ]; if[flag = 0; myind: !#T1[att1][index1]; x: findindex[T1[att1][index1]; T2[att2][index2]]'myind; y: ,/ x; out: (y[;0]; y[;1])]; out} T1.a: `c`d`e`f`a`b`c T2.b: `d`b`h`e`c`b`e`f`e index1: !#T1.a index2: !#T2.b att1: `a att2: `b attsin1: ,att1 attsin2: ,att2 eqjoinone[T1; attsin1;index1; T2; attsin2; index2]