transient-haskell / transient

A full stack, reactive architecture for general purpose programming. Algebraic and monadically composable primitives for concurrency, parallelism, event handling, transactions, multithreading, Web, and distributed computing with complete de-inversion of control (No callbacks, no blocking, pure state)
MIT License
631 stars 28 forks source link

`profits` example require a final `price` event #8

Closed louispan closed 8 years ago

louispan commented 8 years ago

I'm not sure if this is the right place to submit this issue, but I notice that the profits example on fpcomplete require a final price event in the eventList in order to print out the total.

For example, if I changed the eventList to

eventList =
    [ Event "quantity" 10
    , Event "price"     2
    , Event "price"     3
    , Event "quantity" 30
    ]

The the output is

new event: quantity
quantity=10

new event: price
price=2

total=20

new event: price
price=3

total=30

new event: quantity
quantity=30

END

That is, there is no final total=90 emitted.

Is this the expected behaviour?

agocorona commented 8 years ago

Hi Louis, I think that this is the intended behaviour in THAT example. To have what you intend, it is a matter of using applicative :

  total=(*) <$> getEvent "qantitity" <*> getEvent "price"

In this case, the total is calculated either a new quantity or price arrives

The monad is sequential, while the applicative execute the two event getters in parallel

But in this concrete example the monadic case is the appropriate. But hmmm I see that price should be before quantity because it does not make sense when a new price per unit arrives to calculate a total with the last quantity of product sold since that quantity corresponded to the old price, it should be with a new quantity.