kdb+k

Handling single-character strings - in a function or in its caller? ssr()


What is the common way to work with strings in q, in a way, who is responsible for handling a single-character string: function itself or a user who runs it?

Ex:

$ q
KDB+ 3.6 2019.04.02 Copyright (C) 1993-2019 Kx Systems
m32
q)ssr["bar";"r";"z"] /looks good at a first glance
"baz"
q)ssr["bar";"?";"z"] /but wait, nothing happens here
"bar"
q)ssr["bar";(),"?";"z"] /convert 1-char to list: ok
"zzz"

See the difference in sending a single letter (r) vs question mark (?). Just sending a single character ? itself didn't do anything useful.

Is it a feature of ssr? And what is the general case for single-char sending/receiving - who should be responsible in most situations for dealing with atoms vs lists?

Upd: Thanks to @terrylynch for pointing out this feature of ss/ssr:

q)ssr["bar?";"?";"z"]
"barz"

Solution

  • It's a feature of ss which in turn makes it a feature of ssr since ssr uses ss. See "supports some of the pattern-matching capabilities of like" comment: https://code.kx.com/q/ref/ss/

    It looks like it has a check for the lookup char/string of special (regex-related) characters - if it's a single char just treat it like a char, if it's a string type treat it as a regex pattern.