admin管理员组

文章数量:1356275

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

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
Share Improve this question asked Mar 30 at 16:13 UtsavUtsav 5,9622 gold badges35 silver badges56 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

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

You can also drop the inner lambdas and use compositions:

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

本文标签: kdbWhile condition for iterator to be passed as argument of a functionStack Overflow