You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Haskell Update Ecosystem (aka Hue) is a library to work with async and parallel operation in Haskell where IO operarions should be executed in parallel and their result concurrently. Providing a pure and side-effect free ecosystem.
To exemplify the Hue usage I'll describe a simple chat service where people can receive and send message to each other. To simplify this example, messages are not stored.
Like Elm, Redux and other libraries/frameworks that has an update system, Hue has an application state and updater.
Updater
The updater receive a message and returns a new state. In our context, we have a couple of messages. The first is sent when an user is waiting for a new message and the second is to clean the list of "waiters" when a message is sent for them.
dataAppMsg=CleanWaiterList
| AddWaiterHueContext
PS: Don't worry! The HueContext be explained soon. In this context, just assume that it's the user that is waiting for a new message.
dataAppState=State{waiters:: [HueContext] }updater::AppMsg->AppState->AppState
updater msg state =case msg ofCleanWaiterList->
state { waiters =[] }
AddWaiter user
state { waiters = waitingList }
where waitingList = [user] ++ (waiters state)
So as described above the updater receive a message, the current state and return a new state.
The actor
As the updater an actor receive a message (in this case an action). It can perform IO operation and "talk with" the "external world". To make it simple to understand let's to create an endpoint using the scotty library where users will waiting for new messages. We'll use the long polling mechanism, so the endpoint will block the request until a message is sent.
actor::AppAction->HueContext->AppState->HueIterationAppStateAppResult
actor action context state =case action ofGetMessage->
hueStateChange $AddWaiter context
So, when the actor is called with the GetMessage, it calls a state change adding the actual "user" (context) in the list of waiters. The stateChange function returns a HueIteration that is an internal Hue structure that defines what kind of operation should be executed internally.
The hueDispatch will block the execution until the application responds with a result.
The execution context
When we call the hueDispatch a context is created. This is the way that the actor has to comunicate with its action dispatcher. Let's a simple example. When we have a new message we can send it using the hueRespond passing the given context method:
hueRespond context $GetMessageResult message
The hueRespond responds to dispatcher the action result.
So, the result of the IO operation is "lifted" by the Either Monad. When the IO operation is completed with success (Right) or by an error (Left), Hue generates the Either result and uses it to create a message that will be used as actor call.
Step chain
In this section we will go through the most important part of our chat: The message delivery. And we will see how to implement step chains.
Look up the |> function. It is like a javascript Promise.then, in other words, it execute the steps from left to right. The main difference between the semicolon is that the step should be a function that receives a updated copy of the state.
If the next step does not need the updated version of the state, you should use the >>> operator. This is pretty close of |> the difference is that the >>> unifies two HueIterations.
Haskell Update Ecosystem (aka Hue) is a library to work with async and parallel operation in Haskell where
IO
operarions should be executed in parallel and their result concurrently. Providing a pure and side-effect free ecosystem.To exemplify the Hue usage I'll describe a simple chat service where people can receive and send message to each other. To simplify this example, messages are not stored.
Like
Elm
,Redux
and other libraries/frameworks that has an update system, Hue has an application state and updater.Updater
The updater receive a message and returns a new state. In our context, we have a couple of messages. The first is sent when an user is waiting for a new message and the second is to clean the list of "waiters" when a message is sent for them.
PS: Don't worry! The
HueContext
be explained soon. In this context, just assume that it's the user that is waiting for a new message.So as described above the updater receive a message, the current state and return a new state.
The actor
As the
updater
anactor
receive a message (in this case an action). It can performIO
operation and "talk with" the "external world". To make it simple to understand let's to create an endpoint using the scotty library where users will waiting for new messages. We'll use the long polling mechanism, so the endpoint will block the request until a message is sent.So, when the
actor
is called with theGetMessage
, it calls a state change adding the actual "user" (context) in the list of waiters. ThestateChange
function returns aHueIteration
that is an internal Hue structure that defines what kind of operation should be executed internally.The
hueDispatch
will block the execution until the application responds with a result.The execution context
When we call the
hueDispatch
a context is created. This is the way that theactor
has to comunicate with its action dispatcher. Let's a simple example. When we have a new message we can send it using thehueRespond
passing the given context method:The
hueRespond
responds todispatcher
the action result.Perform IO operation
Let's see the
huePerform
type:So, the result of the
IO
operation is "lifted" by theEither Monad
. When theIO
operation is completed with success (Right) or by an error (Left), Hue generates theEither
result and uses it to create a message that will be used asactor
call.Step chain
In this section we will go through the most important part of our chat: The message delivery. And we will see how to implement step chains.
Follow the full implementation of the our
actor
:Look up the
|>
function. It is like a javascriptPromise.then
, in other words, it execute the steps from left to right. The main difference between the semicolon is that the step should be a function that receives a updated copy of the state.The
|>
unifies an iteration and a step and return a new iteration that will be executed in the hue ecosystem.If the next step does not need the updated version of the state, you should use the
>>>
operator. This is pretty close of|>
the difference is that the>>>
unifies twoHueIterations
.You can see the complete example here.
The text was updated successfully, but these errors were encountered: