jkrumbiegel / Chain.jl

A Julia package for piping a value through a series of transformation expressions using a more convenient syntax than Julia's native piping functionality.
MIT License
368 stars 16 forks source link

Add a `@stop` or `@exit` flag #26

Open pdeffebach opened 3 years ago

pdeffebach commented 3 years ago

One thing that might be nice is to add a flag for exiting the @chain block.

One workflow I use at the REPL is to have a big block that I add to a bunch. But if I wrote a bug that is an an intermediate location, I want to be able to go to that spot.

here is a big block

julia> df = @chain begin 
       map(1:10) do i
           @addnt begin 
               @add apartment_id = rand(1:4)
               @add move_date = rand(dates)
               @add action_type = rand(["move_in", "move_out"])
           end
       end
       DataFrame
       @aside begin 
           move_ins = DataFrame(apartment_id = [1, 2, 3, 4], move_date = Date(2010, 01, 01), action_type = fill("move_in", 4))
       end
       vcat(move_ins, _)
       @orderby :apartment_id :move_date
       @transform diff_from_action = ifelse.(:action_type .== "move_in", 1, -1)
       groupby(:apartment_id) 
       @transform num_people_in_apartment_after_action = cumsum(:diff_from_action)
       groupby(:apartment_id)
       @transform cols(AsTable) = begin 
           @addnt begin 
               @add start_dates = :move_date
               @add end_dates = lead(:move_date)
           end
       end
       end

What if I think I created diff_from_action wrong? To get to the the chain at exactly that place, I would have to delete or comment out a lot of stuff. Or add an end and face a bunch of errors.

But if I could just add an @exit flag right after the diff_from_action command, I could just forget about all the stuff after.

This would solve one thing that made me hesitant about @chain earlier. One benefit of the %>% in dplyr was that it was easy to break out of the chain and return what you wanted.

EconometricsBySimulation commented 3 years ago

I think this is a good idea. I wonder if it needs to be a little more obvious than just @exit but maybe @EXIT or something so that looking through a chain with an early break one can immediately spot that it has been interrupted for debugging purposes. I suppose such a feature might be programmatically used for flow control. Something like ((sum(_.A) > x) && @exit).

pfarndt commented 3 years ago

Wouldn't @break be a better name for such an action? @exitseems to be a bit too much in the context of similarly named functions in julia.