Skip to content

add fcollect #13

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

Merged
merged 10 commits into from
Feb 27, 2021
Merged

add fcollect #13

merged 10 commits into from
Feb 27, 2021

Conversation

CarloLucibello
Copy link
Member

Follow up to the discussion in FluxML/Flux.jl#1444.

I tried to give maximum flexibility to the interface.

Notice that it makes sense to pass a 2-args function f(v, vs) instead of a 1-arg f(v), this is because applying a 1-arg f is quite simple a-posteriori

f.(fcollect(m))

Possible applications:

  • Retrieve leaves from a model:
filter(isleaf, fcollect(m))
  • Retrieve all nodes except leaves
filter(!isleaf, fcollect(m))
# or
fcollect(m; recur = (v, vs) -> !isleaf(v))
  • Retrieve parents with all leaf children
a = fcollect(m; f = (v, vs) -> all(isleaf, vs) ? v : nothing)
filter(!isnothing, a)

Copy link
Member

@darsnack darsnack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the two-arg version is because we don't have something like children(x) = functor(x)[1] in Functors.jl. I would prefer syntax like:

a = fcollect(m; f = x -> all(isleaf, children(x)) ? x : nothing)
filter(!isnothing, a)

Co-authored-by: Kyle Daruwalla <daruwalla.k.public@icloud.com>
@CarloLucibello
Copy link
Member Author

CarloLucibello commented Feb 23, 2021

I feel like the two-arg version is because we don't have something like children(x) = functor(x)[1] in Functors.jl. I would prefer syntax like:

a = fcollect(m; f = x -> all(isleaf, children(x)) ? x : nothing)
filter(!isnothing, a)

Yeah. I think we can remove the f argument altogether and let people handle further processing downstream.

filter(x -> all(isleaf, children(x)), fcollect(m))

What do you say?

@darsnack
Copy link
Member

Yeah I think that would match the name fcollect better. No reason to include f just cause fmap has it (which was my original thinking).

@darsnack
Copy link
Member

We'll want to settle on the same name for recurse vs. predicate. I think I am okay with recurse.

@CarloLucibello
Copy link
Member Author

We'll want to settle on the same name for recurse vs. predicate. I think I am okay with recurse.

I'd go with recurse, conveys more intuition, the term predicate is very generic. Although the semantic is opposite, recurse == !predicate.

Copy link
Member

@darsnack darsnack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good to me. Will probably want @ToucheSir's review since he has more wisdom on Functor-y things than me.

CarloLucibello and others added 2 commits February 23, 2021 18:05
Co-authored-by: Kyle Daruwalla <daruwalla.k.public@icloud.com>
@darsnack
Copy link
Member

exclude is good. Will use that instead of predicate.

@CarloLucibello
Copy link
Member Author

I changed my mind, i think it is more convenient to specify what to exclude from the recursion instead of what to include, so removed recurse in favor of exclude.

Copy link
Member

@ToucheSir ToucheSir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, this is essentially Flux.destructure without trying to shove everything into one vector. One thing we could look at next is creating an iterable version. Then fcollect could be expressed simply as collect(fiter(m, ...)).

@CarloLucibello
Copy link
Member Author

Does anyone have merge rights?

@darsnack
Copy link
Member

I don't think so

@CarloLucibello
Copy link
Member Author

we good?

@DhairyaLGandhi DhairyaLGandhi merged commit 0b90595 into FluxML:master Feb 27, 2021
@ToucheSir ToucheSir mentioned this pull request Jan 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants