Skip to content

Commit 3b8a2ca

Browse files
authored
palindrome-products: Don't make users implement helpers for tests. (#497)
The exercise is about creating about finding the largest and smallest palindromes, not about creating tedious equals and print functions. The .mli even states these as helper functions for running tests. Instead we should own those test functions ourselves.
1 parent 05ee37a commit 3b8a2ca

File tree

5 files changed

+31
-31
lines changed

5 files changed

+31
-31
lines changed

exercises/practice/palindrome-products/.meta/example.ml

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ open Base
33
type palindrome_products = {
44
value : int option;
55
factors : (int * int) list;
6-
} [@@deriving show, eq]
6+
}
77

88
let is_palindrome n =
99
let n = Int.to_string n in String.(rev n = n)
@@ -20,13 +20,13 @@ let empty = Ok {value=None; factors=[]}
2020
let smallest ~min ~max =
2121
if min > max
2222
then Error "min must be <= max"
23-
else
23+
else
2424
let open Sequence.Monad_infix in
2525
let seq = seq 1 in
26-
let products =
27-
seq min max >>= fun x ->
28-
seq x max >>= fun y ->
29-
Sequence.singleton (x * y, (x, y))
26+
let products =
27+
seq min max >>= fun x ->
28+
seq x max >>= fun y ->
29+
Sequence.singleton (x * y, (x, y))
3030
|> Sequence.filter ~f:(fun (n, _) -> is_palindrome n) in
3131
products
3232
|> Sequence.to_list
@@ -35,22 +35,20 @@ let smallest ~min ~max =
3535
|> List.hd
3636
|> Option.value_map ~default:(empty) ~f:(fun x -> Ok (to_palindrome_products x))
3737

38-
let largest ~min ~max =
38+
let largest ~min ~max =
3939
if min > max
4040
then Error "min must be <= max"
41-
else
41+
else
4242
let open Sequence.Monad_infix in
4343
let seq = seq (-1) in
44-
let products =
45-
seq max min >>= fun x ->
46-
seq x min >>= fun y ->
47-
Sequence.singleton (x * y, (y, x))
44+
let products =
45+
seq max min >>= fun x ->
46+
seq x min >>= fun y ->
47+
Sequence.singleton (x * y, (y, x))
4848
|> Sequence.filter ~f:(fun (n, _) -> is_palindrome n) in
4949
products
5050
|> Sequence.to_list
5151
|> List.sort ~compare:(fun (x,_) (y,_) -> Int.compare y x)
5252
|> List.group ~break:(fun (x, _) (y, _) -> x <> y)
5353
|> List.hd
5454
|> Option.value_map ~default:(empty) ~f:(fun x -> Ok (to_palindrome_products x))
55-
56-

exercises/practice/palindrome-products/palindrome_products.ml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,3 @@ let smallest ~min ~max =
88

99
let largest ~min ~max =
1010
failwith "'largest' is missing"
11-
12-
let show_palindrome_products _ =
13-
failwith "'show_palindrome_products' is missing"
14-
15-
let equal_palindrome_products _ _ =
16-
failwith "'equal_palindrome_products' is missing"

exercises/practice/palindrome-products/palindrome_products.mli

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,3 @@ val smallest : min : int -> max : int -> (palindrome_products, string) result
1010
(* Returns the largest palindrome with factors in the given range, or an appropriate
1111
error result if the range is ill specified, or there are no palindromes in the range. *)
1212
val largest : min : int -> max : int -> (palindrome_products, string) result
13-
14-
(* These are helper functions for tests. They can be written by hand, or using ppx_deriving
15-
https://github.com/ocaml-ppx/ppx_deriving/blob/master/README.md.
16-
*)
17-
18-
(* Returns a string representation of a palindrome_products. *)
19-
val show_palindrome_products : palindrome_products -> string
20-
21-
(* Returns true if two palindrome_products are equal. *)
22-
val equal_palindrome_products : palindrome_products -> palindrome_products -> bool

exercises/practice/palindrome-products/test.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
open OUnit2
22
open Palindrome_products
33

4+
module For_tests = struct
5+
type palindrome_products = Palindrome_products.palindrome_products = {
6+
value : int option;
7+
factors : (int * int) list;
8+
} [@@deriving eq, show]
9+
end
10+
11+
open For_tests
12+
413
let show_result printer = function
514
| Error e -> "Error " ^ e
615
| Ok a -> "Ok " ^ printer a

templates/palindrome-products/test.ml.tpl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
open OUnit2
22
open Palindrome_products
33

4+
module For_tests = struct
5+
type palindrome_products = Palindrome_products.palindrome_products = {
6+
value : int option;
7+
factors : (int * int) list;
8+
} [@@deriving eq, show]
9+
end
10+
11+
open For_tests
12+
413
let show_result printer = function
514
| Error e -> "Error " ^ e
615
| Ok a -> "Ok " ^ printer a
@@ -10,7 +19,7 @@ let eq_results eq x y = match (x, y) with
1019
| (Ok x, Ok y) -> eq x y
1120
| _ -> false
1221

13-
let ae exp got _test_ctxt =
22+
let ae exp got _test_ctxt =
1423
assert_equal ~printer:(show_result show_palindrome_products) ~cmp:(eq_results equal_palindrome_products) exp got
1524

1625
let tests = [

0 commit comments

Comments
 (0)