# Perhaps a new way of composing functions

**URL:** <https://discourse.julialang.org/t/perhaps-a-new-way-of-composing-functions/32366>\
**Category:** Internals & Design\
**Created:** [December 17, 2019, 1:25am UTC](https://discourse.julialang.org/t/perhaps-a-new-way-of-composing-functions/32366 "2019-12-17T01:25:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [December 17, 2019, 1:25am UTC](https://discourse.julialang.org/t/perhaps-a-new-way-of-composing-functions/32366/1 "2019-12-17T01:25:41Z")

</div>

```julia
julia> msg = "Hello!"
"Hello!"

julia> print("Normal: "); println(uppercase(msg))
Normal: HELLO!

julia> print("Piping: "); msg |> uppercase |> println
Piping: HELLO!

julia> print("Compose: "); (println ∘ uppercase)(msg)
Compose: HELLO!

julia> print("Compose: "); msg |> println ∘ uppercase
Compose: HELLO!

```

Perhaps something like this

```julia
julia> print("Compose: "); println newop uppercase newop msg
Compose: HELLO!

```

Which we can implement using Something like this

```julia
julia> msg = "Hello!"
"Hello!"

julia> ⋆(f1::Function,f2::Function) = f1 ∘ f2
⋆ (generic function with 1 method)

julia> ⋆(f::Function,a) = f(a)
⋆ (generic function with 2 methods)

julia> print("Compose: "); println ⋆ uppercase ⋆ msg
Compose: HELLO!

```

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [December 17, 2019, 2:07am UTC](https://discourse.julialang.org/t/perhaps-a-new-way-of-composing-functions/32366/2 "2019-12-17T02:07:23Z")

</div>

```julia
function comp(args...)
    a = args[end]
    len = length(args)-1
    for i = len:-1:1
        a = args[i](a)
    end
    return a
end

macro comp(args...)
    quote
        a = $args
        comp($(a...))
    end
end
msg ="using macros"
julia> res = print("Compose: "); @comp println uppercase msg
Compose: USING MACROS

```

a good thing in julia is that the syntax is easy to accomodate to your preferences. Frankly, i dont see this as a future addition to standard Julia, but can be well received in a package

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [December 17, 2019, 10:20am UTC](https://discourse.julialang.org/t/perhaps-a-new-way-of-composing-functions/32366/3 "2019-12-17T10:20:16Z")

</div>

Here’s a cleaner version of your `⋆` operator. Instead of behaving differently for things `::Function` you can simply choose an operator which associates the other way:

```julia
julia> :(1 ⋆ 2 ⋆ 3)
:((1 ⋆ 2) ⋆ 3)

julia> :(1 ↑ 2 ↑ 3)
:(1 ↑ (2 ↑ 3))

julia> ↑(x,y) = x(y)
↑ (generic function with 1 method)

julia> println ↑ uppercase ↑ msg
HELLO!

```
