# What is the best way to generate a function based on the other function definition?

**URL:** <https://discourse.julialang.org/t/what-is-the-best-way-to-generate-a-function-based-on-the-other-function-definition/26748>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [July 24, 2019, 3:46pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-generate-a-function-based-on-the-other-function-definition/26748 "2019-07-24T15:46:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![raff](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@raff](https://discourse.julialang.org/u/raff)\
**Post date:** [July 24, 2019, 3:46pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-generate-a-function-based-on-the-other-function-definition/26748/1 "2019-07-24T15:46:31Z")

</div>

Let’s say I have a function

```julia
function compute(x::Vector, y::Vector)
     return (x' * y) / sum(y)
end

```

Both `x` and `y` are vector with 0 or 1 values on each cell.

Based on this definition, I want to automatically generate a function, where for the variable in the denominator (`y`), I created a matrix Y, the rest variables remain to be vectors. The generated function should be something like:

```julia
function compute_generated(x::Vector, Y::Matrix)
    n = size(Y, 1)
    result = 0
    for i = 1:n 
         result += x' * Y[:,i] / i
    end
    return result
end

```

What is the best way to perform this task?  
I took a look at the Julia manual about `macro` and `@generated function`, but I am still confused about if the concept can be applied to the task.

Regards

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 25, 2019, 6:10am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-generate-a-function-based-on-the-other-function-definition/26748/2 "2019-07-25T06:10:09Z")

</div>

> [@raff](#):
>
> What is the best way to perform this task?

I don’t understand how the two functions are related — in the first one the denominator is `sum(y)`, in the kernel of the second one, `i`.

Generally, if you want to reduce a collection using a bivariate kernel function, look at `mapreduce` & friends. Generating a function from the source code of another functions is almost surely the wrong approach for what you are trying to do.

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [July 25, 2019, 11:31am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-generate-a-function-based-on-the-other-function-definition/26748/3 "2019-07-25T11:31:39Z")

</div>

I suspect this can be done with a macro, since the macro could look at the function you defined, break it apart and create a “different” function.

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [July 25, 2019, 11:38am UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-generate-a-function-based-on-the-other-function-definition/26748/4 "2019-07-25T11:38:01Z")

</div>

I don’t know if this is what you are after but take a look at the [`mapslices`](https://pkg.julialang.org/docs/julia/THl1k/1.1.1/base/arrays.html#Base.mapslices) function to apply a function to slices through a matrix.

---

<div class="post-metadata">

**Author:** ![raff](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@raff](https://discourse.julialang.org/u/raff)\
**Post date:** [July 25, 2019, 2:00pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-generate-a-function-based-on-the-other-function-definition/26748/5 "2019-07-25T14:00:12Z")

</div>

I have some rules that translate one function to another. For examples:

- only `sum(x)` and/or `sum(y)`, and other linear manipulation on those are allowed in the denominator.
- If `sum(x)` appears on the denominator, I need to to write a generated function that accept a matrix X. If it doesn’t appear, then the function should accept a vector x.

The goal is to create a library that make this process automatic, i.e. given someone write the original code, the library should automatically generate the transformed function.

---

<div class="post-metadata">

**Author:** ![raff](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@raff](https://discourse.julialang.org/u/raff)\
**Post date:** [July 25, 2019, 2:00pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-generate-a-function-based-on-the-other-function-definition/26748/6 "2019-07-25T14:00:59Z")

</div>

Yea. it looks like macros is the right way to do this.
