# How to make a function factory

**URL:** <https://discourse.julialang.org/t/how-to-make-a-function-factory/24108>\
**Category:** New to Julia\
**Created:** [May 11, 2019, 10:04am UTC](https://discourse.julialang.org/t/how-to-make-a-function-factory/24108 "2019-05-11T10:04:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![robertdj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertdj/32/103_2.png) [@robertdj](https://discourse.julialang.org/u/robertdj)\
**Post date:** [May 11, 2019, 10:04am UTC](https://discourse.julialang.org/t/how-to-make-a-function-factory/24108/1 "2019-05-11T10:04:14Z")

</div>

I’m looking into rewriting parts of my VoronoiCells package. One thing I want to do is to have a type `Rectangle` and make functions that maps points between two Rectangles.

This attempt appears to work:

```julia
struct Rectangle
    Left::Float64
    Right::Float64
    Lower::Float64
    Upper::Float64
end

left(r::Rectangle) = r.Left
right(r::Rectangle) = r.Right
lower(r::Rectangle) = r.Lower
upper(r::Rectangle) = r.Upper

function construct_rectangle_map(from::Rectangle, to::Rectangle)
    function rect_map(p::GeometricalPredicates.Point2D)
        GeometricalPredicates.Point2D(
            left(to) + (getx(p) - left(from)) / (right(from) - left(from)) * (right(to) - left(to)),
            lower(to) + (gety(p) - lower(from)) / (upper(from) - lower(from)) * (upper(to) - lower(to))
        )
    end
end

```

The `getx` and `gety` are from the GeometricalPredicates package.

With `from = Rectangle(-1, 1, 0, 1)` and `to = VoronoiCells.Rectangle(1, 2, 1, 2)` I get a map `m = construct_rectangle_map(from, to)` that seems to work: `m(Point2D(0.3, 0.4))`

However, looking at `@code_lowered m(Point2D(0.3, 0.4))` it appears that the the `left`/`right` are called inside the function.  
I think it would be nice if the numbers are saved as the constants they are for a given map. Does anyone know how to do this?

Thanks!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 11, 2019, 1:38pm UTC](https://discourse.julialang.org/t/how-to-make-a-function-factory/24108/2 "2019-05-11T13:38:10Z")

</div>

`left` and `right` will get inlined to struct accesses when it is compiled, but not to constants. In principle, there are ways to inline them to constants, but trying to have a separate compiled function for large numbers of rectangle pairs is likely to do more harm than good.

I would normally suggest just having a `rect_map(from, to, p)` function that takes 3 arguments. In any context where you need a single-argument function you can do `p -> rect_map(from, to, p)` … you don’t need a “function factory” pattern for this because `construct_rectangle_map` is neither shorter nor more convenient than `p -> ...`. (At [some point in the future](https://github.com/JuliaLang/julia/pull/24990) we might even have the syntax `rect_map(from, to, _)`.)

In the unlikely event that the cost of `rect_map` is the performance-critical step in your code, you can do further optimizations, but I would wait until profiling makes that clear.

---

<div class="post-metadata">

**Author:** ![robertdj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertdj/32/103_2.png) [@robertdj](https://discourse.julialang.org/u/robertdj)\
**Post date:** [May 11, 2019, 8:38pm UTC](https://discourse.julialang.org/t/how-to-make-a-function-factory/24108/3 "2019-05-11T20:38:20Z")

</div>

Thanks! I think your suggestion with a 3 argument `rect_map` sounds like a good plan.
