Version franaise
Home About Download Resources Contact us
Browse thread
Recursion on React.events.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Guillaume Yziquel <guillaume.yziquel@c...>
Subject: Re: Recursion on React.events.
Guillaume Yziquel a écrit :
> Hello.
> 
> I've been dabbling with recursive definition of React events.
> 
> Suppose I have a function called schedule of type
> 
>     val schedule : Calendar.t -> unit React.event
> 
> which throws out an event at the specified time or date.
> 
> I've been trying to create a regular_schedule function that throws 
> events at a regular pace. Daniel Bünzli's module is great, but sometimes 
> a bit rough to get by, specifically on examples such as this one.
> 
> So I came up with a recursive definition of such a React.event:
> 
>> let rec regular_schedule start_time period =
>>   React.E.switch React.E.never begin React.E.map
>>     begin fun () -> regular_schedule (Calendar.add (Calendar.now ()) 
>> period) period end
>>     begin schedule start_time end
>>   end
> 
> First question:
> 
> It almost works, in the sense that if you insert a print_endline after 
> the fun (), well, it indeeds prints stuff on stdin at the specified 
> pace. However, somehow, the event as a whole behaves as React.E.never. 
> So it doesn't work. I guess it has to do with the way React.switch works 
> synchronously, but I really do not get it.

OK. The following piece of code works out, but it seems to me that using 
a React.E.select in this position is a rather ugly hack.

let rec regular_schedule start period =
   let waiting_for = schedule start in
   React.E.switch React.E.never begin React.E.map
     begin fun () -> React.E.select [waiting_for; regular_schedule
       (Calendar.add (Calendar.now ()) period) period]
     end waiting_for
   end


> Second question:
> 
> Is there somehow a notion of 'tail recursion' for such constructs?

I gave some thought to it, and it seems quite plausible that what is 
done in the first place is analoguous to tail recursion. And that using 
React.E.select breaks the tail recursion. Am I offtrack?

> All the best,

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