cancel
Showing results for 
Search instead for 
Did you mean: 

How can I merge two lists using a custom comparator ?

dan
New Contributor
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.

1 REPLY 1

felix1
New Contributor
?[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.

--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to personal-kdbplus+unsubscribe@googlegroups.com.
To post to this group, send email to personal-kdbplus@googlegroups.com.
Visit this group at https://groups.google.com/group/personal-kdbplus.
For more options, visit https://groups.google.com/d/optout.