KX Community

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

Home Forums KX Academy terminating condition for iterators

  • terminating condition for iterators

    Posted by lestat-jin on July 19, 2024 at 3:21 pm

    not sure why the below does not work as it keeps causing crash in Jupyter Notebook?

    {8>count x}{x,sum -2#x}\[0 1]

    I understand the condition to terminate iteration can be placed inside function input, e.g. f[{condition};input] or {condition}f[input], for example this works as given in the notebook : {1000 > max x}{(+)prior x,0}\ 1

    in general what is recommended ways to write conditions for terminating the “loop”/iteration ?

    Thanks,


    lestat-jin replied 2 months, 3 weeks ago 2 Members · 2 Replies
  • 2 Replies
  • megan_mcp

    Administrator
    July 22, 2024 at 3:06 pm

    Hi @lestat-jin

    The correct way to execute your answer would be to remove the square brackets:

    {8>count x}{x,sum -2#x}\0 1

    This is because we are looking for the dyadic form of the scan adverb ‘x f\y’. Which if I use a parse tree to explain, would look like this:

    q)-5!"{8>count x}{x,sum -2#x}\\0 1" / the correct version
    (\;{x,sum -2#x})       
    {8>count x}            
    0 1                    

    However if you use square brackets after scan, it forces it into monadic scan form, ‘x[f\y]’:

    q)-5!"{8>count x}{x,sum -2#x}\\[0 1]"
    {8>count x}
    ((\;{x,sum -2#x});0 1)

    If you have any questions on this, please let me know!

    Thanks,

    Megan

    • lestat-jin

      Member
      July 23, 2024 at 3:50 pm

      Thanks a lot. could you explain why dyadic function is expected here? what is the f in “x f\ y” here? the {8>count x}{x,sum -2#x}\0 1 only takes one argument which is x…

Log in to reply.