[Assorted-commits] SF.net SVN: assorted:[1615] sandbox/trunk/src
Brought to you by:
yangzhang
From: <yan...@us...> - 2010-04-02 13:50:20
|
Revision: 1615 http://assorted.svn.sourceforge.net/assorted/?rev=1615&view=rev Author: yangzhang Date: 2010-04-02 13:50:14 +0000 (Fri, 02 Apr 2010) Log Message: ----------- added ocaml Added Paths: ----------- sandbox/trunk/src/ocaml/ sandbox/trunk/src/ocaml/dabble.ml Added: sandbox/trunk/src/ocaml/dabble.ml =================================================================== --- sandbox/trunk/src/ocaml/dabble.ml (rev 0) +++ sandbox/trunk/src/ocaml/dabble.ml 2010-04-02 13:50:14 UTC (rev 1615) @@ -0,0 +1,216 @@ +(* just generally playing around with ocaml *) + +(* modules *) + +module Hello : +sig + val hello : unit -> unit +end = +struct + let message = "Hello" + let hello () = print_endline message +end + +module type Hello2_type = +sig + val hello : unit -> unit +end +module Hello2 = +struct + let message = "Hello2" + let hello () = print_endline message +end + +let goodbye () = print_endline "Goodbye" +let hello_goodbye () = + Hello.hello (); + goodbye (); +;; + +(* objects *) + +open List + +class ['a] stack = +object (self) + val mutable the_list = ( [] : 'a list ) (* instance variable *) + method push x = (* push method *) + the_list <- x :: the_list + method pop = (* pop method *) + let result = hd the_list in + the_list <- tl the_list; + result + method peek = (* peek method *) + hd the_list + method size = (* size method *) + length the_list +end;; + +let drain_duck s = + while s#size > 0 do + ignore (s#pop) + done +;; + +let drain_stack (s : 'a stack) = + while s#size > 0 do + ignore (s#pop) + done +;; + +let test_stack () = + let s = new stack; + in + for i = 1 to 10 do + s#push i + done; + while s#size > 5 do + Printf.printf "Popped %d off the stack.\n" s#pop + done; + print_endline "Draining stack..."; + drain_duck s; + Printf.printf "%d remaining items on the stack.\n" s#size +;; + +(* mutable state *) + +let mstate () = + let myref = ref 3 + in + myref := !myref + 5; + print_int !myref (* 8 *); + print_newline (); +;; + +(* custom data structures *) + +class obj = +object (self) + val mutable parents = ( [] : obj list ) + method get_prop = + 0 + method set_prop = + 0 +end;; + +type (* set_v = { theset : string } +and str_v = { thestr : string } +(* +TODO add more here: +- cyclic type references +- cyclic references +- advanced data structures +- mutable objs; make sure you can control equality/hashing esp for objects +*) +and obj = { thename : string } +and *) value = StrV of string | SetV of string | Obj of obj +;; + +type (* set_t = { thetype : typ } +and *) obj_t = { theobj : obj } +and typ = StrT | SetT of typ | ObjT of obj +;; + +let types () = + let rec mytyp1 = SetT StrT + and o2 = new obj (* { thename = "hello" } *) + and o3 = new obj (* { thename = "hello" } *) + and mytyp2 = SetT(ObjT(o2)) + and mytyp3 = SetT(ObjT(o3)) + in + print_endline (string_of_bool (mytyp1 == mytyp2)); + print_endline (string_of_bool (mytyp2 == mytyp3)); +;; + +(* data structures: sets, maps *) + +(* module Obj_set = Set.Make + (struct + type t = obj + let compare = compare + end) +;; *) + +module Obj_map = Map.Make + (struct + type t = int + let compare = compare + end) +;; + +module SS = Set.Make(String);; + +(* module Obj_set = HashSet.Make(String) +type Obj_set = HashSet *) + +let test_ds () = + let s = SS.empty + and h = Hashtbl.create 100 in + SS.add "foobar"; +(* print_endline SS. *) +;; + +(* my objects *) + +class obj2 = +object (self) + val mutable parents = HashSet.create 100 "" + val mutable props = (Hashtbl.create 100 : (string, string) Hashtbl.t) + method get_prop p = Hashtbl.find props p +(* method set_prop p v = Hashtbl.find props p *) + method add_par par = HashSet.add parents par + method get_pars = HashSet.keys parents +end;; + +(* return values *) + +(* this actually returns 42, not () as you might think due to the ; *) +let foo () = + 42; +;; + +(* persistence *) + +open Marshal + +let marshal_list () = + let x = to_string [ "foo"; "bar" ] [] in + let y = (from_string x 0 : string list) in + print_endline (nth y 0); + print_endline (nth y 1); +;; + +let marshal_obj () = + let o = new obj2 in + let t = Hashtbl.create 100 in + let s = HashSet.create 100 "" in + o#add_par "one parent"; + Hashtbl.add t "adf" "zxcv"; + HashSet.add s "blah"; + let tser = to_string t [] in + let sser = to_string s [] in + let oser = to_string o [] in + let t2 = (from_string tser 0 : (string,string) Hashtbl.t) in + let s2 = (from_string sser 0 : string HashSet.t) in +(* let o2 = (from_string oser 0 : obj2) in*) + print_endline (Hashtbl.find t2 "adf"); + print_endline (Array.get (HashSet.keys s2) 0); +(* print_endline (Array.get (o2#get_pars) 0);*) +;; + +let main () = + hello_goodbye (); + mstate (); + types (); + print_int (foo ()); + print_newline (); + test_stack (); + test_ds (); + marshal_obj (); + marshal_list (); +;; + +main ();; + +(* match e with + Plus *) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |