How can I merge two lists using a custom comparator ?

This means I must iterate until both lists are exausted,

taking the current element which compares lower off its

list at each iteration.

In pseudo code

<pre>

  A= (1,3, 5, 6, 10, 13, 18, 26, 42, 48)

  B= (1,3, 4, 7, 10, 12, 19, 24, 42, 44)

  a=firstItem(A); b=firstItem(B); next=null; 

  while (a!=null and b!=null) {

    n= compare(a,b) ;

    if (n<=0) { output(a) ; a=nextItem(A) ;}

    if (n>=0) { output(b) ; b=nextItem(B) ;}

  }

  while (a!=null) { output(a); a=nextItem(A) ;}

  while (b!=null) { output(b); b=nextItem(B) ;}

</pre>  

How can I do this properly in kdb (ie without while loops) ?

Here “compare” and “output” will be custom operations, and the real data will tables not lists of numbers,

But the sticking point seems to be just the need to advance through one list or the other conditionally, as illustrated here.

?[a>b;a;b]

Pe 5 mai 2017 10:03, “dan” <treelite29@gmail.com> a scris:

This means I must iterate until both lists are exausted,

taking the current element which compares lower off its

list at each iteration.

In pseudo code

<pre>

  A= (1,3, 5, 6, 10, 13, 18, 26, 42, 48)

  B= (1,3, 4, 7, 10, 12, 19, 24, 42, 44)

  a=firstItem(A); b=firstItem(B); next=null; 

  while (a!=null and b!=null) {

    n= compare(a,b) ;

    if (n<=0) { output(a) ; a=nextItem(A) ;}

    if (n>=0) { output(b) ; b=nextItem(B) ;}

  }

  while (a!=null) { output(a); a=nextItem(A) ;}

  while (b!=null) { output(b); b=nextItem(B) ;}

</pre>  

How can I do this properly in kdb (ie without while loops) ?

Here “compare” and “output” will be custom operations, and the real data will tables not lists of numbers,

But the sticking point seems to be just the need to advance through one list or the other conditionally, as illustrated here.


Submitted via Google Groups