Home › Forums › kdb+ › Binary iteration question › Re: Binary iteration question
-
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.