caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] newbie type problem
  2003-10-24  6:11 [Caml-list] newbie type problem Dustin Sallings
@ 2003-10-23 23:32 ` David Brown
  2003-10-24  6:54   ` Dustin Sallings
  0 siblings, 1 reply; 12+ messages in thread
From: David Brown @ 2003-10-23 23:32 UTC (permalink / raw)
  To: Dustin Sallings; +Cc: caml-list

On Thu, Oct 23, 2003 at 11:11:29PM -0700, Dustin Sallings wrote:

> type per_block = {
>     pb_ts: int;
>     pb_counts: [hashtable goes here];
>     pb_times: [hashtable goes here];
> };;
> 
> 	Can anybody tell me what I'm actually trying to do here?

It is a little hard, since I can't tell what you're trying to represent.
Also, a hashtable might not be the best data structure.

What information are you trying to keep track of?  A hashtable maps
objects of one type to another.  If you wanted the pb_counts field to
map strings to int refs (so you can change the refs), you could use a

   pb_counts: (string, int ref) Hashtbl.t

without more information, it is hard to tell if that is what you really
want.

Ocaml also has arrays, maps, sets, and there are numerous libraries with
other structures as well.

Dave Brown

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] newbie type problem
  2003-10-24  6:54   ` Dustin Sallings
@ 2003-10-24  0:52     ` David Brown
  2003-10-24  8:21       ` Dustin Sallings
  2003-10-24  9:25     ` Hendrik Tews
  1 sibling, 1 reply; 12+ messages in thread
From: David Brown @ 2003-10-24  0:52 UTC (permalink / raw)
  To: Dustin Sallings; +Cc: David Brown, caml-list

On Thu, Oct 23, 2003 at 11:54:07PM -0700, Dustin Sallings wrote:

> { 0: <
> 	pb_counts = { "T1": 23, "T2": 43 }
> 	pb_times = { "T1": 291, "T2": 92 }
> 	>
>   300: <
> 	pb_counts = { "T1": 29, "T2": 50 }
> 	pb_times = { "T1": 202, "T2": 87 }
> 	>
> }
> 
> 	When I spell it out that way, it looks a little less than optimal.  
> However, this is more of a learning tool at this point.

You may want to consider putting the data for a given transaction type
into its own type, and having that be the target of the hashtbl.

   type transaction_info = {
     mutable counts : int;
     mutable times : int;
   }

Then your results could just be a

   (int * (string, transaction_info) Hashtbl.t) list

Dave

BTW, this actually seems like a good problem for learning the language.
I seem to always choose things too difficult, and end up getting
frustrated.

BTW, I have found the .mli files for the libraries to be the best
documentation.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Caml-list] newbie type problem
@ 2003-10-24  6:11 Dustin Sallings
  2003-10-23 23:32 ` David Brown
  0 siblings, 1 reply; 12+ messages in thread
From: Dustin Sallings @ 2003-10-24  6:11 UTC (permalink / raw)
  To: caml-list


	I'm fairly new to caml, but have run into something I can't seem to 
figure out.

	I'm trying to rewrite an application I'd originally written in python 
(and scheme, and eiffel, and C, etc...), but I can't seem to figure out 
how to define the type I want.  Basically, it might look something like 
this:

type per_block = {
     pb_ts: int;
     pb_counts: [hashtable goes here];
     pb_times: [hashtable goes here];
};;

	Can anybody tell me what I'm actually trying to do here?

--
SPY                      My girlfriend asked me which one I like better.
pub  1024/3CAE01D5 1994/11/03 Dustin Sallings <dustin@spy.net>
|    Key fingerprint =  87 02 57 08 02 D0 DA D6  C8 0F 3E 65 51 98 D8 BE
L_______________________ I hope the answer won't upset her. ____________

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] newbie type problem
  2003-10-23 23:32 ` David Brown
@ 2003-10-24  6:54   ` Dustin Sallings
  2003-10-24  0:52     ` David Brown
  2003-10-24  9:25     ` Hendrik Tews
  0 siblings, 2 replies; 12+ messages in thread
From: Dustin Sallings @ 2003-10-24  6:54 UTC (permalink / raw)
  To: David Brown; +Cc: caml-list


On Thursday, Oct 23, 2003, at 16:32 US/Pacific, David Brown wrote:

> It is a little hard, since I can't tell what you're trying to 
> represent.
> Also, a hashtable might not be the best data structure.
>
> What information are you trying to keep track of?  A hashtable maps
> objects of one type to another.  If you wanted the pb_counts field to
> map strings to int refs (so you can change the refs), you could use a
>
>    pb_counts: (string, int ref) Hashtbl.t

	Yeah, this is the kind of thing I was looking for.  I tried a variety 
of things I thought looked like this.

> without more information, it is hard to tell if that is what you really
> want.
>
> Ocaml also has arrays, maps, sets, and there are numerous libraries 
> with
> other structures as well.

	When picking up a language, there always seems to be a bit of effort 
required to learn how to read the docs.  The docs for Map are pretty 
much empty, but it does link to the source, which gives me a good idea.

	I'm not sure how to express my data structure...  I'm parsing a log 
file that contains start and stop timestamps for various transaction 
types.  I want to count up the number and total time of each of these 
transactions and group them by timestamp rounded to the nearest five 
minutes.  So, it'd look something like this:

{ 0: <
	pb_counts = { "T1": 23, "T2": 43 }
	pb_times = { "T1": 291, "T2": 92 }
	>
   300: <
	pb_counts = { "T1": 29, "T2": 50 }
	pb_times = { "T1": 202, "T2": 87 }
	>
}

	When I spell it out that way, it looks a little less than optimal.  
However, this is more of a learning tool at this point.

	Thanks for your help.

--
SPY                      My girlfriend asked me which one I like better.
pub  1024/3CAE01D5 1994/11/03 Dustin Sallings <dustin@spy.net>
|    Key fingerprint =  87 02 57 08 02 D0 DA D6  C8 0F 3E 65 51 98 D8 BE
L_______________________ I hope the answer won't upset her. ____________

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] newbie type problem
  2003-10-24  0:52     ` David Brown
@ 2003-10-24  8:21       ` Dustin Sallings
  2003-10-24 12:49         ` Andrew Lenharth
  2003-10-29  7:23         ` Florian Hars
  0 siblings, 2 replies; 12+ messages in thread
From: Dustin Sallings @ 2003-10-24  8:21 UTC (permalink / raw)
  To: David Brown; +Cc: caml-list


On Thursday, Oct 23, 2003, at 17:52 US/Pacific, David Brown wrote:

> You may want to consider putting the data for a given transaction type
> into its own type, and having that be the target of the hashtbl.
>
>    type transaction_info = {
>      mutable counts : int;
>      mutable times : int;
>    }

	That's exactly what I did, 'cept my time is a float.  :)

> Then your results could just be a
>
>    (int * (string, transaction_info) Hashtbl.t) list

	Well, part of the problem is that my log files aren't necessarily 
sequential, so I have to be able to go back to any point in time and 
update the thing.  Other than that, that's about right (from what I can 
read of that syntax so far).

> BTW, this actually seems like a good problem for learning the language.
> I seem to always choose things too difficult, and end up getting
> frustrated.

	It's still plenty foreign to me, but this is complex enough to help me 
out with a bunch of concepts in the language.  What amazes me is the 
lack of resources I've been able to find in ocaml so far (just 
googling).  It seems like caml's been around for a while, and it works 
quite well.

	I wrote these log processor things in python originally because I know 
it pretty well, but it doesn't run very fast.  In my early tests, ocaml 
and bigloo ran at roughly the same speed, and caml seems very foreign 
to me.  However, recently, I've had a few instability problems with 
bigloo under certain circumstances.  I rewrote that app in ocaml and it 
was many, many times faster and it worked more reliably.

> BTW, I have found the .mli files for the libraries to be the best
> documentation.

	Yeah, but I still couldn't figure out how to make a Map, assuming 
that's what I really wanted in the first place.  A hash table that 
iterates in key sorted order would make my app faster and reduce 
complexity.  I want to see it actually work first, though.  :)

--
SPY                      My girlfriend asked me which one I like better.
pub  1024/3CAE01D5 1994/11/03 Dustin Sallings <dustin@spy.net>
|    Key fingerprint =  87 02 57 08 02 D0 DA D6  C8 0F 3E 65 51 98 D8 BE
L_______________________ I hope the answer won't upset her. ____________

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] newbie type problem
  2003-10-24  6:54   ` Dustin Sallings
  2003-10-24  0:52     ` David Brown
@ 2003-10-24  9:25     ` Hendrik Tews
  2003-10-24 16:23       ` Dustin Sallings
  1 sibling, 1 reply; 12+ messages in thread
From: Hendrik Tews @ 2003-10-24  9:25 UTC (permalink / raw)
  To: caml-list

Dustin Sallings writes:
   
   	When picking up a language, there always seems to be a bit of effort 
   required to learn how to read the docs.  The docs for Map are pretty 
   much empty, but it does link to the source, which gives me a good idea.
   
The txt version contains almost 200 lines for Map, describing 9
constants, 2 types and one functor. The same information is in
all the other versions. Maybe you have not seen the documentation
yet, because you have not clicked on "Make" or "S"?

Bye,

Hendrik

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] newbie type problem
  2003-10-24  8:21       ` Dustin Sallings
@ 2003-10-24 12:49         ` Andrew Lenharth
  2003-10-24 13:22           ` Remi Vanicat
  2003-10-29  7:23         ` Florian Hars
  1 sibling, 1 reply; 12+ messages in thread
From: Andrew Lenharth @ 2003-10-24 12:49 UTC (permalink / raw)
  To: Dustin Sallings; +Cc: caml-list

> 	Yeah, but I still couldn't figure out how to make a Map, assuming 
> that's what I really wanted in the first place.  A hash table that 
> iterates in key sorted order would make my app faster and reduce 
> complexity.  I want to see it actually work first, though.  :)

Yes, making a map is non-obvious.  One first has to figure out the 
module system, with no examples.  Here is a hint:

module StringSet = Set.Make(struct type t = string let compare x y = 
compare x y end)

then you can do things like
StringSet.mem n1 set;;
StringSet.union s1 s2;;
StringSet.add x StringSet.empty;;

BTW: I am sure you can do the compare without the x y by 
compare=compare.

A bit about what is going on.  Set is a parameratized module which you 
must narrow to what you want.  So you use the Make function to return a 
module with the types you want.  They you can use this module in your 
code.

Good examples of this are few and far between.

Andrew Lenharth

-- 
"The reasonable man adapts himself to the world; the unreasonable 
one persists in trying to adapt the world to 
himself. Therefore all progress depends on the unreasonable man."
-- George Bernard Shaw

No matter how cynical you become, it's never enough to keep up.
-- Lily Tomlin

Fools ignore complexity; pragmatists suffer it; experts avoid it; 
geniuses remove it.
-- A. Perlis

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] newbie type problem
  2003-10-24 12:49         ` Andrew Lenharth
@ 2003-10-24 13:22           ` Remi Vanicat
  0 siblings, 0 replies; 12+ messages in thread
From: Remi Vanicat @ 2003-10-24 13:22 UTC (permalink / raw)
  To: Andrew Lenharth; +Cc: Dustin Sallings, caml-list

Andrew Lenharth <andrewl@debian.org> writes:

>> 	Yeah, but I still couldn't figure out how to make a Map, assuming 
>> that's what I really wanted in the first place.  A hash table that 
>> iterates in key sorted order would make my app faster and reduce 
>> complexity.  I want to see it actually work first, though.  :)
>
> Yes, making a map is non-obvious.  One first has to figure out the 
> module system, with no examples.  Here is a hint:
>
> module StringSet = Set.Make(struct type t = string let compare x y = 
> compare x y end)
>
> then you can do things like
> StringSet.mem n1 set;;
> StringSet.union s1 s2;;
> StringSet.add x StringSet.empty;;
>
> BTW: I am sure you can do the compare without the x y by 
> compare=compare.

Yes you can.

In recent caml you might even do :

module StringSet = Set.Make (String)

It work because in the stdlib's String module there is a type t equal to
string, and a compare function.


-- 
Rémi Vanicat

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] newbie type problem
  2003-10-24  9:25     ` Hendrik Tews
@ 2003-10-24 16:23       ` Dustin Sallings
  0 siblings, 0 replies; 12+ messages in thread
From: Dustin Sallings @ 2003-10-24 16:23 UTC (permalink / raw)
  To: Hendrik Tews; +Cc: caml-list


On Friday, Oct 24, 2003, at 02:25 US/Pacific, Hendrik Tews wrote:

> Dustin Sallings writes:
>
>>    	When picking up a language, there always seems to be a bit of 
>> effort
>>    required to learn how to read the docs.  The docs for Map are 
>> pretty
>>    much empty, but it does link to the source, which gives me a good 
>> idea.
>
> The txt version contains almost 200 lines for Map, describing 9
> constants, 2 types and one functor. The same information is in
> all the other versions. Maybe you have not seen the documentation
> yet, because you have not clicked on "Make" or "S"?

	You are correct.  Like I said, takes a little while to get used to doc 
navigation.  :)  Thanks.

-- 
Dustin Sallings

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] newbie type problem
  2003-10-24  8:21       ` Dustin Sallings
  2003-10-24 12:49         ` Andrew Lenharth
@ 2003-10-29  7:23         ` Florian Hars
  2003-10-29  8:03           ` Dustin Sallings
  1 sibling, 1 reply; 12+ messages in thread
From: Florian Hars @ 2003-10-29  7:23 UTC (permalink / raw)
  To: Dustin Sallings; +Cc: caml-list

Dustin Sallings wrote:
>     Well, part of the problem is that my log files aren't necessarily 
> sequential, so I have to be able to go back to any point in time and 
> update the thing.

Last time I had that problem I used two structures: one to keep the open events 
that come in from the logfiles and one to keep the data for the complete 
transactions where I have read all the relevant events.
If your logfiles are line-oriented (as these critters tend to be), you might 
want to write a function thats folds a function over all lines of a text file.
Then your program will look something like (add error checking, logic for 
handling overlapping transactions of the same type and missing functions to taste):

type evt = Start of string * float | End of string * float | Junk

module M = Map.make (struct type t = string let compare = compare end)

let parse_line s =
   if is_start s then
     Start (get_transaction_type s, get_time_form_log_line s)
   else if is_end s then
     End (get_transaction_type s, get_time_from_log_line s)
   else
      Junk

let operate_on_line (start_events, transactions as init) s =
   match parse_line s with
   | Junk -> init
   | Start (t_type, time) ->
         M.add t_type time start_events, transactions
   | End (t_type, time) ->
	let start_time = M.find t_type start_events in
	let l = try M.find t_type transactions with Not_found -> [] in
         M.remove t_type start_events,
         M.add t_type ((time - start_time)::l) transactions

let parse_logfile filename =
   let ic = open_in filename in
   let start_events, transactions =
     Textfile.fold operate_on_line (M.empty, M.empty) ic in
   close_in ic;
   cleanup_dangling_events start_events transactions

let _ = print_transaction_info (parse_logfile "my_logfile")

Yours, Florian.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] newbie type problem
  2003-10-29  7:23         ` Florian Hars
@ 2003-10-29  8:03           ` Dustin Sallings
  2003-10-29 16:27             ` Florian Hars
  0 siblings, 1 reply; 12+ messages in thread
From: Dustin Sallings @ 2003-10-29  8:03 UTC (permalink / raw)
  To: Florian Hars; +Cc: caml-list


On Oct 28, 2003, at 11:23 PM, Florian Hars wrote:

> let parse_logfile filename =
>   let ic = open_in filename in
>   let start_events, transactions =
>     Textfile.fold operate_on_line (M.empty, M.empty) ic in
>   close_in ic;
>   cleanup_dangling_events start_events transactions
>
> let _ = print_transaction_info (parse_logfile "my_logfile")

	What is Textfile?  I wrote something very similar to what I think this 
is in scheme (from which this thing is being ported somewhat slowly):

; Loop on input, pass each line to function f.
; Optional argument should be either a port, or a filename.  If not
; provided, (current-input-port) will be used.
(input-loop f . other)

; A conditional input loop (only loop on a line if (c line) is true)
(conditional-input-loop c f . other)

	I'll probably do the same thing in caml when I get this a little more 
out of the toy phase.

	Thanks for the input.

-- 
Dustin Sallings

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] newbie type problem
  2003-10-29  8:03           ` Dustin Sallings
@ 2003-10-29 16:27             ` Florian Hars
  0 siblings, 0 replies; 12+ messages in thread
From: Florian Hars @ 2003-10-29 16:27 UTC (permalink / raw)
  To: Dustin Sallings; +Cc: caml-list

Dustin Sallings wrote:
>     What is Textfile?  I wrote something very similar to what I think 
> this is in scheme (from which this thing is being ported somewhat slowly):
> 
> ; Loop on input, pass each line to function f.

Yes, it is one of those module everybody writes. My version contains functions

val iter : (string -> unit) -> in_channel -> unit
val fold : ('a -> string -> 'a) -> 'a -> in_channel -> 'a

doing the obvious things with the lines read from the in_channel.

Yours, Florian.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2003-10-29 16:27 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-24  6:11 [Caml-list] newbie type problem Dustin Sallings
2003-10-23 23:32 ` David Brown
2003-10-24  6:54   ` Dustin Sallings
2003-10-24  0:52     ` David Brown
2003-10-24  8:21       ` Dustin Sallings
2003-10-24 12:49         ` Andrew Lenharth
2003-10-24 13:22           ` Remi Vanicat
2003-10-29  7:23         ` Florian Hars
2003-10-29  8:03           ` Dustin Sallings
2003-10-29 16:27             ` Florian Hars
2003-10-24  9:25     ` Hendrik Tews
2003-10-24 16:23       ` Dustin Sallings

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).