KX Community

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

Home Forums kdb+ Something for the weekend?

  • Something for the weekend?

    Posted by SJT on February 24, 2023 at 12:00 am

    A challenge for the weekend? Something from the Vector D?j?

    A single-expression function to replace multiple embedded spaces in a string with single spaces. E.g.

     

    q)rmeb “the quick brown fox” “the quick brown fox”

     

    If you find yourself writing a loop, you’re on the wrong track. This is not a golfing challenge, but my best lambda is 26 chars.

    Extra kudos Find the fastest expression on a million-char string.

    Even more kudos To avoid typing it so often, you have a general filter function f:{y where x@y}. Define rmeb as a projection of f, using a composition as the left argument.

    SJT replied 8 months ago 3 Members · 5 Replies
  • 5 Replies
  • SJT

    Member
    June 3, 2023 at 12:00 am

    A surprise? Char is a sortable type and Less Than is as fast as Equals and faster than Not Equals.

    q)ts:100000 “quick brown fox”>” ”

    13 752

    q)ts:100000 “quick brown fox”<>” ”

    27 880

    q)ts:100000 “quick brown fox”=” ”

    13 752

    q)ts:100000 not”quick brown fox”=” ”

    23 864

  • SJT

    Member
    March 19, 2024 at 10:55 am

    Hint: there is a faster test than x=" " or null x.

  • roryk

    Member
    March 19, 2024 at 11:00 am

     

    {x where not(&’:)” “=x} / 23 chars and fast f[not(&’:)” “=] / as a projection

     

  • jfealy

    Member
    March 19, 2024 at 11:00 am

    One thing to consider is that the seed value for the derived function means any leading blanks will be removed. Can use prev

    q){x where not(&':)" "=x}s:" the quick brown fox " 
    "the quick brown fox " 
    q){x where not n&prev n:null x}s 
    " the quick brown fox "

     

  • jfealy

    Member
    March 19, 2024 at 11:00 am

    Masking is nice but as an alternative & still taking care not to remove leading blanks

    q){x where 1b,1_not” “~’:x}s:” the quick brown fox ” ” the quick brown fox ”

    Appears to be faster

    q)S:10000000#s 
    q)ts:5 {x where not n&prev n:null x}S 
    197 100663904 
    q)ts:5 {x where 1b,1_not" "~':x}S 
    147 83886816

    caching taking place?

    q)ts:5 {x where not n&prev n:null x}S 
    144 100663904 
    q)ts:5 {x where 1b,1_not" "~':x}S 
    148 83886816

Log in to reply.