[Haskell-cafe] Re: Simple Fudgets-question

Thomas Hallgren hallgren at cse.ogi.edu
Mon Jan 26 15:38:07 EST 2004


Henrik Berg wrote:

>Thomas Hallgren <hallgren at cse.ogi.edu> writes:
>
>  
>
>>>... All I want to do is to resend the input out on the output.
>>>      
>>>
>>If that is all you want, this combinator is the right choice:
>>
>>	throughF :: F a b -> F a (Either b a)
>>    
>>
>
>Yes, but (Either b a) won't give me the input _together_ with the
>output, will it?  I need a fudget that accepts some input, does some
>work on it, and outputs the result _and_ the original input for
>further processing by other fudgets.  As far as I can see, this means
>I need a tupple (a, b), and not the disjoint sum (Either a b).
>  
>
Aha, I suspected you wanted something more :-)

Fudgets are asynchronous in nature, so there is no single right way to 
merge two streams into a stream of pairs, and I guess that is why there 
is no combinator for it in the library. Here is some code I found in an 
old example, illustrating one way to do it...

collectF :: F (Either a b) (a,b)
collectF = absF (collectSP Nothing Nothing)

collectSP :: Maybe a -> Maybe b -> SP (Either a b) (a,b)
collectSP x y =
  case (x,y) of
     (Just x,Just y) -> putSP (x,y) (collectSP Nothing Nothing)
     _ -> getSP (\msg ->
		case msg of
		   Right y -> collectSP x (Just y)
		   Left x -> collectSP (Just x) y
		)

-- This is how collectF was used:
--fileF = collectF>==<throughF fileDialogF>==<fileMenuF


-- 
Thomas H




More information about the Haskell-Cafe mailing list