For comparison, here is the analogous code from DataFramesMeta.

replace_syms(x, membernames) = x
function replace_syms(e::Expr, membernames)
    if e.head == :call && length(e.args) == 2 && e.args[1] == :^
        return e.args[2]
    elseif e.head == :.     # special case for :a.b
        return Expr(e.head, replace_syms(e.args[1], membernames),
                            typeof(e.args[2]) == Expr && e.args[2].head == 
:quote ? e.args[2] : replace_syms(e.args[2], membernames))
    elseif e.head != :quote
        return Expr(e.head, (isempty(e.args) ? e.args : map(x -> 
replace_syms(x, membernames), e.args))...)
    else
        if haskey(membernames, e.args[1])
            return membernames[e.args[1]]
        else
            a = gensym()
            membernames[e.args[1]] = a
            return a
        end
    end
end

function with_helper(d, body)
    membernames = Dict{Symbol, Symbol}()
    body = replace_syms(body, membernames)
    funargs = map(x -> :( getindex($d, $(Meta.quot(x))) ), 
collect(keys(membernames)))
    funname = gensym()
    return(:( function $funname($(collect(values(membernames))...)) $body 
end; $funname($(funargs...)) ))
end

macro with(d, body)
    esc(with_helper(d, body))
end

On Wednesday, July 8, 2015 at 3:50:12 PM UTC-4, Brandon Taylor wrote:
>
> It should be noted that in R, environments are simply pointers. They take 
> up no memory and shouldn't cause drastic slowdowns (I don't think).
>
> On Wednesday, July 8, 2015 at 3:48:08 PM UTC-4, Brandon Taylor wrote:
>>
>> For example, here's a code snippet from R:
>>
>> function (x, data) eval(x$expr, data, x$env)
>>
>> x is an expression which contains an environment attribute. It is 
>> evaluated first checking the bindings in data (data being a dataframe) and 
>> then checing the bindings in x's environment.
>>
>> On Wednesday, July 8, 2015 at 3:37:04 PM UTC-4, Brandon Taylor wrote:
>>>
>>> *reifying. Deifying environments might not be the best idea.
>>>
>>> On Wednesday, July 8, 2015 at 3:34:53 PM UTC-4, Brandon Taylor wrote:
>>>>
>>>> I was aware of those packages (though I hadn't read the discussions 
>>>> referenced). Macros are great but they are incredibly difficult to reason 
>>>> with concerning issues of scope (at least for me). Deifying environments 
>>>> could solve all of these issues (and so much more) in one fell swoop.
>>>>
>>>> On Wednesday, July 8, 2015 at 3:20:00 PM UTC-4, David Gold wrote:
>>>>>
>>>>> Some of these issues have been thought about fairly extensively by the 
>>>>> stats community in particular, precisely on account of the use cases you 
>>>>> cite:
>>>>>
>>>>> https://github.com/JuliaStats/DataFrames.jl/pull/472
>>>>> https://github.com/JuliaStats/DataFrames.jl/issues/504 
>>>>> <https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2FJuliaStats%2FDataFrames.jl%2Fissues%2F504&sa=D&sntz=1&usg=AFQjCNHgUEZP8TyJ_BuUyyFA5SIxneOJTA>
>>>>>
>>>>> I think that the matter is still very much an open question. I have no 
>>>>> sense that anything is going to be added to Base Julia itself. Currently, 
>>>>> the best way (that I know of, anyway) to achieve the delayed evaluation 
>>>>> effect is via the use of macros. See for instance:
>>>>>
>>>>> https://github.com/JuliaStats/DataFramesMeta.jl
>>>>> https://github.com/one-more-minute/Lazy.jl
>>>>>
>>>>> I'm hope somebody else will be able to pop in an give a more thorough 
>>>>> answer, but the above may at least be a place to start.
>>>>>
>>>>> On Wednesday, July 8, 2015 at 2:03:45 PM UTC-4, Brandon Taylor wrote:
>>>>>>
>>>>>> Hadley Wickham's lazyeval package in R is pretty cool in that you can 
>>>>>> attach an environment to an expression, pass it in and out of functions 
>>>>>> with various modifications, and then evaluate the expression within the 
>>>>>> original environment (or any other environment that you choose). R in 
>>>>>> general has the functions like list2env and list(environment()) that 
>>>>>> allow 
>>>>>> one to convert an environment into a list and back again (list being the 
>>>>>> R 
>>>>>> equivalent of a Dict). Are there any plans to add these kind of features 
>>>>>> to 
>>>>>> Julia?
>>>>>>
>>>>>

Reply via email to