-
Notifications
You must be signed in to change notification settings - Fork 23
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
Adds List.sample and List.sampleN #3398
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@quri/squiggle-lang": patch | ||
--- | ||
|
||
Added List.sample and List.sampleN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,3 +119,27 @@ export function isEqual<T>(arr1: readonly T[], arr2: readonly T[]): boolean { | |
|
||
return true; | ||
} | ||
|
||
export function sample<T>(array: readonly T[], rng: PRNG): T { | ||
const index = Math.floor(rng() * array.length); | ||
return array[index]; | ||
} | ||
|
||
export function sampleN<T>(array: readonly T[], n: number, rng: PRNG): T[] { | ||
const size = Math.max(0, Math.floor(n)); | ||
if (size === 0) return []; | ||
if (size >= array.length) return shuffle(array, rng); | ||
|
||
const result: T[] = []; | ||
const indices = new Set<number>(); | ||
|
||
while (indices.size < size) { | ||
const index = Math.floor(rng() * array.length); | ||
if (!indices.has(index)) { | ||
indices.add(index); | ||
result.push(array[index]); | ||
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. This doesn't look optimal, but I tested it on (asymptote is probably something like |
||
} | ||
} | ||
|
||
return result; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Sampling 10 items from the list with 3 items will return the list of 3 items, is this intentional?
I'd expect it to create duplicates, and that would be consistent with how Dist.sampleN works
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.
Ok, I see that this defaults to sampling without replacement on lower N values, so it makes sense to keep it consistent.
It's a pity that it's inconsistent with how
Dist.sampleN
works, though. I assume it was intentional, and also the reason why you didn't make un-namespacedsampleN
non-polymorphic?Seems like a potential footgun...
How about this: we rename the version with replacement to
pickN
, and makesampleN
consistent for lists and dists(with replacement)?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.
Good points. Agreed it's gnarly.
Another option is to add configs. Maybe something like,
sampleN(samples, {withReplacement: boolean, shuffle: boolean})
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.
Yeah, options could work, but if we want to combine this with polymorphism on dists vs lists (which seems natural and good), and have different replacement defaults, then it's still problematic.