kona
kona copied to clipboard
Introduce a "format String" Verb
I like to suggest to introduce a verb for formating strings, similar to pythons %. I'd be a kind of inverse dollar.
I don't think there are many charters left that are available on most keyboards. On QWERTY keyboards the following charters are left: be §,€ or °. In the following I will use §, but that's just a example. /edit: I'd like to stress again: I'm not in favour of §, i just use that for the shake of the argument.
§ is diadic. The first argument is a string, the second one a array. Occurrence of "{}" in the left string are replaced by the value in the second argument, converted into a string
"A float: {} and the current time: {}" § (3.141; _t)
Is the same as
"A float: ",($3.141)," and the current time: ",($_t)
Formatting, equivalent to c could also be supported: (%d should be equivalent with {d}, %x equivalent to {x},
"A float: {2.3f} and the current time: {d}" § (3.141; _t)
would evaluate to
"A float: 03.141 and the current time: -616154346"
Of course Lists should be supported:
"A Pi and Tau: {1.3f}" § (3.141 6.283)
Kona code could functions could be embedded in doulbe curly braces:
"Several uneven numbers: {d{1+2*x}}" § (!100)
1+2*x is just regular kona code. That gets evaluted as a function for each element in the list. "d" is the format string.
I'd be happy to hear comments.
Not in favor of this. Too much magic for some typing convenience. K is minimal. This doesn't fit well in its model. Even kx.com's q (k4) doesn't provide such a verb. If you need it, you can write a function for it. Finally, kona is ascii only.
On Jun 23, 2015, at 7:24 AM, Miko4 [email protected] wrote:
I like to suggest to introduce a verb for formating strings, similar to pythons %. I'd be a kind of inverse dollar.
I don't think there are many charters left that are available on most keyboards. On QWERTY keyboards the following charters are left: be §,€ or °. In the following I will use §, but that's just a example
§ is diadic. The first argument is a string, the second one a array. Occurrence of "{}" in the left string are replaced by the value in the second argument, converted into a string
"A float: {} and the current time: {}" § (3.141; _t) Is the same as
"A float: ",($3.141)," and the current time: ",($_t) Formatting, equivalent to c could also be supported: (%d should be equivalent with {d}, %x equivalent to {x},
"A float: {2.3f} and the current time: {d}" § (3.141; _t) would evaluate to
"A float: 03.141 and the current time: -616154346" Of course Lists should be supported:
"A Pi and Tau: {1.3f}" § (3.141 6.283) Kona code could functions could be embedded in doulbe curly braces:
"Several uneven numbers: {d{1+2_x}}" § (!100) 1+2_x is just regular kona code. That gets evaluted as a function for each element in the list. "d" is the format string.
I'd be happy to hear comments.
— Reply to this email directly or view it on GitHub.
Not in favor of this formulation, in favor of the general idea. I have always thought vprintf
support, potentially as reserved verb _printf
, was a good idea, provided it is trivial to implement and maintain. It is an almost universally supported language feature.
Better put! It'll be more like vprintf - the second arg will be a list. Don't expect it to be trivial! For some embedded code I once implemented it in C (from scratch but minus floats). Getting all the details right can be fiddly.
On Jun 23, 2015, at 7:48 AM, Kevin Lawler [email protected] wrote:
Not in favor of this formulation, in favor of the general idea. I have always thought vprintf support, potentially as reserved verb _printf, was a good idea, provided it is trivial to implement and maintain. It is an almost universally supported language feature.
— Reply to this email directly or view it on GitHub.
The §,€ and ° characters are not part of the US-QWERTY keyboard layout.
It would be possible to add some simple overloads to $
to get most of the desired functionality- you can already do things like
,/$("I have ";23;" coconuts")
"I have 23 coconuts"
When simple concatenation of the natural string conversion of types is acceptable, and gain more control by using prototypes and string lengths
,/("";2;"")$("foo ";65;" bar")
"foo 65 bar"
K4 does type conversion based on a left character argument and K5 does something along those lines with symbols. For example, in K5:
`c`i`b$65 66 67
("A";66;1b)
There's still quite a bit of "unpopulated space" in left arguments to $
.