最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

kdb+ - While condition for iterator to be passed as argument of a function - Stack Overflow

programmeradmin4浏览0评论

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
发布评论

评论列表(0)

  1. 暂无评论