Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

functor by default #51

Merged
merged 24 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docs
  • Loading branch information
CarloLucibello committed Oct 21, 2024
commit 88ffc38380a11fe0e709a43631092dfb99a83bc3
6 changes: 4 additions & 2 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ For large machine learning models it can be cumbersome or inefficient to work wi

## Basic Usage and Implementation

By default, julia types are marked as [`@functor`](@ref)s, meaning that Functors.jl is allowed to look into the fields of the instances of the struct and modify them. This is achieved through [`fmap`](@ref).
By default, julia types are marked as [`@functor`](@ref Functors.functor)s, meaning that Functors.jl is allowed to look into the fields of the instances of the struct and modify them. This is achieved through [`fmap`](@ref). To opt-out of this behaviour, use [`@leaf`](@ref) on your custom type.

The workhorse of `fmap` is actually a lower level function, [`functor`](@ref):
```julia-repl

The workhorse of `fmap` is actually a lower level function, [`functor`](@ref Functors.functor):

```julia-repl
julia> using Functors
Expand Down
14 changes: 7 additions & 7 deletions src/Functors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ functor
@functor T
@functor T (x,)

Adds methods to [`functor`](@ref) allowing recursion into objects of type `T`,
Adds methods to [`functor`](@ref Functors.functor) allowing recursion into objects of type `T`,
and reconstruction. Assumes that `T` has a constructor accepting all of its fields,
which is true unless you have provided an inner constructor which does not.

Expand Down Expand Up @@ -80,7 +80,7 @@ var"@functor"
"""
isleaf(x)

Return true if `x` has no [`children`](@ref) according to [`functor`](@ref).
Return true if `x` has no [`children`](@ref Functors.children) according to [`functor`](@ref Functors.functor).

# Examples
```jldoctest
Expand Down Expand Up @@ -108,7 +108,7 @@ isleaf
"""
children(x)

Return the children of `x` as defined by [`functor`](@ref).
Return the children of `x` as defined by [`functor`](@ref Functors.functor).
Equivalent to `functor(x)[1]`.
"""
children
Expand All @@ -118,8 +118,8 @@ children

A structure and type preserving `map`.

By default it transforms every leaf node (identified by `exclude`, default [`isleaf`](@ref))
by applying `f`, and otherwise traverses `x` recursively using [`functor`](@ref).
By default it transforms every leaf node (identified by `exclude`, default [`isleaf`](@ref Functors.isleaf))
by applying `f`, and otherwise traverses `x` recursively using [`functor`](@ref Functors.functor).
Optionally, it may also be associated with objects `ys` with the same tree structure.
In that case, `f` is applied to the corresponding leaf nodes in `x` and `ys`.

Expand Down Expand Up @@ -264,7 +264,7 @@ fmapstructure
"""
fcollect(x; exclude = v -> false)

Traverse `x` by recursing each child of `x` as defined by [`functor`](@ref)
Traverse `x` by recursing each child of `x` as defined by [`functor`](@ref Functors.functor)
and collecting the results into a flat array, ordered by a breadth-first
traversal of `x`, respecting the iteration order of `children` calls.

Expand Down Expand Up @@ -355,7 +355,7 @@ fmapstructure_with_path
"""
fleaves(x; exclude = isleaf)

Traverse `x` by recursing each child of `x` as defined by [`functor`](@ref)
Traverse `x` by recursing each child of `x` as defined by [`functor`](@ref Functors.functor)
and collecting the leaves into a flat array,
ordered by a breadth-first traversal of `x`, respecting the iteration order of `children` calls.

Expand Down
2 changes: 1 addition & 1 deletion src/functor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const NoChildren = Tuple{}
"""
@leaf T

Define [`functor`](@ref) for the type `T` so that `isleaf(x::T) == true`.
Define [`functor`](@ref Functors.functor) for the type `T` so that `isleaf(x::T) == true`.
"""
macro leaf(T)
:($Functors.functor(::Type{<:$(esc(T))}, x) = ($Functors.NoChildren(), _ -> x))
Expand Down
Loading