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

empty do chain syntax

Open jkrumbiegel opened this issue 2 years ago • 5 comments

Allows this

@chain begin
    ["hello", "world"]
    @chain map() do
        collect
        @chain filter() do
            uppercase
            _ ∉ ('E', 'L')
        end
        String
    end
end

instead of this

@chain begin
    ["hello", "world"]
    map(_) do x
        @chain x begin
            collect
            filter(_) do y
                @chain y begin
                    uppercase
                    _ ∉ ('E', 'L')
                end
            end
            String
        end
    end
end

jkrumbiegel avatar Dec 09 '21 14:12 jkrumbiegel

I don't hate this. It's kind of cool. But it is unnecessary in the sense that DataFramesMeta.jl doesn't really have a use-case for this kind of thing.

Could you give more examples for where this kind of feature would be useful?

pdeffebach avatar Dec 11 '21 21:12 pdeffebach

Dataframesmeta has no need for nesting, that's true. I've mostly needed better nesting when dealing with other collections, for example when doing string parsing or web scraping or things like that. There's really no good idiom to continue a chain at a lower nesting level, unless you skip out of the logic with a normal map, or put the chain in manually, but that loses the succinctness of the macro.

jkrumbiegel avatar Dec 12 '21 06:12 jkrumbiegel

FWIW I've been using this branch for Advent of Code these past few days, where there's a decent amount of parsing and general transformations. I like it a lot, as discussed in https://github.com/jkrumbiegel/Chain.jl/issues/45. Here's an example:

links = @chain input begin
    strip
    split("\n")
    @chain map() do
        split("-")
        Pair(_...) 
        [_, _ |> reverse]
    end
    flatten
    collect
end

max-sixty avatar Dec 13 '21 02:12 max-sixty

FWIW this just appeared on my notifications; it looks like the same design! https://discourse.julialang.org/t/ann-datapipes-jl/60734/61

max-sixty avatar Dec 13 '21 20:12 max-sixty

I posted the PR with the design on slack, possibly got it from there

jkrumbiegel avatar Dec 13 '21 22:12 jkrumbiegel