kdb+

While condition for iterator to be passed as argument of a function


I have below function for fibonacci which is returning expected output

q)fib:{{x,sum -2#x}/[{last[x] < 100};x]}    
q)fib 1 1
1 1 2 3 5 8 13 21 34 55 89 144

How could we replace the value 100 in the inner function {last[x] < 100} with an argument from the outer function?

Expected function call structure -

q)fib[1 1;100]
1 1 2 3 5 8 13 21 34 55 89 144 /- expected output

Solution

  • Here's one way

    q)fib:{{x,sum -2#x}/[{last[y]<x}y;x]}
    q)fib[1 1;100]
    1 1 2 3 5 8 13 21 34 55 89 144