[Rd] "+" operator on characters revisited
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Jan 24 20:30:29 CET 2011
On Mon, Jan 24, 2011 at 2:15 PM, Davor Cubranic <cubranic at stat.ubc.ca> wrote:
> On 2011-01-23, at 4:34 AM, Gabor Grothendieck wrote:
>
>> On Sun, Jan 23, 2011 at 6:56 AM, Vitalie S. <spinuvit.list at gmail.com> wrote:
>>> Gabor Grothendieck <ggrothendieck at gmail.com> writes:
>>>
>>>> Also the gsubfn supports quasi perl style string interpolation that
>>>> can sometimes be used to avoid the use of paste in the first place.
>>>> Just preface the function in question by fn$ like this:
>>>>
>>>> library(gsubfn)
>>>> fn$cat("pi = $pi\n")
>>>
>>> Thanks for the tip. Not bad indeed.
>>> Almost as readable as
>>>
>>> cat("pi = " + pi + "\n")
>>
>> To me the + can be substantially less readable. The need to
>> repeatedly quote everything makes it just as bad as paste. Compare
>> the following and try to figure out if there is an error in quoting in
>> the + and paste solutions. Trying to distinguish the single and
>> double quotes is pretty difficult but simple in the fn$ and sprintf
>> solutions. Even if there were no quotes the constant need to
>> interpose quotes makes it hard to read.
>
> That may be a matter of taste, but FWIW it seems that shell-style string interpolation (using the dollar prefix) has going out of style in recent scripting languages. Ruby uses the expression substitution construct ("#{expr}"), while Python has "str.format", both allowing arbitrary expressions.
>
fn$ supports that too using `...`
> library(sqldf)
> fn$sqldf("select * from BOD where demand > `mean(BOD$demand)` limit 2")
Time demand
1 3 19
2 4 16
> And most editors have syntax highlighting that distinguishes strings from other program elements. This makes quoting errors pretty obvious.
>
That only makes it slightly easier to handle the mess. Its better to
get rid of the quotes in the first place.
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-devel
mailing list