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> print("Compose: "); println newop uppercase newop msg
Compose: HELLO!
Which we can implement using Something like this
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!
1 Like
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
4 Likes
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> :(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!
5 Likes