Skip to content

Commit 477b10b

Browse files
giacomocavalierilpil
authored andcommitted
Add labels to sample
This makes it consistent with other "similar" functions like take and drop. The second label also makes it clearer that it might return less items than expected.
1 parent ee9f68e commit 477b10b

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/gleam/list.gleam

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,23 +2313,22 @@ fn max_loop(list, compare, max) {
23132313
/// // -> [2, 4, 5] // A random sample of 3 items
23142314
/// ```
23152315
///
2316-
pub fn sample(list: List(a), k: Int) -> List(a) {
2317-
case k <= 0 {
2316+
pub fn sample(from list: List(a), up_to n: Int) -> List(a) {
2317+
case n <= 0 {
23182318
True -> []
23192319
False -> {
2320-
let #(reservoir, list) = split(list, k)
2320+
let #(reservoir, list) = split(list, n)
23212321

2322-
case length(reservoir) < k {
2322+
case length(reservoir) < n {
23232323
True -> reservoir
23242324
False -> {
23252325
let reservoir =
23262326
reservoir
2327-
|> map2(range(0, k - 1), _, fn(a, b) { #(a, b) })
2327+
|> map2(range(0, n - 1), _, fn(a, b) { #(a, b) })
23282328
|> dict.from_list
23292329

2330-
let w = float.exponential(log_random() /. int.to_float(k))
2331-
2332-
sample_loop(list, reservoir, k, k, w) |> dict.values
2330+
let w = float.exponential(log_random() /. int.to_float(n))
2331+
sample_loop(list, reservoir, n, n, w) |> dict.values
23332332
}
23342333
}
23352334
}

0 commit comments

Comments
 (0)