Skip to content

Conversation

bep
Copy link
Member

@bep bep commented Aug 25, 2025

No description provided.

@jmooring
Copy link
Member

I had a couple of thoughts, neither of which are terribly important, while creating the documentation:

https://deploy-preview-3184--gohugoio.netlify.app/functions/collections/d/

First, with this signature...

collections.D SEED N MAX

...it seems like the maximum returned should be MAX but it's is actually MAX - 1. You've covered that in the half-open interval description, but I'm wondering if we should just add 1 before calling vitter.D.

Second, we're casting all args to ints, but the cache key is based on the raw value, which might not be an int. So the same result could be cached more than once, but I don't think this is very likely to happen in the wild.

@bep
Copy link
Member Author

bep commented Aug 25, 2025

...it seems like the maximum returned should be MAX but it's is actually MAX - 1. You've covered that in the half-open interval description, but I'm wondering if we should just add 1 before calling vitter.D.

In my head, the current API would allow for constructs ala:

{{ $pages := site.RegularPages }}
{{ range (collections.D 42 10 ( $pages | len )) }}
   {{ with (index $pages .) }}
   {{ end }}
{{ end }}

The above should be a very effective way to pick 10 random pages without any (potentiallly expensive) shuffling. If we added 1 to MAX, the above would not work (or the user would have to do some subtraction).

So the same result could be cached more than once, but I don't think this is very likely to happen in the wild.

I'm not sure I care too much about that, but I guess it wold look cleaner with ints as the key.

@jmooring
Copy link
Member

You're right. The current API works great with a page collection, which I expect will be the predominant use case. I'll update the docs with another example.

bep added 2 commits August 25, 2025 23:47
hi is commonly used as the exclusive ending index in Go slice notations.
@bep bep force-pushed the feat/vitter branch 2 times, most recently from 7fccc4a to 717464f Compare August 26, 2025 08:51
@bep bep merged commit 1ba8087 into gohugoio:master Aug 26, 2025
12 of 30 checks passed
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.

2 participants