Skip to content

Commit 5974711

Browse files
authored
Merge pull request #394 from jmid/weight-renaming
The Great Renaming, Chapter 20: revisiting `_weighted` renaming
2 parents 895b35a + d7a2456 commit 5974711

File tree

5 files changed

+58
-48
lines changed

5 files changed

+58
-48
lines changed

CHANGELOG.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@
9393
- Renamed `shuffle` combinators:
9494
- Add `QCheck.Gen.shuffle_array` and deprecate in-place `QCheck.Gen_shuffle_a`
9595
- Add `QCheck.Gen.shuffle_list` and deprecate `QCheck.Gen_shuffle_l`
96-
- Add `QCheck.Gen.shuffle_weighted_list` and deprecate `QCheck.Gen_shuffle_w_l`
96+
- Add `QCheck.Gen.shuffle_list_weighted` and deprecate `QCheck.Gen_shuffle_w_l`
9797
- Add `QCheck2.Gen.shuffle_array` and deprecate `QCheck2.Gen_shuffle_a`
9898
- Add `QCheck2.Gen.shuffle_list` and deprecate `QCheck2.Gen_shuffle_l`
99-
- Add `QCheck2.Gen.shuffle_weighted_list` and deprecate `QCheck2.Gen_shuffle_w_l`
99+
- Add `QCheck2.Gen.shuffle_list_weighted` and deprecate `QCheck2.Gen_shuffle_w_l`
100100
- Renamed `oneof` combinators:
101101
- Add `QCheck.Gen.oneof_list` and deprecate `QCheck.Gen.oneofl`
102102
- Add `QCheck.Gen.oneof_array` and deprecate `QCheck.Gen.oneofa`
@@ -106,6 +106,16 @@
106106
like `QCheck.oneof_weighted` and deprecate `QCheck.choose` for consistency
107107
- Add `QCheck2.Gen.oneof_list` and deprecate `QCheck2.Gen.oneofl`
108108
- Add `QCheck2.Gen.oneof_array` and deprecate `QCheck2.Gen.oneofa`
109+
- Renamed `frequency` combinators:
110+
- Add `QCheck.Gen.oneof_weighted` and deprecate `QCheck.Gen.frequency`
111+
- Add `QCheck.Gen.oneof_list_weighted` and deprecate `QCheck.Gen.frequencyl`
112+
- Add `QCheck.Gen.oneof_array_weighted` and deprecate `QCheck.Gen.frequencya`
113+
- Add `QCheck.oneof_weighted` and deprecate `QCheck.frequency`
114+
- Add `QCheck.oneof_list_weighted` and deprecate `QCheck.frequency_list`
115+
- Add `QCheck.oneof_array_weighted` and deprecate `QCheck.frequency_array`
116+
- Add `QCheck2.Gen.oneof_weighted` and deprecate `QCheck2.Gen.frequency`
117+
- Add `QCheck2.Gen.oneof_list_weighted` and deprecate `QCheck2.Gen.frequencyl`
118+
- Add `QCheck2.Gen.oneof_array_weighted` and deprecate `QCheck2.Gen.frequencya`
109119
- Add missing `QCheck2.Gen.map_keep_input` for consistency
110120
- Add `QCheck.no_shrink` for consistency
111121
- Fix shrinking for `QCheck2.Gen.exponential` which could shrink to `infinity`

src/core/QCheck.ml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,22 @@ module Gen = struct
142142
let oneof_array xs st = Array.get xs (Random.State.int st (Array.length xs))
143143
let oneofa = oneof_array
144144

145-
let oneof_weighted_list l st =
145+
let oneof_list_weighted l st =
146146
let sums = sum_int (List.map fst l) in
147147
let i = Random.State.int st sums in
148148
let rec aux acc = function
149149
| ((x,g)::xs) -> if i < acc+x then g else aux (acc+x) xs
150-
| _ -> failwith "Gen.oneof_weighted_list"
150+
| _ -> failwith "Gen.oneof_list_weighted"
151151
in
152152
aux 0 l
153153

154-
let frequencyl = oneof_weighted_list
154+
let frequencyl = oneof_list_weighted
155155

156-
let oneof_weighted_array a = oneof_weighted_list (Array.to_list a)
156+
let oneof_array_weighted a = oneof_list_weighted (Array.to_list a)
157157

158-
let frequencya = oneof_weighted_array
158+
let frequencya = oneof_array_weighted
159159

160-
let oneof_weighted l st = oneof_weighted_list l st st
160+
let oneof_weighted l st = oneof_list_weighted l st st
161161

162162
let frequency = oneof_weighted
163163

@@ -358,15 +358,15 @@ module Gen = struct
358358

359359
let shuffle_l = shuffle_list
360360

361-
let shuffle_weighted_list l st =
361+
let shuffle_list_weighted l st =
362362
let sample (w, v) =
363363
let fl_w = float_of_int w in
364364
(float_bound_inclusive 1. st ** (1. /. fl_w), v)
365365
in
366366
let samples = List.rev_map sample l in
367367
List.sort (fun (w1, _) (w2, _) -> poly_compare w1 w2) samples |> List.rev_map snd
368368

369-
let shuffle_w_l = shuffle_weighted_list
369+
let shuffle_w_l = shuffle_list_weighted
370370

371371
let range_subset ~size low high st =
372372
let range_size = high - low + 1 in
@@ -1896,10 +1896,10 @@ let frequency ?print ?small ?shrink ?collect l =
18961896

18971897
(** Given list of [(frequency,value)] pairs, returns value with probability proportional
18981898
to given frequency *)
1899-
let oneof_weighted_list ?print ?small l = make ?print ?small (Gen.oneof_weighted_list l)
1900-
let frequencyl = oneof_weighted_list
1901-
let oneof_weighted_array ?print ?small a = make ?print ?small (Gen.oneof_weighted_array a)
1902-
let frequencya = oneof_weighted_array
1899+
let oneof_list_weighted ?print ?small l = make ?print ?small (Gen.oneof_list_weighted l)
1900+
let frequencyl = oneof_list_weighted
1901+
let oneof_array_weighted ?print ?small a = make ?print ?small (Gen.oneof_array_weighted a)
1902+
let frequencya = oneof_array_weighted
19031903

19041904
let map_same_type f a =
19051905
adapt_ a (fun st -> f (a.gen st))

src/core/QCheck.mli

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -234,25 +234,25 @@ module Gen : sig
234234
Each of the given generators are chosen based on a positive integer weight.
235235
@deprecated use {!oneof_weighted} instead. *)
236236

237-
val oneof_weighted_list : (int * 'a) list -> 'a t
237+
val oneof_list_weighted : (int * 'a) list -> 'a t
238238
(** Constructs a generator that selects among a given list of values.
239239
Each of the given values are chosen based on a positive integer weight.
240240
@since NEXT_RELEASE *)
241241

242-
val frequencyl : (int * 'a) list -> 'a t [@@deprecated "Use [oneof_weighted_list] instead"]
242+
val frequencyl : (int * 'a) list -> 'a t [@@deprecated "Use [oneof_list_weighted] instead"]
243243
(** Constructs a generator that selects among a given list of values.
244244
Each of the given values are chosen based on a positive integer weight.
245-
@deprecated use {!oneof_weighted_list} instead. *)
245+
@deprecated use {!oneof_list_weighted} instead. *)
246246

247-
val oneof_weighted_array : (int * 'a) array -> 'a t
247+
val oneof_array_weighted : (int * 'a) array -> 'a t
248248
(** Constructs a generator that selects among a given array of values.
249249
Each of the array entries are chosen based on a positive integer weight.
250250
@since NEXT_RELEASE *)
251251

252-
val frequencya : (int * 'a) array -> 'a t [@@deprecated "Use [oneof_weighted_array] instead"]
252+
val frequencya : (int * 'a) array -> 'a t [@@deprecated "Use [oneof_array_weighted] instead"]
253253
(** Constructs a generator that selects among a given array of values.
254254
Each of the array entries are chosen based on a positive integer weight.
255-
@deprecated use {!oneof_weighted_array} instead. *)
255+
@deprecated use {!oneof_array_weighted} instead. *)
256256

257257
val shuffle_array : 'a array -> 'a array t
258258
(** Creates a generator of shuffled arrays.
@@ -270,7 +270,7 @@ module Gen : sig
270270
(** Creates a generator of shuffled lists.
271271
@deprecated Use {!shuffle_list} instead. *)
272272

273-
val shuffle_weighted_list : (int * 'a) list -> 'a list t
273+
val shuffle_list_weighted : (int * 'a) list -> 'a list t
274274
(** Creates a generator of weighted shuffled lists. A given list is shuffled on each
275275
generation according to the weights of its elements. An element with a larger weight
276276
is more likely to be at the front of the list than an element with a smaller weight.
@@ -283,10 +283,10 @@ module Gen : sig
283283
284284
@since NEXT_RELEASE *)
285285

286-
val shuffle_w_l : (int * 'a) list -> 'a list t [@@deprecated "Use [shuffle_weighted_list] instead"]
287-
(** An alias for {!shuffle_weighted_list} since NEXT_RELEASE.
286+
val shuffle_w_l : (int * 'a) list -> 'a list t [@@deprecated "Use [shuffle_list_weighted] instead"]
287+
(** An alias for {!shuffle_list_weighted} since NEXT_RELEASE.
288288
@since 0.11
289-
@deprecated use {!shuffle_weighted_list} instead. *)
289+
@deprecated use {!shuffle_list_weighted} instead. *)
290290

291291
val range_subset : size:int -> int -> int -> int array t
292292
(** [range_subset ~size:k low high] generates an array of length [k]
@@ -1812,27 +1812,27 @@ val frequency : ?print:'a Print.t -> ?small:('a -> int) ->
18121812
(** Similar to {!oneof} but with frequencies.
18131813
@deprecated use {!oneof_weighted} instead. *)
18141814

1815-
val oneof_weighted_list : ?print:'a Print.t -> ?small:('a -> int) ->
1815+
val oneof_list_weighted : ?print:'a Print.t -> ?small:('a -> int) ->
18161816
(int * 'a) list -> 'a arbitrary
18171817
(** Same as {!oneof_list}, but each element is paired with its frequency in
18181818
the probability distribution (the higher, the more likely).
18191819
@since NEXT_RELEASE *)
18201820

18211821
val frequencyl : ?print:'a Print.t -> ?small:('a -> int) ->
1822-
(int * 'a) list -> 'a arbitrary [@@deprecated "Use [oneof_weighted_list] instead"]
1822+
(int * 'a) list -> 'a arbitrary [@@deprecated "Use [oneof_list_weighted] instead"]
18231823
(** Same as {!oneof_list}, but each element is paired with its frequency in
18241824
the probability distribution (the higher, the more likely).
1825-
@deprecated use {!oneof_weighted_list} instead. *)
1825+
@deprecated use {!oneof_list_weighted} instead. *)
18261826

1827-
val oneof_weighted_array : ?print:'a Print.t -> ?small:('a -> int) ->
1827+
val oneof_array_weighted : ?print:'a Print.t -> ?small:('a -> int) ->
18281828
(int * 'a) array -> 'a arbitrary
1829-
(** Same as {!oneof_weighted_list}, but with an array.
1829+
(** Same as {!oneof_list_weighted}, but with an array.
18301830
@since NEXT_RELEASE *)
18311831

18321832
val frequencya : ?print:'a Print.t -> ?small:('a -> int) ->
1833-
(int * 'a) array -> 'a arbitrary [@@deprecated "Use [oneof_weighted_array] instead"]
1834-
(** Same as {!oneof_weighted_list}, but with an array.
1835-
@deprecated use {!oneof_weighted_array} instead. *)
1833+
(int * 'a) array -> 'a arbitrary [@@deprecated "Use [oneof_array_weighted] instead"]
1834+
(** Same as {!oneof_list_weighted}, but with an array.
1835+
@deprecated use {!oneof_array_weighted} instead. *)
18361836

18371837
val map : ?rev:('b -> 'a) -> ('a -> 'b) -> 'a arbitrary -> 'b arbitrary
18381838
(** [map f a] returns a new arbitrary instance that generates values using

src/core/QCheck2.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -660,15 +660,15 @@ module Gen = struct
660660

661661
let frequency = oneof_weighted
662662

663-
let oneof_weighted_list (l : (int * 'a) list) : 'a t =
663+
let oneof_list_weighted (l : (int * 'a) list) : 'a t =
664664
List.map (fun (weight, value) -> (weight, pure value)) l
665665
|> oneof_weighted
666666

667-
let frequencyl = oneof_weighted_list
667+
let frequencyl = oneof_list_weighted
668668

669-
let oneof_weighted_array a = oneof_weighted_list (Array.to_list a)
669+
let oneof_array_weighted a = oneof_list_weighted (Array.to_list a)
670670

671-
let frequencya = oneof_weighted_array
671+
let frequencya = oneof_array_weighted
672672

673673
let char_range ?(origin : char option) (a : char) (b : char) : char t =
674674
(int_range ~origin:(Char.code (Option.value ~default:a origin)) (Char.code a) (Char.code b)) >|= Char.chr
@@ -779,7 +779,7 @@ module Gen = struct
779779

780780
let shuffle_l = shuffle_list
781781

782-
let shuffle_weighted_list (l : ((int * 'a) list)) : 'a list t = fun st ->
782+
let shuffle_list_weighted (l : ((int * 'a) list)) : 'a list t = fun st ->
783783
let sample (w, v) =
784784
let Tree.Tree (p, _) = float_bound_inclusive 1. st in
785785
let fl_w = float_of_int w in
@@ -791,7 +791,7 @@ module Gen = struct
791791
|> List.rev_map snd
792792
|> Tree.pure
793793

794-
let shuffle_w_l = shuffle_weighted_list
794+
let shuffle_w_l = shuffle_list_weighted
795795

796796
let pair (g1 : 'a t) (g2 : 'b t) : ('a * 'b) t = liftA2 (fun a b -> (a, b)) g1 g2
797797

src/core/QCheck2.mli

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -737,36 +737,36 @@ module Gen : sig
737737
@deprecated use {!oneof_weighted} instead.
738738
*)
739739

740-
val oneof_weighted_list : (int * 'a) list -> 'a t
740+
val oneof_list_weighted : (int * 'a) list -> 'a t
741741
(** Constructs a generator that selects among a given list of values.
742742
Each of the given values are chosen based on a positive integer weight.
743743
744744
Shrinks towards the first element of the list.
745745
@since NEXT_RELEASE
746746
*)
747747

748-
val frequencyl : (int * 'a) list -> 'a t [@@deprecated "Use [oneof_weighted_list] instead"]
748+
val frequencyl : (int * 'a) list -> 'a t [@@deprecated "Use [oneof_list_weighted] instead"]
749749
(** Constructs a generator that selects among a given list of values.
750750
Each of the given values are chosen based on a positive integer weight.
751751
752752
Shrinks towards the first element of the list.
753-
@deprecated use {!oneof_weighted_list} instead.
753+
@deprecated use {!oneof_list_weighted} instead.
754754
*)
755755

756-
val oneof_weighted_array : (int * 'a) array -> 'a t
756+
val oneof_array_weighted : (int * 'a) array -> 'a t
757757
(** Constructs a generator that selects among a given array of values.
758758
Each of the array entries are chosen based on a positive integer weight.
759759
760760
Shrinks towards the first element of the array.
761761
@since NEXT_RELEASE
762762
*)
763763

764-
val frequencya : (int * 'a) array -> 'a t [@@deprecated "Use [oneof_weighted_array] instead"]
764+
val frequencya : (int * 'a) array -> 'a t [@@deprecated "Use [oneof_array_weighted] instead"]
765765
(** Constructs a generator that selects among a given array of values.
766766
Each of the array entries are chosen based on a positive integer weight.
767767
768768
Shrinks towards the first element of the array.
769-
@deprecated use {!oneof_weighted_array} instead.
769+
@deprecated use {!oneof_array_weighted} instead.
770770
*)
771771

772772
(** {3 Shuffling elements} *)
@@ -787,7 +787,7 @@ module Gen : sig
787787
(** Creates a generator of shuffled lists.
788788
@deprecated use {!shuffle_list} instead. *)
789789

790-
val shuffle_weighted_list : (int * 'a) list -> 'a list t
790+
val shuffle_list_weighted : (int * 'a) list -> 'a list t
791791
(** Creates a generator of weighted shuffled lists. A given list is shuffled on each
792792
generation according to the weights of its elements. An element with a larger weight
793793
is more likely to be at the front of the list than an element with a smaller weight.
@@ -800,10 +800,10 @@ module Gen : sig
800800
801801
@since NEXT_RELEASE *)
802802

803-
val shuffle_w_l : (int * 'a) list -> 'a list t [@@deprecated "Use [shuffle_weighted_list] instead"]
804-
(** A synonym for {!shuffle_weighted_list} since NEXT_RELEASE
803+
val shuffle_w_l : (int * 'a) list -> 'a list t [@@deprecated "Use [shuffle_list_weighted] instead"]
804+
(** A synonym for {!shuffle_list_weighted} since NEXT_RELEASE
805805
@since 0.11
806-
@deprecated use {!shuffle_weighted_list} instead. *)
806+
@deprecated use {!shuffle_list_weighted} instead. *)
807807

808808
(** {3 Corner cases} *)
809809

0 commit comments

Comments
 (0)