Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
CarloLucibello committed Oct 21, 2024
1 parent 6285507 commit c43f2ea
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
8 changes: 4 additions & 4 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ Using [`@leaf`](@ref) instead of [`@functor`](@ref) will prevent the fields of a
By default all composite types in are functors and can be traversed, unless marked with [`@leaf`](@ref).

The following types instead are explicitly marked as leaves in Functors.jl:
- `Number`
- `AbstractArray{<:Number}`
- `AbstractString`
- `Number`.
- `AbstractArray{<:Number}`, except for the wrappers `Transpose`, `Adjoint`, and `PermutedDimsArray`.
- `AbstractString`.

This is because in typical application the internals of these are abstracted away and it is not desirable to traverse them.

## What if I get an error?

Since by default Funcotrs.jl tries to traverse most types e.g. when using [`fmap`](@ref), it is possible it fails in case the type has not an appropriate constructor. If use experience this issue, you have a few alternatives:
Since by default Functors.jl tries to traverse most types e.g. when using [`fmap`](@ref), it is possible it fails in case the type has not an appropriate constructor. If use experience this issue, you have a few alternatives:
- Mark the type as a leaf using [`@leaf`](@ref)
- Use the `@functor` macro to specify which fields to traverse.
- Define an appropriate constructor for the type.
Expand Down
2 changes: 1 addition & 1 deletion src/Functors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ include("base.jl")
functor(typeof(x), x)
Returns a tuple containing, first, a `NamedTuple` of the children of `x`
(typically its fields), and second, a reconstruction funciton.
(typically its fields), and second, a reconstruction function.
This controls the behaviour of [`fmap`](@ref).
Methods should be added to `functor(::Type{T}, x)` for custom types,
Expand Down
2 changes: 1 addition & 1 deletion src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function functor(::Type{<:PermutedDimsArray{T,N,perm,iperm}}, x) where {T,N,perm
(parent = _PermutedDimsArray(x, iperm),), y -> PermutedDimsArray(only(y), perm)
end
function functor(::Type{<:PermutedDimsArray{T,N,perm,iperm}}, x::PermutedDimsArray{Tx,N,perm,iperm}) where {T,Tx,N,perm,iperm}
(parent = parent(x),), y -> PermutedDimsArray(only(y), perm) # most common case, avoid wrapping wrice.
(parent = parent(x),), y -> PermutedDimsArray(only(y), perm) # most common case, avoid wrapping twice.
end

_PermutedDimsArray(x, iperm) = PermutedDimsArray(x, iperm)
Expand Down
3 changes: 2 additions & 1 deletion src/walks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ function check_lenghts(x, ys...)
end

_map(f, x::Dict, ys...) = Dict(k => f(v, (y[k] for y in ys)...) for (k, v) in x)
_map(f, x::D, ys...) where {D<:AbstractDict} = constructorof(D)(k => f(v, (y[k] for y in ys)...) for (k, v) in x)
_map(f, x::D, ys...) where {D<:AbstractDict} =
constructorof(D)(k => f(v, (y[k] for y in ys)...) for (k, v) in x)

_values(x) = x
_values(x::AbstractDict) = values(x)
Expand Down

0 comments on commit c43f2ea

Please sign in to comment.