<div dir="ltr"><div><div>Nobody on that?<br></div>What I really want is: a computation that can be interrupted if enough result is already there.<br></div>I would be surprise if that concept doesn't exist already somewhere :)<br>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Jun 19, 2014 at 5:17 PM, Corentin Dupont <span dir="ltr"><<a href="mailto:corentin.dupont@gmail.com" target="_blank">corentin.dupont@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div>Hi guys,<br></div><div>I am making a DSL for event composition (inspired from digestive-functor & reactive-banana) and I find myself wanting a primitive like that:<br>
<span style="font-family:courier new,monospace"><br>
-- given a list of events, evaluate the function on those that have already fired.<br>-- returns an event yelding the result when ready.<br>ListEvent  :: [Event a] -> ([a] -> Maybe b) -> Event b  </span><br><br>
</div>
Does this type tell you something? Is there a better abstraction that can encompass it?<br></div><div>My DSL is like that:<br><br></div><div><div><span style="font-family:courier new,monospace">-- | Composable events<br>
data Event a where<br>
   SumEvent   :: Event a -> Event a -> Event a        -- The first event to fire will be returned<br>   AppEvent   :: Event (a -> b) -> Event a -> Event b -- Both events should fire, and then the result is returned<br>

   PureEvent  :: a -> Event a                         -- Create a fake event. The result is useable with no delay.<br>   EmptyEvent :: Event a                              -- An event that is never fired.<br>   BaseEvent  :: (Typeable a) => Field a -> Event a   -- Embed a base event</span><br>

<br></div><div>It's easy to define instances for Applicative and Alternative.<br></div><div>But then I have a use case that I cannot solve:<br></div><div>In the case of votations (which use this DSL), sometime you can end prematuraly the voting, even if not everybody voted yet: for example if the majority is already reached.<br>

</div><div>In this case no need to wait for everybody, we can end the vote.<br></div><div><br>This does not seem to be possible with Applicative or Monad interfaces: things have to be sequenced in a monad, they cannot be evaluated in parralel.<br>

</div><div>For example:<br><span style="font-family:courier new,monospace"><br></span></div><div><span style="font-family:courier new,monospace">do<br></span></div><div><span style="font-family:courier new,monospace">   v1 <- getVote1<br>

</span></div><div><span style="font-family:courier new,monospace">   v2 <- getVote2<br></span></div><div><span style="font-family:courier new,monospace">   v3 <- getVote3<br></span></div><div><span style="font-family:courier new,monospace">   evalMajority(v1, v2, v3)</span><br>

<br></div><div>In this example I have to wait for all 3 votes to be completed, but in fact only two would suffice to achieve majority.<br></div><div>That's why I added "ListEvent" to the DSL above which allow to achieve that but I'm wondering if there is a better/bigger abstraction to put it in.<br>

<br></div><div>Thanks!!<span class="HOEnZb"><font color="#888888"><br></font></span></div><span class="HOEnZb"><font color="#888888"><div>Corentin<br></div><div><br></div></font></span></div></div>
</blockquote></div><br></div>