- Commit
- 1bbe53693337efb75a1fbef33faffd2d3319ad57
- Parent
- be2c5ab39ecf02aeb341a34d63941832e26f8a58
- Author
- Pablo <pablo-escobar@riseup.net>
- Date
Compressed two lines of code
A OCaml module for manipulating unlimited register machines
Compressed two lines of code
1 file changed, 3 insertions, 6 deletions
Status | File Name | N° Changes | Insertions | Deletions |
Modified | main.ml | 9 | 3 | 6 |
diff --git a/main.ml b/main.ml @@ -7,13 +7,10 @@ let usage () = let default_max_iters = 1000 (** An equivalent to Haskell's [sequence] *) -let seq (xs : 'a option list) : 'a list option = - let f x acc = - x |> Option.map (fun y -> Option.map (fun ys -> y :: ys) acc) - |> Option.join +let sequence (xs : 'a option list) : 'a list option = + let f x acc = Option.bind x (fun x -> Option.map (List.cons x) acc) in List.fold_right f xs (Some []) - (** Execute the program from [file] with initial register values given by [regs] and the maximum number of allowed iterations set to [iters] *) let main iters regs file = @@ -77,7 +74,7 @@ let (iters, regs_str, filepath) : int * string * string = let regs = let regs = List.filter ((<>) "") (String.split_on_char ' ' regs_str) in - match seq (List.map int_of_string_opt regs) with + match sequence (List.map int_of_string_opt regs) with | Some regs -> regs | None ->