Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

made Piece.of_string raise an exception on parsing fail rather than r…

…eturn an option
  • Loading branch information...
commit 6325b7087b5dbe3c366e104157de36f299839833 1 parent 901aae4
@rapha authored
Showing with 8 additions and 7 deletions.
  1. +2 −1  board.ml
  2. +1 −1  piece.ml
  3. +5 −5 test.ml
View
3  board.ml
@@ -69,7 +69,8 @@ let to_string =
rows |- List.rev |- List.map row_to_string |- List.reduce (^)
let build rows =
- let cols = rows |> List.map (Str.split (Str.regexp "") |- List.map Piece.of_string) |> transpose row_length in
+ let try_parse_piece s = try Some (Piece.of_string s) with _ -> None in
+ let cols = rows |> List.map (Str.split (Str.regexp "") |- List.map try_parse_piece) |> transpose row_length in
Board cols
let evaluate _ _ = 0.
View
2  piece.ml
@@ -1,7 +1,7 @@
type t = A | B
let to_string = function A -> "A" | B -> "B"
-let of_string = function "A" -> Some A | "B" -> Some B | _ -> None
+let of_string = function "A" -> A | "B" -> B | s -> failwith (Printf.sprintf "invalid piece: %s" s)
let opponent = function
| A -> B
View
10 test.ml
@@ -7,12 +7,12 @@ let _ =
let drop_in col _ _ = col in
run_test_tt_main ("unit tests" >::: [
- ("Player.to_string" >::: [
- "A" >:: (fun() ->
- Piece.A |> Piece.to_string |> assert_equal "A"
+ ("Player.to_string and Player.of_string are inverse" >::: [
+ "piece -> string -> piece" >:: (fun() ->
+ Piece.A |> Piece.to_string |> Piece.of_string |> assert_equal Piece.A
);
- "B" >:: (fun() ->
- Piece.B |> Piece.to_string |> assert_equal "B"
+ "string -> piece -> string" >:: (fun() ->
+ "A" |> Piece.of_string |> Piece.to_string |> assert_equal "A"
);
]);
("Board" >::: [
Please sign in to comment.
Something went wrong with that request. Please try again.