# How to make functions multipliable?

**URL:** https://discourse.julialang.org/t/how-to-make-functions-multipliable/55487
**Category:** General Usage
**Tags:** question
**Created:** [February 17, 2021, 8:55pm UTC](https://discourse.julialang.org/t/how-to-make-functions-multipliable/55487 "2021-02-17T20:55:48Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)
#### Post date: [February 17, 2021, 8:55pm UTC](https://discourse.julialang.org/t/how-to-make-functions-multipliable/55487/1 "2021-02-17T20:55:48Z")

</div>

I want to make functions be able to be multiplied:

```julia
f(x) = x^2
g = 3 * f
g(10) # == 300

```

Is this achievable?

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [February 17, 2021, 8:59pm UTC](https://discourse.julialang.org/t/how-to-make-functions-multipliable/55487/2 "2021-02-17T20:59:59Z")

</div>

No. This is not feasible. However you can do

```julia
g(args...) = 3 * f(args...)

```

---

<div class="post-metadata">

### Author: ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)
#### Post date: [February 17, 2021, 9:00pm UTC](https://discourse.julialang.org/t/how-to-make-functions-multipliable/55487/3 "2021-02-17T21:00:48Z")

</div>

Shouldn’t it be `g(args... ; ka...) = 3 * f(args... ; ka...)`?

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [February 17, 2021, 9:01pm UTC](https://discourse.julialang.org/t/how-to-make-functions-multipliable/55487/5 "2021-02-17T21:01:05Z")

</div>

You certainly _can_ do this:

```julia
julia> function Base.:*(x::Number, f::Function)
         y -> x * f(y)
       end

julia> f(x) = x^2
f (generic function with 1 method)

julia> g = 3 * f
#13 (generic function with 1 method)

julia> g(10)
300

```

but I wouldn’t recommend it. This is a form of [Type Piracy](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-type-piracy) which we don’t recommend because it can cause unexpected effects in other users’ code.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [February 17, 2021, 9:07pm UTC](https://discourse.julialang.org/t/how-to-make-functions-multipliable/55487/6 "2021-02-17T21:07:10Z")

</div>

> [@NightMachinary](#):
>
> Shouldn’t it be `g(args... ; ka...) = 3 * f(args... ; ka...)` ?

Maybe you want to pass multiple arguments to an inner function like this?

```julia
julia> f(x,y;z) = x^2 + y^2 + z^2 # many parameters
f (generic function with 1 method)

julia> g(func,newarg) = 3*func(newarg) # enclosing function
g (generic function with 1 method)

julia> x = 10; z = 5; 

julia> y = 3 
3

julia> g( y -> f(x,y,z=z), y )
402

```

You can also compose:

```julia
julia> f(x) = x^2
f (generic function with 1 method)

julia> g(x) = 3*x
g (generic function with 1 method)

julia> 10 |> f |> g
300

julia> (g ∘ f)(10) # \circ
300

```

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [February 17, 2021, 9:07pm UTC](https://discourse.julialang.org/t/how-to-make-functions-multipliable/55487/7 "2021-02-17T21:07:13Z")

</div>

Sure, if it has keyword arguments then that works.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [February 17, 2021, 9:12pm UTC](https://discourse.julialang.org/t/how-to-make-functions-multipliable/55487/8 "2021-02-17T21:12:07Z")

</div>

You can also do a callable struct

```julia
julia> struct G{T}
           f::T
       end

julia> (x::G)(args...; kwargs...) = 3 * x.f(args...; kwargs...);

julia> f(x, y) = x + y;

julia> g(1, 2)
9

```

---

<div class="post-metadata">

### Author: ![anon74562486](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@anon74562486](https://discourse.julialang.org/u/anon74562486)
#### Post date: [February 17, 2021, 9:12pm UTC](https://discourse.julialang.org/t/how-to-make-functions-multipliable/55487/9 "2021-02-17T21:12:43Z")

</div>

This works but I don’t know if is a good practice:

```julia
multiplication(x) = y -> y*x 
g = multiplication(3) ∘ f
g(10) #300

```

or

```julia
g = (x -> 3x) ∘ f
g(10) #300

```

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [February 17, 2021, 10:15pm UTC](https://discourse.julialang.org/t/how-to-make-functions-multipliable/55487/10 "2021-02-17T22:15:55Z")

</div>

Maybe worth noting that every function has a type, and thus you can do this for your one specific function without piracy.

```julia
julia> Base.:(-)(::typeof(f)) = x -> -f(x)

julia> map(-f, [1,2,3])
3-element Vector{Int64}:
 -1
 -4
 -9

```
