KX Community

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

Home Forums kdb+ Question about query that stuck the KDB process Re: Question about query that stuck the KDB process

  • rocuinneagain

    Member
    April 29, 2022 at 12:00 am

    https://code.kx.com/q4m3/6_Functions/#677-more-over-iteration

    The final overload of / is equivalent to a while loop in imperative programming. It provides a declarative way to specify a test to end the iteration. Thinking functionally, we provide a predicate function that is applied to the result at each step. The iteration continues as long as the predicate result is 1b and stops otherwise. For example, we stop the computation of the Fibonacci sequence once it exceeds 1000 as follows.

    q)fib:{x,sum -2#x} q)fib/[{1000>last x}; 1 1] 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

     

    Your code accidentally boiled down to an infinite loop as the result never moves to 0b

    1 1/[sums;1]

     

    /Each iteration it runs 1 1[1] / Which returns 1 /Then sums 1 /This returns 1 /This is a non zero value which gets treated as 1b so the loop starts again /Infinite loop