# Metaprogramming: create function for different ops, +, -, \* etc

**URL:** <https://discourse.julialang.org/t/metaprogramming-create-function-for-different-ops-etc/762>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 6, 2016, 2:31pm UTC](https://discourse.julialang.org/t/metaprogramming-create-function-for-different-ops-etc/762 "2016-12-06T14:31:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [December 6, 2016, 2:31pm UTC](https://discourse.julialang.org/t/metaprogramming-create-function-for-different-ops-etc/762/1 "2016-12-06T14:31:23Z")

</div>

I would like to understand how to generate functions programmatically.

basically how to do this (nevermind if this example is sensible):

```julia
function -(d1::DataFrame,d2::DataFrame)
    if !(nrow(d1)==nrow(d2))
        error("need same num of rows")
    end
    if !(names(d1).==names(d2))
        error("need same colnames")
    end
    df = DataFrame(df1)
    for n in names(d1)
        df[n] = df1[n] .- df2[n]
    end
end
function +(d1::DataFrame,d2::DataFrame)
    if !(nrow(d1)==nrow(d2))
        error("need same num of rows")
    end
    if !(names(d1).==names(d2))
        error("need same colnames")
    end
    df = DataFrame(df1)
    for n in names(d1)
        df[n] = df1[n] .+ df2[n]
    end
end

# would like to do something like
# for op = (:+,:-, :*, :/,)
# eval(:(($op)(a,b) = ... )
# end

```

thanks

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [December 6, 2016, 2:41pm UTC](https://discourse.julialang.org/t/metaprogramming-create-function-for-different-ops-etc/762/2 "2016-12-06T14:41:58Z")

</div>

I am not sure about your use. Here is an example of generating arithmetic functions in a loop (from Base/float.jl)

```
 for op in (:+,:-,:*,:/,:\,:^)
     @eval ($op)(a::Float16, b::Float16) = Float16(($op)(Float32(a), Float32(b)))
 end

```

The `@eval` lets `($op)` be replaced with the appropriate token for each of the symbols in (:+,:-,:\*,:/,:,:^) .  
For a longer “program”

```
for op in (:+,:-,:*,:/,:\,:^)
      @eval begin
             # ...
      end
end

```

If this is helpful to you, feel welcome to ask follow on questions. If your question is about creating functions through developing Expr[essions], let us know that.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 6, 2016, 2:43pm UTC](https://discourse.julialang.org/t/metaprogramming-create-function-for-different-ops-etc/762/3 "2016-12-06T14:43:55Z")

</div>

```nohighlight
import Base: +, -, *, /

type A
   v::Int
end

for op = (:+,:-, :*, :/,)
   @eval begin
       ($op)(a::A, b::A) = A($op(a.v, b.v))
   end
end

julia> A(1) + A(2)
A(3)

julia> A(1) - A(2)
A(-1)

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 6, 2016, 2:46pm UTC](https://discourse.julialang.org/t/metaprogramming-create-function-for-different-ops-etc/762/4 "2016-12-06T14:46:48Z")

</div>

To turn the `:+` into a `:.+` symbol, this works but looks ugly: `:($(Symbol("."*string(:+))))`. Is there a better way?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [December 6, 2016, 3:06pm UTC](https://discourse.julialang.org/t/metaprogramming-create-function-for-different-ops-etc/762/5 "2016-12-06T15:06:11Z")

</div>

```julia
julia> op = :+
:+

julia> Symbol(".", op)
:.+

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 6, 2016, 3:22pm UTC](https://discourse.julialang.org/t/metaprogramming-create-function-for-different-ops-etc/762/6 "2016-12-06T15:22:20Z")

</div>

Cool. Can even shave one more char off: `Symbol(:.,:+)`
