Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing a function to join #30328

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Standard library changes
`--project=@.` ([#29108]).
* The `spawn` API is now more flexible and supports taking IOBuffer directly as a I/O stream,
converting to a system pipe as needed ([#30278]).
* `join` now accepts a function argument ([#TODO]).

#### Dates
* New `DateTime(::Date, ::Time)` constructor ([#29754]).
Expand Down
26 changes: 19 additions & 7 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,52 +230,64 @@ IOBuffer(s::SubString{String}) = IOBuffer(view(unsafe_wrap(Vector{UInt8}, s.stri
# join is implemented using IO

"""
join([io::IO,] strings, delim, [last])
join([io::IO,] [f::Function,] strings, delim, [last])

Join an array of `strings` into a single string, inserting the given delimiter between
adjacent strings. If `last` is given, it will be used instead of `delim` between the last
two strings. If `io` is given, the result is written to `io` rather than returned as
as a `String`.
as a `String`. If `f` is given, the function is applied to each string before joining.

!!! compat "Julia 1.1"
Providing a function argument requires Julia 1.1 or later.

# Examples
```jldoctest
julia> join(["apples", "bananas", "pineapples"], ", ", " and ")
"apples, bananas and pineapples"

julia> join(uppercasefirst, ["pears", "rhubarb"], " & ")
"Pears & Rhubarb"
```

`strings` can be any iterable over elements `x` which are convertible to strings
via `print(io::IOBuffer, x)`. `strings` will be printed to `io`.
"""
function join(io::IO, strings, delim, last)
function join(io::IO, f::Function, strings, delim, last)
first = true
local prev
for str in strings
if @isdefined prev
first ? (first = false) : print(io, delim)
print(io, prev)
print(io, f(prev))
end
prev = str
end
if @isdefined prev
first || print(io, last)
print(io, prev)
print(io, f(prev))
end
nothing
end
function join(io::IO, strings, delim="")
function join(io::IO, f::Function, strings, delim="")
# Specialization of the above code when delim==last,
# which lets us emit (compile) less code
first = true
for str in strings
first ? (first = false) : print(io, delim)
print(io, str)
print(io, f(str))
end
end
join(io::IO, strings, delim="") = join(io, identity, strings, delim)
join(io::IO, strings, delim, last) = join(io, identity, strings, delim, last)

join(strings) = sprint(join, strings)
join(strings, delim) = sprint(join, strings, delim)
join(strings, delim, last) = sprint(join, strings, delim, last)

join(f::Function, strings) = sprint((io, s)->join(io, f, s), strings)
join(f::Function, strings, delim) = sprint((io, s)->join(io, f, s, delim), strings)
join(f::Function, strings, delim, last) = sprint((io, s)->join(io, f, s, delim, last), strings)

## string escaping & unescaping ##

need_full_hex(c::Union{Nothing, AbstractChar}) = c !== nothing && isxdigit(c)
Expand Down
3 changes: 3 additions & 0 deletions test/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ end
@test join(()) == ""
@test join((), ", ") == ""
@test join((), ", ", ", and ") == ""
@test join(uppercase, ["a", "b", "c"]) == "ABC"
@test join(abs2, 1:4, " ") == "1 4 9 16"
@test join(x->round(x, digits=3), [π, π, π], ' ', "... ") == "3.142 3.142... 3.142"
end

# quotes + interpolation (issue #455)
Expand Down