Pre-ANN: StringInterpolation.jl

274 views
Skip to first unread message

Eric Forgy

unread,
Jan 5, 2016, 9:34:06 PM1/5/16
to julia-users
Hi,

It's not easy getting my old grey matter to learn new tricks, but I'm slowly learning Julia. I doubt this is worth adding to METADATA, but I find it useful for my work and hope others do too. If there are any ways to improve it, please let me know.

Best regards,
Eric

From the README:


StringInterpolation.jl

String interpolation is an awesome feature of Julia, but string interpolation for non-standard string literals is not automatic and requires significant boilerplate code to make it work.

This package simply resurrects an old Base method `interp_parse` and adds a macro `@interpolate`. For example:

julia> Pkg.clone("https://github.com/EricForgy/StringInterpolation.jl.git")
julia
> using StringInterpolation
julia
> x = "World"
julia
> @interpolate "Hello \$x"
"Hello World"

Note the $ is escaped in the string we want to interpolate.

The intended use for this package is when building non-standard string literals. For example:

macro test_str(s)
   
return quote
        str
= @interpolate $s
       
# Do what you want to do with interpolated string here.
        sprint
(print,str)
   
end
end

Example

The following non-standard string literal simply makes 3 copies of the interpolated string:

macro triple_str(s)
   
return quote
        str
= @interpolate $s
        sprint
(print,str^3)
   
end
end

Then, you can use the macro as follows:

julia> x = "World"; println(triple"Hello \$x\n")
Hello World
Hello World
Hello World

Scott Jones

unread,
Jan 6, 2016, 1:31:19 AM1/6/16
to julia-users
Looks like a good start!
You might also want to handle the $(xxx) form of interpolation (which can get pretty tricky I imagine), as well as all of the \ escapes, such as \n, \x, \u, \U, etc.

I'd like just this, but with the Swift syntax for interpolation, i.e. \(xxx), which doesn't have the problem of causing a frequently used character ($) to have to be quoted
(\$) where it wouldn't be in C/C++/Java/etc. (or for LaTex strings).

Eric Forgy

unread,
Jan 6, 2016, 2:31:18 AM1/6/16
to julia-users
Hi Scott,

Thanks for the enthusiasm :)

 
You might also want to handle the $(xxx) form of interpolation (which can get pretty tricky I imagine), as well as all of the \ escapes, such as \n, \x, \u, \U, etc.

`interp_parse` was written by Stefan so you can imagine it should be fairly complete, i.e. it does already handle $(xxx) and all escape characters :)

When it sees an unescaped $, it will call "parse" with greedy = false, which continues until it finds a complete expression. This is how it handles $(xxx).

Scott Jones

unread,
Jan 6, 2016, 9:21:43 AM1/6/16
to julia-users
Looking into it further, it seems that interp_parse is only handling the interpolation, but the sprint(print you must call print_unescaped, which handles the standard \ sequences.
That's better (not duplicating the \ handling code, both in print_unescaped and interp_parse).

Good stuff, Eric!

Scott Jones

unread,
Jan 7, 2016, 12:41:56 PM1/7/16
to julia-users
Well, you know the saying "Imitation is the sincerest form of flattery"?
Well, I was so impressed by your work (and more so now that I just saw JuliaJS!), that I was motivated to write some code!

Your package gave me the knowledge of how to implement the type of interpolation that I wanted in Julia, so I implemented my own package:
which currently has support for Swift-style interpolation and Unicode literals, i.e. "\(expr)" and "\u{hexdigits}".
I also added support for having readable emoji and latex named literal characters in strings, as "\:emojiname:" and "\{latexname}".

Some examples:
julia> z = "World" ; u"Hello \(z)"
"Hello World"
julia> u"\u{1f596}   \{circ}   \:grin:    "
"🖖   ∘   😁    "


On Wednesday, January 6, 2016 at 2:31:18 AM UTC-5, Eric Forgy wrote:

Eric Forgy

unread,
Jan 7, 2016, 6:36:24 PM1/7/16
to julia-users
Haha! :D

Scott, that is awesome :)

I don't deserve any credit for StringInterpolation. It is simply Stefan's old interp_parse resurrected and even JuliaJS is largely an immitation of Blink. I should make this more clear in the READMEs but was rushing to get them out hoping to get feedback to make them better. I plan to use JuliaJS for building web dashboards (and maybe a Julia-based JuliaBox).

Combining your StringUtils with JuliaJS (or whatever its name will be - suggestions welcome - JSCall?), we can build the next Slack 🦄😃
Reply all
Reply to author
Forward
0 new messages