Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve method content as an `Expr`ession

I have a function f defined as follows.

f(x, y) = 3x^2 + x*y - 2y + 1

How can I retrieve the following quote block for this method, which includes the function contents?

quote  # REPL[0], line 2:
    ((3 * x ^ 2 + x * y) - 2y) + 1
end
like image 918
Harrison Grodin Avatar asked Feb 28 '17 16:02

Harrison Grodin


People also ask

How do you get dynamic content in expression Power Automate?

Add dynamic content Power AutomateGo to +Create > Instant cloud flow > Manually trigger the flow > + Add an input > Number. Next, we will add a compose action with an expression using add function. Go to + New step > Compose. This is how to add dynamic content in Power Automate.

How do you use expression in Power Automate?

To use an expression in your flow, first open the Add dynamic content menu. You will see a new Expression tab, select that. On the Expression tab there is a list of the common expressions that are used inside of flows. They are categorized by area, and you can see the full list by selecting See more on each category.

How do I use formatDateTime in Power Automate?

Example stepsSelect the input field where you want the formatted DateTime value. Go to the expression editor (go to Add dynamic content > select the Expression tab). Type formatDateTime() (or look under Date and time functions). Provide the value to be formatted and surrounded by single quotes.

How do you use utcNow in flow?

@utcNow Function Method The @utcNow function can be manipulated by using an additional function, addHours(). To use this rather than simply putting “utcNow()” in the expression section, you would need to put addHours() then pass utcNow() into it as the first parameter.


1 Answers

As folks have mentioned in the comments, digging through the fields of the methods like this isn't a stable or officially supported API. Further, your simple example is deceiving. This isn't, in general, representative of the original code you wrote for the method. It's a simplified intermediate AST representation with single-assignment variables and drastically simplified control flow. In general, the AST it returns isn't valid top-level Julia code. It just so happens that for your simple example, it is.

That said, there is a documented way to do this. You can use code_lowered() to get access to this intermediate representation without digging through undocumented fields. This will work across Julia versions, but I don't think there are official guarantees on the stability of the intermediate representation yet. Here's a slightly more complicated example:

julia> f(X) = for elt in X; println(elt); end
f (generic function with 1 method)

julia> code_lowered(f)[1]
LambdaInfo template for f(X) at REPL[17]:1
:(begin
        nothing
        SSAValue(0) = X
        #temp# = (Base.start)(SSAValue(0))
        4:
        unless !((Base.done)(SSAValue(0),#temp#)) goto 13
        SSAValue(1) = (Base.next)(SSAValue(0),#temp#)
        elt = (Core.getfield)(SSAValue(1),1)
        #temp# = (Core.getfield)(SSAValue(1),2) # line 1:
        (Main.println)(elt)
        11:
        goto 4
        13:
        return
    end)

julia> code_lowered(f)[1] == methods(f).ms[1].lambda_template
true

If you really want to see the code exactly as it was written, the best way is to use the embedded file and line information and refer to the original source. Note that this is precisely the manner in which Gallium.jl (Julia's debugger) finds the source to display as it steps through functions. It's undocumented, but you can even access the REPL history for functions defined interactively. See how Gallium does it through here.

like image 176
mbauman Avatar answered Sep 29 '22 05:09

mbauman