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

Feature request: f.(x) do ... end #33800

Open
tkf opened this issue Nov 9, 2019 · 3 comments · May be fixed by #36156
Open

Feature request: f.(x) do ... end #33800

tkf opened this issue Nov 9, 2019 · 3 comments · May be fixed by #36156
Labels
broadcast Applying a function over a collection

Comments

@tkf
Copy link
Member

tkf commented Nov 9, 2019

It looks like the dot-call syntax can't be used with the do notation at the moment:

julia> :(f.(x) do; end)
ERROR: syntax: missing comma or ) in argument list

But I think it is handy in some situations. For example:

first_nonempty_lines = open.(files) do io
    for line in eachline(io)
        isempty(line) || return line
    end
end

Does it make sense to support this?

@vchuravy
Copy link
Sponsor Member

This reads to me as very surprising.

I personally find the below much cleaner.

map(files) do f
  open(f, "w") do io
    for line in eachline(io)
        isempty(line) || return line
    end
  end
end

One could do:

map(open.(files, "w")) do io

end

but of-course that runs the issue that open no longer automatically closes the io object...

@tkf
Copy link
Member Author

tkf commented Nov 14, 2019

Is this because .( is easy to miss when it is followed by a large do block?

If that's the case, wouldn't this be less of a problem in a more complex example?

open.(joinpath.(directory, stemnames .* ".jl")) do io
    for line in eachline(io)
        isempty(line) || return line
    end
end

@ararslan
Copy link
Member

ararslan commented Nov 16, 2019

At first I wasn't convinced, but given that this works:

julia> files = ["a", "b", "c"];

julia> open.(io->println(io, "hi"), files, "w")
3-element Array{Nothing,1}:
 nothing
 nothing
 nothing

julia> open.(io->read(io, String), files, "r")
3-element Array{String,1}:
 "hi\n"
 "hi\n"
 "hi\n"

it seems natural that do should work as well, since it should be functionally (no pun intended) equivalent to specifying the function "normally."

EDIT: I changed my mind, pun retroactively intended.

@ararslan ararslan added the broadcast Applying a function over a collection label Nov 16, 2019
@tkf tkf linked a pull request Jun 5, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
broadcast Applying a function over a collection
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants