[ccell] / trunk / example / radiob.ml  
ViewVC logotype

View of /trunk/example/radiob.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 64 - (download) (annotate)
Mon Jan 4 15:51:10 2010 UTC (8 years, 9 months ago) by ogasawara
File size: 1093 byte(s)
(*
  radio button dependency.
  $Id$

  to compile: 
   ocamlfind ocamlc -thread -package ccell -linkpkg radiob.ml -o radiob
*)

module R = Ccell.Frp.TimeVaryReact
module E = Ccell.Frp.TimeVaryEvent

let (+>) f g = g f
let tee f x = ignore (f x); x

let rec forever f x = 
  let v = f x in forever f v
    
let key_event, sender =
  E.make ()

let press_j =
  key_event +> E.filter (fun c -> c = 'j')

let press_k =
  key_event +> E.filter (fun c -> c = 'k')

let map_true e =
  E.map (fun _ -> true) e

let map_false e =
  E.map (fun _ -> false) e

let radio_button1 =
  let new_flag =
    E.merge (map_true press_j) (map_false press_k)
  in
  E.react true new_flag

let radio_button2 =
  let new_flag =
    E.merge (map_true press_k) (map_false press_j)
  in
  E.react false new_flag

let _ =
  radio_button1 
  +> R.changes ~eq:(=)
  +> E.listen (fun b -> Printf.printf "button1 changes %b\n%!" b);
  radio_button2
  +> R.changes ~eq:(=)
  +> E.listen (fun b -> Printf.printf "button2 changes %b\n%!" b);
  try
    forever (fun () -> sender (input_char stdin)) ()
  with
    _ ->
      ()


root@forge.ocamlcore.org
ViewVC Help
Powered by ViewVC 1.0.0