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

Add collections.Complement #5400

Closed
bep opened this issue Nov 4, 2018 · 12 comments · Fixed by #5407
Closed

Add collections.Complement #5400

bep opened this issue Nov 4, 2018 · 12 comments · Fixed by #5407
Assignees
Milestone

Comments

@bep
Copy link
Member

bep commented Nov 4, 2018

http://www.wolframalpha.com/input/?i=wolfram+language+symbol+Complement&lk=1&assumption=%22ClashPrefs%22+-%3E+%7B%22WolframLanguageSymbol%22,+%22Complement%22%7D

  • We can have an alias complement
  • It should take a number of slices as input so I can do $pages = $pages | complement $pages1 $pages2 etc.
  • The implementation can assume that all elements in these slices are comparable/hashable types
@bep bep added the Enhancement label Nov 4, 2018
@bep bep added this to the v0.51 milestone Nov 4, 2018
@bep bep self-assigned this Nov 5, 2018
bep added a commit to bep/hugo that referenced this issue Nov 5, 2018
bep added a commit to bep/hugo that referenced this issue Nov 5, 2018
bep added a commit to bep/hugo that referenced this issue Nov 5, 2018
@kaushalmodi
Copy link
Contributor

kaushalmodi commented Nov 5, 2018

$pages = $pages | complement $pages1 $pages2

What does that do? $pages will contain elements from $pages that are neither in $pages1 nor in $pages2?

Complement of a set A is all elements that are not in set A, but are in the universe set U.

So if U = {1, 2, 3} and A = {2}, complement of A A' = {1, 3}.

I hope that the hugo implementation of complement does the same (but that example makes me think that it's not).

A complement needs just two arguments.. (1) the set being complemented (A), and (2) the universe set (U). But your example shows that complement has 3 args!

Further reading: https://en.wikipedia.org/wiki/Complement_(set_theory)


Update: Fix typo: union -> universe

@bep
Copy link
Member Author

bep commented Nov 5, 2018

A complement needs just two arguments.

A complement function needs at least one argument to make some sense. But the signature and definition for "Hugo's complement" matches Wolfram's.

@kaushalmodi
Copy link
Contributor

kaushalmodi commented Nov 5, 2018

A complement function needs at least one argument to make some sense.

You meant at least two arguments, right?

From Wolfram's complement signature, eall is the U set I mentioned above, and e1 is the A set in my example.

So what I asked above:

$pages = $pages | complement $pages1 $pages2

$pages will contain elements from $pages that are neither in $pages1 nor in $pages2?

holds true?

May be for clarity, you write the example code as:

$pages_not_in_e1_and_not_in_e2 = $pages_eall | complement $pages_e1 $pages_e2

Correct?


To match with Wolfram definition of Complement: https://reference.wolfram.com/language/ref/Complement.html

Complement[eall,e1,e2,…]

gives the elements in eall that are not in any of the ei.

@bep
Copy link
Member Author

bep commented Nov 5, 2018

You meant at least two arguments, right?

No, one (1). A slice that complements nothing would be unchanged.

If we instead of CS set theory talk in practical examples:

  • The use case that brought us here was: Show 4 posts at the top and the rest some other place on the page, e.g. $first4 := .Pages | first 4 and $other := .Pages | complement $first4
  • But this could also be a valid use case: $first4 := .Pages | first 4,$last4 := .Pages | last 4 and $other := .Pages | complement $first4 $last4

Which also matches my sense of language: "give me the pages that complement the first 4 and the last 4".

bep added a commit to bep/hugo that referenced this issue Nov 5, 2018
@kaushalmodi
Copy link
Contributor

kaushalmodi commented Nov 5, 2018

No, one (1). A slice that complements nothing would be unchanged.

What's the use case of that? Why not require two arguments at the minimum?

A "complement" is always with reference to something. So we need the thing being complemented, and the reference against which we are complementing that.

Allowing only one arg to complement doesn't make sense to me.

@bep
Copy link
Member Author

bep commented Nov 5, 2018

What's the use case of that? Why not require two arguments at the minimum?

I assume that you agree that:

{{ slice "a" "b" | complement slice }}

Should result in "a" and "b".

But what about:

{{ slice "a" "b" | complement giveMeSomeSlices }}

If giveMeSomeSlices returns nil (as in: We found no slices), what would your wanted behaviour be? I guess the above is two arguments, but not 2 slices.

bep added a commit to bep/hugo that referenced this issue Nov 5, 2018
@bep
Copy link
Member Author

bep commented Nov 5, 2018

I have adjusted it to fail on < 2 arguments, but it still makes sense to "complement nothing".

@kaushalmodi
Copy link
Contributor

{{ slice "a" "b" | complement giveMeSomeSlices }}

I would interpret as:

  • eall = '{"a", "b"}
  • e1 = '{} (empty set)

So complement(eall, e1) = '{"a", "b"}.

So the question is a Go-lang question.. is passing a nil (empty Go slice) argument the same as passing no argument? If so, I can understand why you put the requirement of at least 1 arg.

@bep
Copy link
Member Author

bep commented Nov 5, 2018

is passing a nil (empty Go slice) argument the same as passing no argument?

No. Nil are also values, so you are correct in the "2 argument" argument, but I have been bitten by the "being too restrictive" in the validations we do in that template package.

@kaushalmodi
Copy link
Contributor

kaushalmodi commented Nov 5, 2018

but I have been bitten by the "being too restrictive"

This is not being restrictive.. it's just the definition of "complement". Something can be complemented always with respect to some reference. There has to be always some reference.

So your recent change (bep@20d85f9#diff-7abc0a71d95894310bb43c55be54d0e0R26) looks good.

I don't care about the internal implementation. My earlier suggestion to break up the Complement args was just to get my point across that we need a Universe (U) set and then we need at aleast one other set that needs to be complemented out of that Universe.

Thanks.


Update: Fix typo: union -> universe

@kaushalmodi
Copy link
Contributor

All along I was saying "union", but I meant universe Ref: https://en.wikipedia.org/wiki/Complement_(set_theory)#Definition

.. if U is the universe that contains all the elements under study

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants