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

@chain one-liner nested in an outer @chain may not work? #42

Closed
max-sixty opened this issue Dec 8, 2021 · 5 comments
Closed

@chain one-liner nested in an outer @chain may not work? #42

max-sixty opened this issue Dec 8, 2021 · 5 comments

Comments

@max-sixty
Copy link

This might be "overusing" @chain, but I found there was an inconsistency between one-liner and nested blocks with this:

This works:

@chain read(open("Day-8.txt"), String) begin
    strip
    split("\n")
    map(_) do _
        map(split(_, "|")) do _
            @chain _ begin
                strip
                split
            end
        end
    end
end

But making the inner @chain into a one-line version fails with "Malformed nested @chain macro"

@chain read(open("Day-8.txt"), String) begin
    strip
    split("\n")
    map(_) do _
        map(split(_, "|")) do _
            @chain _ strip split
        end
    end
end

FYI I tried using do x rather than do _, but to no avail.

I'm not sure the nesting is a good design — I was attempting to parse a heavily nested string, and was using new indentation to signify each level of nesting — so I'm putting this in for info, rather than something that's important per-se.

Thanks for the excellent package, I'm really enjoying using it

@max-sixty
Copy link
Author

Tangentially — if there were a way of combining the map ... do x with @chain; that would be ideal; e.g.:

@chain read(open("Day-8.txt"), String) begin
    strip
    split("\n")
    @. begin
        split("|")
        @. begin
            strip
            split
        end
    end
end

@jkrumbiegel
Copy link
Owner

The single line nested chain error is a bug, fixed that here #43

The other thing is that you should not really repeat the underscore so often, it can work if the variable inside map just shadows the outer one, but it's not very clear. Better like this:

@chain "a\nb" begin
    strip
    split("\n")
    map(_) do x
        map(split(x, "|")) do y
            @chain y strip split
        end
    end
end

@jkrumbiegel
Copy link
Owner

Ah and I guess you don't need the underscore in the nested @chain anyway, because it's implicitly inserted

@max-sixty
Copy link
Author

The single line nested chain error is a bug, fixed that here #43

Thanks a lot!

I agree re the underscores. That's related to something I liked about the idea of combining the map ... do x with @chain (with, say @. as in the example, though maybe there are better names) — the whole block could be point-free.

@max-sixty
Copy link
Author

Re the narrower issue — confirmed it works — thanks again. I'll close this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants