I just created a pull request. I hope I got it right. This is the first time I use the pull-request feature at Github.
https://github.com/JuliaLang/Formatting.jl/pull/15 Daniel. On 28 September 2015 at 07:03, Kevin Squire <kevin.squ...@gmail.com> wrote: > How about submitting a patch to Formatting.jl? > > On Sunday, September 27, 2015, Michael Hatherly <michaelhathe...@gmail.com> > wrote: > >> As mentioned in the other thread, >> https://groups.google.com/d/msg/julia-users/hBbEGEopi0A/OX4ZEhFnBgAJ and >> https://groups.google.com/d/msg/julia-users/hBbEGEopi0A/fKQcqDEVBgAJ, >> there are concerns about generating new code for every single formatting >> string used. I guess generally this won’t be much of an issue, but it could >> be. Maybe write a package and see how much interest there is first? >> >> — Mike >> >> On Sunday, 27 September 2015 13:02:25 UTC+2, Daniel Carrera wrote: >>> >>> Hello, >>> >>> I made a trivial change to a some clever code by Tim Holy, and used it >>> to make printf() and sprintf() function with the familiar syntax that we >>> know from C/C++ (requires Julia 0.4): >>> >>> >>> immutable FormatString{S} end >>> >>> FormatString(str::AbstractString) = FormatString{symbol(str)} >>> >>> @generated function Base.print{format}(::Type{FormatString{format}}, >>> args...) >>> meta = Expr(:meta, :inline) >>> fmt = string(format) >>> allargs = [:(args[$d]) for d = 1:length(args)] >>> quote >>> @printf($fmt, $(allargs...)) >>> end >>> end >>> @generated function Base.sprint{format}(::Type{FormatString{format}}, >>> args...) >>> meta = Expr(:meta, :inline) >>> fmt = string(format) >>> allargs = [:(args[$d]) for d = 1:length(args)] >>> quote >>> @sprintf($fmt, $(allargs...)) >>> end >>> end >>> >>> function printf(s::AbstractString, args...) >>> print(FormatString(s), args...) >>> end >>> function sprintf(s::AbstractString, args...) >>> print(FormatString(s), args...) >>> end >>> >>> >>> Could (or should) something like this be included in Julia by default? >>> The first time you call printf() sprintf() with a new format string, the >>> function call is slower than the @printf and @sprintf macros, but >>> subsequent calls are just as fast: >>> >>> julia> @time @printf("%7d %7.2f", 220/7, 22/7) >>> 31 3.14 0.024334 seconds (20.68 k allocations: 912.738 KB) >>> >>> julia> @time @printf("%7d %7.2f", 220/7, 22/7) >>> 31 3.14 0.000102 seconds (30 allocations: 1.094 KB) >>> >>> julia> >>> >>> julia> fmt = "%6d %7.2f" >>> "%6d %7.2f" >>> >>> julia> @time printf(fmt, 220/7, 22/7) >>> 31 3.14 0.036154 seconds (36.81 k allocations: 1.675 MB) >>> >>> julia> @time printf(fmt, 220/7, 22/7) >>> 31 3.14 0.000095 seconds (37 allocations: 1.250 KB) >>> >>> >>> >>> Daniel. >>> >>