# Writing complex Flux Models

**URL:** <https://discourse.julialang.org/t/writing-complex-flux-models/40439>\
**Category:** Machine Learning\
**Created:** [May 29, 2020, 8:06pm UTC](https://discourse.julialang.org/t/writing-complex-flux-models/40439 "2020-05-29T20:06:42Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [June 2, 2020, 6:57pm UTC](https://discourse.julialang.org/t/writing-complex-flux-models/40439/4 "2020-06-02T18:57:08Z")

</div>

I think I’ve seen somewhere that closures being anonymous structs is an implementation detail which it is not recommended to depend on. Of course, if it is a single purpose project then by all means, just go ahead and implement that functor in there and use it while it works.

```julia
# Too lazy to load Flux, I think something like this might work except closurefunctor shall be Flux.functor
julia> function test(a, b)
       return function(x)
          return a .+ b .* x
       end
       end
test (generic function with 2 methods)

julia> tt = test(1, [2,3]);

julia> fieldnames(typeof(tt))
(:a, :b)

julia> closurefunctor(x) = map(fn -> getfield(x, fn), fieldnames(typeof(x))), test
closurefunctor (generic function with 1 method)

julia> p,re = closurefunctor(tt);

julia> p
(1, [2, 3])

julia> re(4, [1,2,3])
#21 (generic function with 1 method)

julia> ttt = re(4, [1,2,3])
#21 (generic function with 1 method)

julia> ttt(3)
3-element Array{Int64,1}:
  7
 10
 13

```

**Shameless plug** : This package supports a static computation graph format designed to be created and manipulated programatically: [GitHub - DrChainsaw/NaiveNASflux.jl: Your local Flux surgeon](https://github.com/DrChainsaw/NaiveNASflux.jl)

---

_[View the full topic](https://discourse.julialang.org/t/writing-complex-flux-models/40439)._
