caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* React.E.switch issue.
@ 2009-09-24  7:40 Guillaume Yziquel
  2009-09-24  8:01 ` Guillaume Yziquel
  2009-09-24 13:42 ` Daniel Bünzli
  0 siblings, 2 replies; 5+ messages in thread
From: Guillaume Yziquel @ 2009-09-24  7:40 UTC (permalink / raw)
  To: Daniel Bünzli; +Cc: caml-list

Hello.

Here is a piece of code that I am having issue. I'm trying to use React 
to parse reactively a server output. I have the following event definition:

>   let event_starting_at str = React.E.fold begin fun accu chunk -> accu^chunk end str received_event in 
>   let to_be_parsed_event_of_event, set_to_be_parsed_event_of_event = React.E.create () in
>   let to_be_parsed_event = React.E.switch (event_starting_at "") to_be_parsed_event_of_event in
>   let reinitialise_with unparsed_text = set_to_be_parsed_event_of_event (event_starting_at unparsed_text) in

String chunks from the server are received by the 'received_event' 
event, on the first line.

My issue is that the 'reinitialise_with' function is called in a 
function 'phi' which is used in the following way:

let message_event = React.E.map phi to_be_parsed_event.

phi is the parsing function, so I'm not reproducing it directly here. 
However, you would have the behaviour described further below with the 
following code:

let phi string_chunk =
   let new_string_chunk = String.copy string_chunk in
   reinitialise_with new_string_chunk;
   new_string_chunk

This creates hiccups in the events.

For example, at different time intervals, with to_be_parsed_event, I get:

	q
	qqw
	qqwwe
	qqwweer
	qqwweerrt

etc... where I'd like in fact to have

	q
	qw
	qwe
	qwer
	qwert

Advice would be appreciated on how to correct this.

All the best,

-- 
      Guillaume Yziquel
http://yziquel.homelinux.org/


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

* Re: React.E.switch issue.
  2009-09-24  7:40 React.E.switch issue Guillaume Yziquel
@ 2009-09-24  8:01 ` Guillaume Yziquel
  2009-09-24 13:42 ` Daniel Bünzli
  1 sibling, 0 replies; 5+ messages in thread
From: Guillaume Yziquel @ 2009-09-24  8:01 UTC (permalink / raw)
  To: Daniel Bünzli; +Cc: caml-list

Guillaume Yziquel a écrit :
> Hello.

Hi again.

> Here is a piece of code that I am having issue. I'm trying to use React 
> to parse reactively a server output. I have the following event definition:
> 
>>   let event_starting_at str = React.E.fold begin fun accu chunk -> 
>> accu^chunk end str received_event in   let 
>> to_be_parsed_event_of_event, set_to_be_parsed_event_of_event = 
>> React.E.create () in
>>   let to_be_parsed_event = React.E.switch (event_starting_at "") 
>> to_be_parsed_event_of_event in
>>   let reinitialise_with unparsed_text = 
>> set_to_be_parsed_event_of_event (event_starting_at unparsed_text) in

I've replaced the previous code by the following piece of code, and it 
now works...

>   let event_starting_at ?drop:(drop = false) str = React.E.fold begin fun accu chunk -> accu^chunk end str
>     begin match drop with | false -> received_event | true -> React.E.drop_once received_event end in 
>   let to_be_parsed_event_of_event, set_to_be_parsed_event_of_event = React.E.create () in
>   let to_be_parsed_event = React.E.switch (event_starting_at "") to_be_parsed_event_of_event in
>   let reinitialise_with unparsed_text = set_to_be_parsed_event_of_event
>     (event_starting_at ~drop: true unparsed_text) in

Now, is this a bug or a feature?

All the best,

Guillaume Yziquel.

-- 
      Guillaume Yziquel
http://yziquel.homelinux.org/


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

* Re: React.E.switch issue.
  2009-09-24  7:40 React.E.switch issue Guillaume Yziquel
  2009-09-24  8:01 ` Guillaume Yziquel
@ 2009-09-24 13:42 ` Daniel Bünzli
  2009-09-24 14:14   ` Guillaume Yziquel
  1 sibling, 1 reply; 5+ messages in thread
From: Daniel Bünzli @ 2009-09-24 13:42 UTC (permalink / raw)
  To: guillaume.yziquel; +Cc: caml-list

> My issue is that the 'reinitialise_with' function is called in a
> function 'phi' which is used in the following way:
>
> let message_event = React.E.map phi to_be_parsed_event.

Cannot really make sense out of your snippets of code. However this
phi functions seems to invoke a primitive event sending function and
that's explicitely prohibited by the documentation. Primitive event
sending/signal setting functions cannot be invoked as side effects
inside update cycles (see doc of E.create/S.create).

Not sure what you are trying to achieve but I suspect fixed point
combinators (E.fix/S.fix) may help you to solve your problem.

Best,

Daniel


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

* Re: React.E.switch issue.
  2009-09-24 13:42 ` Daniel Bünzli
@ 2009-09-24 14:14   ` Guillaume Yziquel
  2009-09-28 11:57     ` Daniel Bünzli
  0 siblings, 1 reply; 5+ messages in thread
From: Guillaume Yziquel @ 2009-09-24 14:14 UTC (permalink / raw)
  To: Daniel Bünzli; +Cc: caml-list

Daniel Bünzli a écrit :
>> My issue is that the 'reinitialise_with' function is called in a
>> function 'phi' which is used in the following way:
>>
>> let message_event = React.E.map phi to_be_parsed_event.
> 
> Cannot really make sense out of your snippets of code.

What I want to do is the following:

The server sends events with chunks of string that I want to parse.

I accumulate the string chunks (with React.E.fold) until I can parse 
some server message out of it.

When some message is parsed, I discard this part of the event, and I 
install another "accumulating event" (accumulating with React.E.fold).

In order to discard and renew the "accumulating event", I send an event 
to the event of event. This is why I use the React.E.switch construct.

The output of React.E.switch is an event that can hold, for example the 
following sequence of values:

My fi
My first mes
My first message. My s

(Here is where the event of event is renewed:)

My s
My secon
My second mess

etc...

> However this phi functions seems to invoke a primitive event sending function

Yes. phi is the function that parses the messages from the server. So it 
has to send an event to renew the "accumulating event".

> and that's explicitely prohibited by the documentation. Primitive event
> sending/signal setting functions cannot be invoked as side effects
> inside update cycles (see doc of E.create/S.create).

OK. So this is not a bug, nor a feature.

> Not sure what you are trying to achieve but I suspect fixed point
> combinators (E.fix/S.fix) may help you to solve your problem.

I tried to look into this, but I did not see how they could help me with 
my specific issue.

> Best,
> 
> Daniel

For now, I'm using a workaround, but I'll try to see how I can make this 
clean...

Many thanks for your answer.

-- 
      Guillaume Yziquel
http://yziquel.homelinux.org/


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

* Re: React.E.switch issue.
  2009-09-24 14:14   ` Guillaume Yziquel
@ 2009-09-28 11:57     ` Daniel Bünzli
  0 siblings, 0 replies; 5+ messages in thread
From: Daniel Bünzli @ 2009-09-28 11:57 UTC (permalink / raw)
  To: guillaume.yziquel; +Cc: caml-list

Sorry I'm travelling in a sparsely connected area. Besides I'm heading
to a region where it seems the internet was closed down by the local
government. If that is still the case I won't be able to respond for
some weeks.

Now quickly two things.

1) It seems to me that you are using events where signals should be
used. Signals should be used to represent "state". Your "accumulating
event" sounds like state to me, use a signal for that.

2) If you really want a primitive event (or signal) to generate new
_primitive_ event occurences (or signal updates), just wrap the call
to the event sending (or signal update) function in a thunk and store
it in a queue, once the update cycle is over dequeue the next thunk
and invoke it. Fixed point operators will in effect perform something
similar but preserve the applicative nature of your program -- however
I think that you are right in your case they won't help.

Now I really suggest you to think hard about 1). I'm sure you can get
a clean solution, something like use S.fold with the (primitive) event
of the server to accumulate the unparsed input and remember the last
parsed message.

Best,

Daniel


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

end of thread, other threads:[~2009-09-28 11:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-09-24  7:40 React.E.switch issue Guillaume Yziquel
2009-09-24  8:01 ` Guillaume Yziquel
2009-09-24 13:42 ` Daniel Bünzli
2009-09-24 14:14   ` Guillaume Yziquel
2009-09-28 11:57     ` Daniel Bünzli

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).