-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
fmapstructure #14
fmapstructure #14
Conversation
Nice, this is basically just traversing the tree, right? We could technically replace the construction with an Also, we would miss out on non-functor-able objects in this view which is a shame. |
Pretty much, I think we'd some kind of kwarg and an additional conditional for fmap, because Non-functorable objects are handled exactly the same way as they are in |
Is I don't think complicating |
If anything I feel like |
Not really,
Come to think of it, we could solve this just by having |
Yeah but all the example transforms are just |
|
Yeah switching the PR to |
Maybe add something in the README? Also, I think this can be lifted out of WIP; it's pretty much good to go 😄. |
Also should we rename |
I had to add a better test after finding a bug in the first version ( As for what to name |
Well I came up with |
Also, |
After sleeping on it, my vote is for |
Thoughts on leading vs no leading underscore? I couldn't find any reference to |
|
Done. Shall we get this merged so that Flux can start using it? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a couple docstring suggestions, but it looks good otherwise.
src/functor.jl
Outdated
|
||
Like [`fmap`](@ref), but doesn't preserve the type of custom structs. | ||
|
||
Useful for when the output must be plain-old julia data structures. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we omit this? I'm not sure it's the right wording, since functored structs are still "plain-old Julia data structures."
@@ -13,7 +13,7 @@ function makefunctor(m::Module, T, fs = fieldnames(T)) | |||
f in fs ? :(y[$(yᵢ += 1)]) : :(x.$f) | |||
end | |||
escfs = [:($f=x.$f) for f in fs] | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of no-op line changes. Do you know what that's from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, my editor may have trimmed existing trailing whitespace automatically on save?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah, so we'll keep these
Co-authored-by: Kyle Daruwalla <[email protected]>
@@ -13,7 +13,7 @@ function makefunctor(m::Module, T, fs = fieldnames(T)) | |||
f in fs ? :(y[$(yᵢ += 1)]) : :(x.$f) | |||
end | |||
escfs = [:($f=x.$f) for f in fs] | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah, so we'll keep these
src/functor.jl
Outdated
# Examples | ||
```jldoctest | ||
julia> struct Foo; x; y; end | ||
|
||
julia> @functor Foo | ||
|
||
julia> struct Bar; x; end | ||
|
||
julia> @functor Bar | ||
|
||
julia> m = Foo(Bar([1,2,3]), (4, 5)); | ||
|
||
julia> fmap(x -> 2x, m) | ||
Foo(Bar([2, 4, 6]), (8, 10)) | ||
|
||
julia> fmap(string, m) | ||
Foo(Bar("[1, 2, 3]"), ("4", "5")) | ||
|
||
julia> fmap(string, m, exclude = v -> v isa Bar) | ||
Foo("Bar([1, 2, 3])", (4, 5)) | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want a custom walk example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't think of a succinct one that wasn't fmapstructure
, so I just added a doctest for that instead. Are there any examples you can think of?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just something that only walks the children that are arrays of arrays? Or some other simple type constrained walk.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put in an absolutely uninspired example, but it's an example nonetheless. Happy to change it if something more illustrative comes up.
@CarloLucibello I don't have permissions on this branch. Or do we use |
Thanks all |
The name |
I agree the name isn't ideal, but the intent was to point out that this only preserves the structure (for some handwavy definition of "structure") and not the actual types involved. While Another way to look at this is as an instance of a poor man's hylomorphism. This take is appealing because it lets you view |
Actually, perhaps a more direct analogy would be that |
Example usage:
Note how the implementation is almost exactly the same as
fmap
, just without there
(construction). Ideally this functionality could be folded intofmap
itself, but I'm not sure what the most elegant way to conditionally control reconstruct is.cc @DhairyaLGandhi @darsnack