* Re: Random.int on non unix platform
@ 1999-05-26 12:07 Damien Doligez
1999-06-28 22:35 ` Martin Quinson
0 siblings, 1 reply; 5+ messages in thread
From: Damien Doligez @ 1999-05-26 12:07 UTC (permalink / raw)
To: caml-list
>From: Martin Quinson <mquinson@zeppelin-cb.de>
>How can I write this without the unix library ?
>
>Random.init (int_of_float (Unix.time ()));;
You can do:
Random.init (int_of_float (Sys.time ()));;
or you can input a random seed from the user and give it as argument
to Random.init.
-- Damien
^ permalink raw reply [flat|nested] 5+ messages in thread
* Events, method iterator
1999-06-28 22:35 ` Martin Quinson
@ 1999-05-30 19:44 ` Ubik
1999-06-01 9:39 ` Jerome Vouillon
0 siblings, 1 reply; 5+ messages in thread
From: Ubik @ 1999-05-30 19:44 UTC (permalink / raw)
To: caml-list
Hi everybody. I premise:
1) I'm a neophyte about caml
2) I can't speak French, so my mails will be mono-lingual.
Now the questions :
a) How can i get the value associated with an event (if an event actually
"carries" a value ...) ?
i tried to send out a function through an int->int->int channel, i
receive it from the other side with
an int->int->int event, but i can't apply it ...
b) Is there a way to iterate a method upon a list of objects of some type ?
Suppose i have a hierarchy
with many overridden methods, and that i use containers of base-type,
like this :
class shape = object
method virtual paint : unit
...
end
class circle = object
inherit shape
method paint = ...
...
end
...
let paintObject x = x#paint
class container = object
inherit shape
val mutable _objs:shape list = []
method paint = List.iter paintObject _objs
...
end
I have to define a function for every method to iterate, in this way ?
There's no way to define an
explicit function to send messages to objects ?
Thank you
Emiliano
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Events, method iterator
1999-05-30 19:44 ` Events, method iterator Ubik
@ 1999-06-01 9:39 ` Jerome Vouillon
0 siblings, 0 replies; 5+ messages in thread
From: Jerome Vouillon @ 1999-06-01 9:39 UTC (permalink / raw)
To: Ubik, caml-list
On Sun, May 30, 1999 at 09:44:25PM +0200, Ubik wrote:
> a) How can i get the value associated with an event (if an event actually
> "carries" a value ...) ?
> i tried to send out a function through an int->int->int channel, i
> receive it from the other side with
> an int->int->int event, but i can't apply it ...
An event is something that may happen on a channel. You have to wait
for it to get a value from the channel. You can either wait for one
event using the function "Event.sync", wait for any of a list of
events with "select" or check if an event has already happen with
"poll".
[...]
> let paintObject x = x#paint
>
> class container = object
> inherit shape
> val mutable _objs:shape list = []
> method paint = List.iter paintObject _objs
> ...
> end
> I have to define a function for every method to iterate, in this way ?
> There's no way to define an
> explicit function to send messages to objects ?
Yes, you need to define a function each time. But you can do it inline :
method paint = List.iter (fun x -> x#paint) _objs
-- Jérôme
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Random.int on non unix platform
1999-05-26 12:07 Random.int on non unix platform Damien Doligez
@ 1999-06-28 22:35 ` Martin Quinson
1999-05-30 19:44 ` Events, method iterator Ubik
0 siblings, 1 reply; 5+ messages in thread
From: Martin Quinson @ 1999-06-28 22:35 UTC (permalink / raw)
To: Damien Doligez; +Cc: caml-list
> >From: Martin Quinson <mquinson@zeppelin-cb.de>
>
> >How can I write this without the unix library ?
> >
> >Random.init (int_of_float (Unix.time ()));;
>
> You can do:
>
> Random.init (int_of_float (Sys.time ()));;
>
> or you can input a random seed from the user and give it as argument
> to Random.init.
(francais en bas)
hmmm. from htmlman/module sys :
> val time: unit -> float
>
> Return the processor time, in seconds, used by the program since the
> beginning of execution.
So, if this call is at the begining of the programm, it will allways generate
the same number (or almost). from the random.ml source code :
(* Simple initialisation. The seed is an integer.
Two seeds that are close enough will not produce uncorrelated
pseudo-random sequences.
*)
And I really dislike the second solution...
Maybe would it be possible to make the module Random initialize itself alone
before the first call, in the next release...
[en francais]
J'aimerais initialiser le generateur aléatoire de nombre sans utiliser la
librairie Unix. Dans d'autres langage, j'ai l'habitude d'utiliser l'heure pour
ca, mais, apparament, c'est impossible sans la librairie Unix (ce qui n'est
pas portable, oblige a recompiler le top-level et complique la ligne de
compilation des compilateurs)
Quant à l'idée de demander une graine à l'utilisateur, je trouve ca un peu
préhistorique, comme approche. ;)
A mon avis, le plus simple serait encore que le module Random se charge tout
seul comme un grand de s'initialiser sans demander a l'utilisateur de le faire.
(dans une prochaine version ?)
Merci & thanks,
Martin.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Events, method iterator
[not found] <4.1.19990531191134.00939130@email.alpcom.it>
@ 1999-05-31 20:17 ` Markus Mottl
0 siblings, 0 replies; 5+ messages in thread
From: Markus Mottl @ 1999-05-31 20:17 UTC (permalink / raw)
To: Ubik; +Cc: OCAML
> >> There's no way to define an
> >> explicit function to send messages to objects ?
> >
> >Yes, there is: just use unbound (=lambda-) functions. E.g.:
> >
> > List.iter (fun obj -> obj#paint) _objs
>
> Yeah, this is a step in the direction I intended, thanks, but I think i
> can't write a sort of
>
> let send msg obj = obj#msg (* I know this won't work *)
Yes, this won't work, because OCAML relies on static type checking only
(I think this is good). It is impossible to decide at compile time
whether all objects "obj" passed to "send" really implement message "msg".
> to be used like this :
> ...
> List.iter (send paint) _objs
>
> ...
> List.iter (send (moveRel 20 20)) _objs
Two possibilities:
You can define an algebraic data type of messages. E.g.
type action = Paint
| MoveRel of int * int
let send action obj = match action with
| Paint -> obj#paint
| MoveRel (x,y) -> obj#move_rel x y
class graph_obj name = object
method paint = Printf.printf "%s#paint\n" name
method move_rel x y = Printf.printf "%s#move_rel %d %d\n" name x y
end
let _ =
List.iter (send (MoveRel(20,20)))
[new graph_obj "first"; new graph_obj "second"]
But I prefer the version, where you have to define the functions (shorter
and clearer):
let paint obj = obj#paint
let move_rel x y obj = obj#move_rel x y
class graph_obj name = object
method paint = Printf.printf "%s#paint\n" name
method move_rel x y = Printf.printf "%s#move_rel %d %d\n" name x y
end
let _ =
List.iter (move_rel 20 20) [new graph_obj "first"; new graph_obj "second"]
This is hardly any effort and works just fine.
Don't forget that functions are first class values in OCAML!! You can
pass them around easily (including functions that encapsulate method
calls)! E.g.:
let paint obj = obj#paint
let move_rel x y obj = obj#move_rel x y
class graph_obj name = object
val mutable x = 0
val mutable y = 0
method paint = Printf.printf "%s#paint\n" name
method move_rel dx dy =
x <- x + dx; y <- y + dy;
Printf.printf "%s#move_rel %d %d -> x:%d y:%d\n" name dx dy x y
end
let _ =
let obj_lst = [new graph_obj "first"; new graph_obj "second"]
and msg_lst = [move_rel 42 10; move_rel (-5) 7; paint] in
List.iter (fun msg -> List.iter msg obj_lst) msg_lst
Syntactically convenient enough? :-)
Well, I could imagine a slightly more convenient version if there were
some kind of operator which automatically creates a function that applies
a method to an object. Maybe something like:
List.iter (#move_rel 10 20) obj_lst
Another use:
let msg_lst = [#move_rel 42 10; #move_rel (-5) 7; #paint]
Thus, the encapsulating function does not have to be necessarily bound
(keeps the namespace tidy) and the programmer can see from the syntax
that it is methods that get invoked.
I don't know whether the implementors want to add such a kind of syntactic
sugar... any suggestions?
> I know the result is the same, but I prefer a solution like this,
> I like sintactic-clean code ...
Me, too. But I like semantically correct code even more - and thus I
don't want to have my programs crash, just because a message is passed
to an object, which can't handle it. But as you can see, the present
concept is just fine...
Best regards,
Markus
--
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~1999-06-01 15:22 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-05-26 12:07 Random.int on non unix platform Damien Doligez
1999-06-28 22:35 ` Martin Quinson
1999-05-30 19:44 ` Events, method iterator Ubik
1999-06-01 9:39 ` Jerome Vouillon
[not found] <4.1.19990531191134.00939130@email.alpcom.it>
1999-05-31 20:17 ` Markus Mottl
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox