|
2 | 2 |
|
3 | 3 | Functors.jl provides a set of tools to represent [functors](https://en.wikipedia.org/wiki/Functor_(functional_programming)). Functors are a powerful means to apply functions to generic objects without changing their structure.
|
4 | 4 |
|
5 |
| -Functors can be used in a variety of ways. One is to traverse a complicated or nested structure as a tree and apply a function `f` to every field it encounters along the way. |
| 5 | +The most straightforward use is to traverse a complicated nested structure as a tree, and apply a function `f` to every field it encounters along the way. |
6 | 6 |
|
7 | 7 | For large models it can be cumbersome or inefficient to work with parameters as one big, flat vector, and structs help manage complexity; but it may be desirable to easily operate over all parameters at once, e.g. for changing precision or applying an optimiser update step.
|
8 | 8 |
|
9 |
| -## Appropriate Use |
10 |
| - |
11 |
| -!!! warning "Not everything should be a functor!" |
12 |
| - Due to its generic nature it is very attractive to mark several structures as [`@functor`](@ref) when it may not be quite safe to do so. |
13 |
| - |
14 |
| -Typically, since any function `f` is applied to the leaves of the tree, but it is possible for some functions to require dispatching on the specific type of the fields causing some methods to be missed entirely. |
15 |
| - |
16 |
| -Examples of this include element types of arrays which typically have their own mathematical operations defined. Adding a [`@functor`](@ref) to such a type would end up missing methods such as `+(::MyElementType, ::MyElementType)`. Think `RGB` from Colors.jl. |
17 |
| - |
18 | 9 | ## Basic Usage and Implementation
|
19 | 10 |
|
20 | 11 | When one marks a structure as [`@functor`](@ref) it means that Functors.jl is allowed to look into the fields of the instances of the struct and modify them. This is achieved through [`Functors.fmap`](@ref).
|
@@ -61,3 +52,11 @@ Baz(1.0, 2)
|
61 | 52 |
|
62 | 53 | Any field not in the list will be passed through as-is during reconstruction. This is done by invoking the default constructor, so structs that define custom inner constructors are expected to provide one that acts like the default.
|
63 | 54 |
|
| 55 | +## Appropriate Use |
| 56 | + |
| 57 | +!!! warning "Not everything should be a functor!" |
| 58 | + Due to its generic nature it is very attractive to mark several structures as [`@functor`](@ref) when it may not be quite safe to do so. |
| 59 | + |
| 60 | +Typically, since any function `f` is applied to the leaves of the tree, but it is possible for some functions to require dispatching on the specific type of the fields causing some methods to be missed entirely. |
| 61 | + |
| 62 | +Examples of this include element types of arrays which typically have their own mathematical operations defined. Adding a [`@functor`](@ref) to such a type would end up missing methods such as `+(::MyElementType, ::MyElementType)`. Think `RGB` from Colors.jl. |
0 commit comments