update
Former-commit-id: bdede6ed1b6f578f2ef046c338caf02d0b29d453 [formerly 7187de361b53e9c8ec121df379b762f2db736ea2] Former-commit-id: 447d58460fbbfd05ffe08428a1288e392637561d
This commit is contained in:
18
_embed/public/ace/demo/kitchen-sink/docs/ocaml.ml
Normal file
18
_embed/public/ace/demo/kitchen-sink/docs/ocaml.ml
Normal file
@@ -0,0 +1,18 @@
|
||||
(*
|
||||
* Example of early return implementation taken from
|
||||
* http://ocaml.janestreet.com/?q=node/91
|
||||
*)
|
||||
|
||||
let with_return (type t) (f : _ -> t) =
|
||||
let module M =
|
||||
struct exception Return of t end
|
||||
in
|
||||
let return = { return = (fun x -> raise (M.Return x)); } in
|
||||
try f return with M.Return x -> x
|
||||
|
||||
|
||||
(* Function that uses the 'early return' functionality provided by `with_return` *)
|
||||
let sum_until_first_negative list =
|
||||
with_return (fun r ->
|
||||
List.fold list ~init:0 ~f:(fun acc x ->
|
||||
if x >= 0 then acc + x else r.return acc))
|
||||
Reference in New Issue
Block a user