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

Add function composition #299

Open
pdeffebach opened this issue Sep 15, 2021 · 2 comments
Open

Add function composition #299

pdeffebach opened this issue Sep 15, 2021 · 2 comments

Comments

@pdeffebach
Copy link
Collaborator

I think that when we see fun1(fun2(x)) the function passed to transform should be fun1 \circ fun2. That way we can take advantage of more fast paths iirc.

@bkamins
Copy link
Member

bkamins commented Sep 15, 2021

Yes and the thing is that fun1 \circ fun2 is compiled only once as opposed to x -> fun1(fun2(x)) which has to be compiled every time. but I would label it as a minor improvement.

@nalimilan
Copy link
Member

Actually I had implemented something like that back in 2019 before you cleaned up the package. FWIW, here are the functions I had written:

"""Check whether expression is a single-argument call, like f(x)"""
isoneargcall(e::Expr) =
    e.head === :call && length(e.args) == 2 && e.args[1] isa Symbol

function tocomposition(body)
    if body.head == :(=) && length(body.args) == 2 &&
        body.args[2] isa Expr
        funs = Symbol[]
        e = body.args[2]
        while e isa Expr && isoneargcall(e)
            push!(funs, e.args[1])
            e = e.args[2]
        end
        if e isa Symbol || (e isa QuoteNode && e.value isa Symbol)
            # Recursion parsed the full expression
            return mapreduce(eval, , funs)
        else # Expression doesn't consist only in single-argument calls
            return body
        end
    end
    return body
end

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

3 participants