Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
237 lines (234 sloc) 5.54 KB
let x = 3;;
#part 1
In_channel.read_all;;
Out_channel.write_all "test.txt" ~data:"This is only a test.";;
In_channel.read_all "test.txt";;
#part 2
let x = 3;;
#part 3
#require "async";;
open Async.Std;;
Reader.file_contents;;
#part 4
let contents = Reader.file_contents "test.txt";;
Deferred.peek contents;;
#part 5
contents;;
#part 6
Deferred.peek contents;;
#part 7
Deferred.bind ;;
#part 8
let uppercase_file filename =
Deferred.bind (Reader.file_contents filename)
(fun text ->
Writer.save filename ~contents:(String.uppercase text))
;;
uppercase_file "test.txt";;
Reader.file_contents "test.txt";;
#part 9
let uppercase_file filename =
Reader.file_contents filename
>>= fun text ->
Writer.save filename ~contents:(String.uppercase text)
;;
#part 10
let count_lines filename =
Reader.file_contents filename
>>= fun text ->
List.length (String.split text ~on:'\n')
;;
#part 11
return;;
let three = return 3;;
three;;
#part 12
let count_lines filename =
Reader.file_contents filename
>>= fun text ->
return (List.length (String.split text ~on:'\n'))
;;
#part 13
Deferred.map;;
#part 14
let count_lines filename =
Reader.file_contents filename
>>| fun text ->
List.length (String.split text ~on:'\n')
;;
count_lines "/etc/hosts";;
#part 15
let ivar = Ivar.create ();;
let def = Ivar.read ivar;;
Deferred.peek def;;
Ivar.fill ivar "Hello";;
Deferred.peek def;;
#part 16
module type Delayer_intf = sig
type t
val create : Time.Span.t -> t
val schedule : t -> (unit -> 'a Deferred.t) -> 'a Deferred.t
end;;
#part 17
upon;;
#part 18
module Delayer : Delayer_intf = struct
type t = { delay: Time.Span.t;
jobs: (unit -> unit) Queue.t;
}
let create delay =
{ delay; jobs = Queue.create () }
let schedule t thunk =
let ivar = Ivar.create () in
Queue.enqueue t.jobs (fun () ->
upon (thunk ()) (fun x -> Ivar.fill ivar x));
upon (after t.delay) (fun () ->
let job = Queue.dequeue_exn t.jobs in
job ());
Ivar.read ivar
end;;
#part 19
let rec loop_forever () = loop_forever ();;
let always_fail () = assert false;;
#part 20
let do_stuff n =
let x = 3 in
if n > 0 then loop_forever ();
x + n
;;
#part 21
let rec loop_forever () : never_returns = loop_forever ();;
#part 22
let do_stuff n =
let x = 3 in
if n > 0 then loop_forever ();
x + n
;;
#part 23
never_returns;;
let do_stuff n =
let x = 3 in
if n > 0 then never_returns (loop_forever ());
x + n
;;
#part 24
let (r,w) = Pipe.create ();;
#part 25
let (r,w) = Pipe.create ();;
let write_complete = Pipe.write w "Hello World!";;
Pipe.read r;;
write_complete;;
#part 26
Pipe.transfer;;
#part 27
Command.async_basic;;
#part 28
#require "cohttp.async";;
Cohttp_async.Client.get;;
#part 29
Deferred.all;;
#part 30
Deferred.all_unit;;
#part 31
let maybe_raise =
let should_fail = ref false in
fun () ->
let will_fail = !should_fail in
should_fail := not will_fail;
after (Time.Span.of_sec 0.5)
>>= fun () ->
if will_fail then raise Exit else return ()
;;
maybe_raise ();;
maybe_raise ();;
#part 32
let handle_error () =
try
maybe_raise ()
>>| fun () -> "success"
with _ -> return "failure"
;;
handle_error ();;
handle_error ();;
#part 33
let handle_error () =
try_with (fun () -> maybe_raise ())
>>| function
| Ok () -> "success"
| Error _ -> "failure"
;;
handle_error ();;
handle_error ();;
#part 34
let blow_up () =
let monitor = Monitor.create ~name:"blow up monitor" () in
within' ~monitor maybe_raise
;;
blow_up ();;
blow_up ();;
#part 35
let swallow_error () =
let monitor = Monitor.create () in
Stream.iter (Monitor.errors monitor) ~f:(fun _exn ->
printf "an error happened\n");
within' ~monitor (fun () ->
after (Time.Span.of_sec 0.5) >>= fun () -> failwith "Kaboom!")
;;
(* swallow_error ();; *)
#part 36
exception Ignore_me;;
let swallow_some_errors exn_to_raise =
let child_monitor = Monitor.create () in
let parent_monitor = Monitor.current () in
Stream.iter (Monitor.errors child_monitor) ~f:(fun error ->
match Monitor.extract_exn error with
| Ignore_me -> printf "ignoring exn\n"
| _ -> Monitor.send_exn parent_monitor error);
within' ~monitor:child_monitor (fun () ->
after (Time.Span.of_sec 0.5)
>>= fun () -> raise exn_to_raise)
;;
#part 37
swallow_some_errors Not_found;;
#part 38
(* swallow_some_errors Ignore_me;; *)
#part 39
let string_and_float = Deferred.both
(after (sec 0.5) >>| fun () -> "A")
(after (sec 0.25) >>| fun () -> 32.33);;
string_and_float;;
#part 40
Deferred.any [ (after (sec 0.5) >>| fun () -> "half a second")
; (after (sec 10.) >>| fun () -> "ten seconds") ] ;;
#part 41
choice;;
choose;;
#part 42
let def = In_thread.run (fun () -> List.range 1 10);;
def;;
#part 43
let log_delays thunk =
let start = Time.now () in
let print_time () =
let diff = Time.diff (Time.now ()) start in
printf "%s, " (Time.Span.to_string diff)
in
let d = thunk () in
Clock.every (sec 0.1) ~stop:d print_time;
d >>| fun () -> print_time (); printf "\n"
;;
#part 44
log_delays (fun () -> after (sec 0.5));;
#part 45
let busy_loop n =
let x = ref None in
for i = 1 to 100_000_000 do x := Some i done
;;
log_delays (fun () -> return (busy_loop ()));;
#part 46
log_delays (fun () -> In_thread.run busy_loop);;
#part 47
let noalloc_busy_loop () =
for i = 0 to 100_000_000 do () done
;;
log_delays (fun () -> In_thread.run noalloc_busy_loop);;