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

lib/attrsets: add mapCartesianProduct function #298680

Merged
merged 5 commits into from Apr 19, 2024

Conversation

gvolpe
Copy link
Member

@gvolpe gvolpe commented Mar 24, 2024

Description of changes

The cartesianProductOfSets function was added some time ago (aka transpose or sequence in other functional languages), while deprecating the crossLists function. I have come across the deprecation message "use cartesianProductOfSets instead" without further details, which motivated me to improve the situation by adding a new convenient function. Example:

λ lib.mapCartesianProduct ({a,b}: a+b) {a=[1 2]; b=[3 4];}
[ 4 5 5 6 ]

It's the equivalent of using the former followed by map, which seems to be quite a common pattern to justify a single function:

λ builtins.map ({a,b}: a+b) (lib.cartesianProductOfSets {a=[1 2]; b=[3 4];})
[ 4 5 5 6 ]

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.05 Release Notes (or backporting 23.05 and 23.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/lib-crosslists-is-deprecated-use-lib-cartesianproductofsets-instead/41824/4

@infinisil
Copy link
Member

infinisil commented Mar 25, 2024

aka transpose or sequence in other functional languages

I don't think I've ever seen this operation be called that? Transposing usually means diagonally mirroring a Matrix.

cartesianProductOfSets is a bit too long imo, I think we could shorten it to cartesianProduct though, otherwise it seems like a fitting name.

And then I think mapCartesianProduct would be best because it's the same as map f . cartesianProduct a.

@gvolpe
Copy link
Member Author

gvolpe commented Mar 25, 2024

I don't think I've ever seen this operation be called that? Transposing usually means diagonally mirroring a Matrix.

Oh I got the transpose function completely wrong, you're right it doesn't apply to this use case 😅

Though, sequence is what it's called in functional languages like Haskell and Scala, e.g.

λ sequence [[1,2,3],[4,5,6]]
[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]

It is more generic than this, but it results in the Cartesian product of every item when dealing with lists of lists.

cartesianProductOfSets is a bit too long imo, I think we could shorten it to cartesianProduct though, otherwise it seems like a fitting name.

I agree. This would mean deprecating the existing cartesianProductOfSets for this new one, right?

And then I thin mapCartesianProduct would be best because it's the same as map f . cartesianProduct a.

Definitely makes sense, would be happy to adjust to the suggested names, thanks for reviewing!

@gvolpe gvolpe changed the title lib/attrsets: add transposeMap function lib/attrsets: add mapCartesianProduct function Mar 25, 2024
@infinisil
Copy link
Member

Though, sequence is what it's called in functional languages like Haskell and Scala, e.g.

λ sequence [[1,2,3],[4,5,6]]
[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]

It is more generic than this, but it results in the Cartesian product of every item when dealing with lists of lists.

I'd say that's half of a coincidence, because it relies on the Applicative instance (see the more generic sequenceA) for lists to be the cartesian product, but there's also another Applicative implementation on lists that would make sense :D

I agree. This would mean deprecating the existing cartesianProductOfSets for this new one, right?

Yeah I guess that would be best.

Copy link
Member

@infinisil infinisil left a comment

Choose a reason for hiding this comment

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

Please also separate each change into a separate commit, e.g.:

  • Renaming cartesianProductOfSets
  • Tree-wide replacement
  • Introducing mapCartesianProduct and introducing its tests
  • Tree-wide replacement
  • Applying lints

lib/attrsets.nix Outdated Show resolved Hide resolved
lib/lists.nix Outdated Show resolved Hide resolved
@gvolpe
Copy link
Member Author

gvolpe commented Mar 25, 2024

Should've looked at your profile before, we could discuss ZipLists another day 😄

Applying lints

Can you elaborate on what this means? I guess it's running some kind of formatter, but I'm not familiar with the current process.

@gvolpe
Copy link
Member Author

gvolpe commented Apr 15, 2024

@infinisil could you have a look at the remaining open questions when you get a chance? Thanks

Copy link
Member

@infinisil infinisil left a comment

Choose a reason for hiding this comment

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

Other than the commits needing cleanup this looks good to me!

@gvolpe
Copy link
Member Author

gvolpe commented Apr 15, 2024

Other than the commits needing cleanup this looks good to me!

@infinisil done ✅

@Mindavi
Copy link
Contributor

Mindavi commented Apr 17, 2024

LGTM except for the 'apply lints' commit that doesn't seem very related

@gvolpe
Copy link
Member Author

gvolpe commented Apr 17, 2024

LGTM except for the 'apply lints' commit that doesn't seem very related

It is not related indeed, but I had to make changes on that file and noticed a lot of things that can be removed. I don't think anybody would care to submit a PR to remove unused bindings to be honest, but happy to adjust the commit message if there are better suggestions.

@Mindavi Mindavi merged commit e00a40a into NixOS:master Apr 19, 2024
22 checks passed
@Mindavi
Copy link
Contributor

Mindavi commented Apr 19, 2024

I think it's ok on second thought, thanks!

@gvolpe gvolpe deleted the lib/transposeMap branch April 19, 2024 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants