Chain.jl icon indicating copy to clipboard operation
Chain.jl copied to clipboard

Using anon functions in a nested @. function

Open max-sixty opened this issue 2 years ago • 2 comments

Another one from me — I'm adding these because I think this library is superb and I'm exploring how far it can go — I realize the past few issues have been corner cases.

Building on https://github.com/jkrumbiegel/Chain.jl/issues/42, this works:

s = "dbaf fag bdfgea ecbgfa dgbfe fa edacg agcfdbe cefdbg fdgea | fgbaed cdegbf dacfbge gdcfbe
abc acefbd gacedbf gbcde dgabf ca fgcedb cega dgbca cgeadb | ca bcged abgfd bca"

function inner(x)
    (x -> split(x, "|"))(x)
end

input = @chain s begin
    strip    

    split("\n")
    @. inner
end

But if we replace inner with the function, it fails:

input = @chain s begin
    strip    

    split("\n")
    @. (x -> split(x, "|")) # (or with (_) at the end)
end

# You can only use the @. macro and automatic first argument insertion if what follows is of the form `[Module.SubModule.]func`

My understanding from the docs is that that should be syntactic sugar for this, which also works:

input = @chain s begin
    strip    

    split("\n")
    (x -> split(x, "|")).(_)
end

max-sixty avatar Dec 08 '21 22:12 max-sixty

You could just do split.("|")? I'm not sure if adding the overhead for recognizing anonymous functions makes so much sense given all the other options to write this.

jkrumbiegel avatar Dec 09 '21 14:12 jkrumbiegel

For sure in this specific case there's no need, and I agree it's not important. It can be useful on the margin when debugging, or copying code from elsewhere. I was mostly surprised it wasn't consistent, rather than really needing it.

It's a corner case, and very reasonable to deprioritize. Thanks.

max-sixty avatar Dec 10 '21 00:12 max-sixty