KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Binary iteration question Re: Binary iteration question

  • Laura

    Administrator
    May 18, 2022 at 12:00 am

    Rians excellent logic can be refactored to use the Over iterator. (You might not be familiar with Over with a ternary or quaternary function.)

     

    q)g:{"b"$0^fills@/[;(x,y+1){where x=x msum y}'1 notz;:;1 0]count[z]#0N} 
    q)g[3;2] 
    00001111000111001101000b 00000011110001111111110b 
    q)g[3;2] 
    110010111001111b 000000001111111b

     

    The key concept here is that the first argument of Amend At Over @/ is the initial state: count[z]#0N. The other (right) arguments are same-length lists, or atoms.  Over works through the argument lists in succession.

    Once again we see the Zen monks as a point-free alternative to writing (z;not z).

    The refactoring here doesnt save much time, but spotting opportunities like this improves your ability to find iterator solutions, some of which will save you significant CPU.