0
getQuotes: {[sym;start;end]
  url: f[sym;start;end]
  {some_test} {request.quotes[url;x`next_page_token]} \ request.quotes[url;0b] 
  }

I have a while loop. The issue I'm having is that url is out of scope for the while lambda:

'url
  [2]  alpaca.q:47: .alpaca.getQuotes@:{request.quotes[url;x`next_page_token]}
                                                                             
                                                       ^

I have read on this post that this is by design: Variable scope propagation in k, but it doesn't really give me a solution.

I've tried to wrap it in a unary, which didn't work.

I made url global with url::, this did work, but it's not very clean.

Is there a preferred pattern for passing url into the lambda?

cjm2671
  • 18,348
  • 31
  • 102
  • 161

1 Answers1

1

You can pass variables into the inner lambdas as parameters:

getQuotes: {[sym;start;end]
  url: f[sym;start;end]
  {some_test} {[x;url]request.quotes[url;x`next_page_token]}[;url] \ request.quotes[url;0b] 
  }
terrylynch
  • 11,844
  • 13
  • 21
  • The `x` is the response from the first iteration; the result gets fed back into the loop until some_test = 0. I actually didn't this would work, but I'll give it a shot now. – cjm2671 Jan 12 '22 at 19:54
  • Yep, as predicted, x is now out of scope. I tried variants of this (passing `x` through), but no luck. I'm not sure this is possible without a global. – cjm2671 Jan 12 '22 at 19:57
  • Oh I see now, I think you want a projection for the scan to work. I've edited my answer above for clarity. ```{some_test} {[x;url]request.quotes[url;x`next_page_token]}[;url] \ request.quotes[url;0b]``` – terrylynch Jan 12 '22 at 21:40
  • 1
    That's the one! Thank you! :) – cjm2671 Jan 14 '22 at 10:12