-
-
Notifications
You must be signed in to change notification settings - Fork 14.5k
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.fileset.intersection
: init
#257356
lib.fileset.intersection
: init
#257356
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ let | |
_toSourceFilter | ||
_unionMany | ||
_printFileset | ||
_intersection | ||
; | ||
|
||
inherit (builtins) | ||
|
@@ -18,6 +19,7 @@ let | |
; | ||
|
||
inherit (lib.lists) | ||
elemAt | ||
imap0 | ||
; | ||
|
||
|
@@ -276,6 +278,45 @@ If a directory does not recursively contain any file, it is omitted from the sto | |
_unionMany | ||
]; | ||
|
||
/* | ||
The file set containing all files that are in both of two given file sets. | ||
See also [Intersection (set theory)](https://en.wikipedia.org/wiki/Intersection_(set_theory)). | ||
|
||
The given file sets are evaluated as lazily as possible, | ||
with the first argument being evaluated first if needed. | ||
|
||
Type: | ||
intersection :: FileSet -> FileSet -> FileSet | ||
|
||
Example: | ||
# Limit the selected files to the ones in ./., so only ./src and ./Makefile | ||
intersection ./. (unions [ ../LICENSE ./src ./Makefile ]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a more realistic example that readily applies in many projects: let buildFiles = fileFilter (f: f.ext != "nix") ../.;
in intersection buildFiles (unions [ ../build-support ./src ./Makefile ]) Not sure if I got the filter right. Had to change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initially I wrote the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I can add this example in the PR once this one is merged |
||
*/ | ||
intersection = | ||
# The first file set. | ||
# This argument can also be a path, | ||
# which gets [implicitly coerced to a file set](#sec-fileset-path-coercion). | ||
fileset1: | ||
# The second file set. | ||
# This argument can also be a path, | ||
# which gets [implicitly coerced to a file set](#sec-fileset-path-coercion). | ||
fileset2: | ||
let | ||
filesets = _coerceMany "lib.fileset.intersection" [ | ||
{ | ||
context = "first argument"; | ||
value = fileset1; | ||
} | ||
{ | ||
context = "second argument"; | ||
value = fileset2; | ||
} | ||
]; | ||
in | ||
_intersection | ||
(elemAt filesets 0) | ||
(elemAt filesets 1); | ||
|
||
/* | ||
Incrementally evaluate and trace a file set in a pretty way. | ||
This function is only intended for debugging purposes. | ||
|
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.
@roberth @fricklerhandwerk This PR isn't done yet, but I wonder what you think of this design decision.
It's an interesting duality:
We might really only need
unions
(taking a list) andintersection
(binary operation).union
(binary operation) doesn't seem that important, but I can imagine some situations where it could be useful and we have it already.Notably Haskell's
Data.Set
also providesunion
,unions
,intersection
but nointersections
(there is one, but it's inData.Set.Internal
).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.
We could have an interface that requires at least one set:
intersect :: a -> [a] -> a
. But probably a binary one is good enough. One can still fold over a list, then defining the base set will be required for sane results. That again we can add to documentation as an 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.
In math it's easy to invoke a somewhat implicitly defined "universe" set. Us finite mortals would have to make that explicit, which is another way to land on
a -> List a -> a
whereas @fricklerhandwerk seems to have arrived throughNonEmptyList a -> a
.