# Can FunctionWrappers.jl express higher order functions?

**URL:** https://discourse.julialang.org/t/can-functionwrappers-jl-express-higher-order-functions/66404
**Category:** General Usage
**Tags:** type, functions
**Created:** [August 14, 2021, 9:03pm UTC](https://discourse.julialang.org/t/can-functionwrappers-jl-express-higher-order-functions/66404 "2021-08-14T21:03:03Z")
**Posts on this page:** 1
**Showing post:** 4

<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: [August 14, 2021, 9:56pm UTC](https://discourse.julialang.org/t/can-functionwrappers-jl-express-higher-order-functions/66404/4 "2021-08-14T21:56:10Z")

</div>

By the way, this overly verbose syntax is a great excuse to write a macro:

```julia
using MacroTools
using FunctionWrappers: FunctionWrapper

wrapperize(x) = esc(x)

function wrapperize(expr::Expr)
	if expr.head == :block
		return Expr(:block, wrapperize.(expr.args)...)
	elseif expr.head == :tuple
		return Expr(:tuple, wrapperize.(expr.args)...)
	elseif @capture(expr, (inputs__,) -> output_)
		return :(FunctionWrapper{$(wrapperize(output)), Tuple{$(wrapperize.(inputs)...)}})
	elseif @capture(expr, (input_) -> output_)
		return :(FunctionWrapper{$(wrapperize(output)), Tuple{$(wrapperize(input))}})
	else
		error("I can only handle expressions of the form `(inputs...) -> output`")
	end
end

macro fn(expr)
	wrapperize(expr)
end

```

Examples:

```julia
julia> @fn Int -> Int
FunctionWrapper{Int64, Tuple{Int64}}

julia> @fn (Int, Float64) -> Int
FunctionWrapper{Int64, Tuple{Int64, Float64}}

julia> @fn (Float64, Int -> Int) -> String
FunctionWrapper{String, Tuple{Float64, FunctionWrapper{Int64, Tuple{Int64}}}}

```

---

_[View the full topic](https://discourse.julialang.org/t/can-functionwrappers-jl-express-higher-order-functions/66404)._
