KX Community

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

Home Forums kdb+ Meet the Zen monks

  • Meet the Zen monks

    Posted by SJT on January 12, 2022 at 12:00 am

    In Jeff Borrors classic textbook Q for Mortals you will find frequent references to moments of Zen meditation leading to flashes of insight into the workings of q.

    My teacher Myokyo-ni liked to quote Dogen-zenji:

    The Great Way is not difficult. It avoids only picking and choosing.

    The Do form of the Scan iterator has a pattern I think of as the Zen monks.

     

    How many Zen monks does it take to change a lightbulb?
    Two. One to change it; one not to change it.

    The basic pattern is to apply a function and not to apply it. Consider the trim keyword. It must find the spaces in a string, then the continuous spaces from each end. If we had to write trim in q it might be

     

    q){b:x<>" ";(b?1b)_ neg[reverse[b]?1b] _ x}" Trim the spaces. " 
    "Trim the spaces."

     

    We notice the repetitions:

    • both b and reverse[b] are searched for 1b
    • two uses of the Drop operator

    We want to do the search/drop thing from both ends of the string.

     

    q){x{y _ x}/1 -1*(1 reverse" "<>x)?'1b}" Trim the spaces. " 
    "Trim the spaces."

     

    Notice the {y _ x} reduction above. Lambda {y f x} commutes a function f by switching its arguments. The pattern R{y f x}/L  successively applies a list of left arguments L to an argument R.

    Here we use 1 reverse to get the boolean vector and its reversal. I think of this 1 f pattern as the Zen monks.

    Here is another use for it, in finding the shape (rows and columns) of a matrix.

     

    q)show m:{max[count each x]$'x}string`avoids`picking`and`choosing "avoids " "picking " "and " "choosing" 
    q)shp:{count each 1 firstx} / shape of a matrix 
    q)shp m 4 8

     

    The Zen Buddhist pension plan: A day without work is a day without food. Can you see any other work for the monks?

    lkerr replied 8 months, 1 week ago 4 Members · 3 Replies
  • 3 Replies
  • fbodon

    Member
    April 3, 2022 at 12:00 am

    This 1 f pattern is crazy, but i love it

  • leahs

    Member
    March 19, 2024 at 11:01 am

    Such a great post – so interesting!

    Thanks for sharing @SJT 

  • lkerr

    Member
    March 19, 2024 at 1:12 pm

    Thanks for sharing @SJT !

Log in to reply.