The Eff monad implemented in Flow

type DB = { type: 'DB' };type User = {
username: string,
uid: number
};
function createUser(username: string): Eff<{ write: DB }, User> {

}
function lookupUser(username: string): Eff<{ read: DB }, ?User> {

}
const createThenLookupUser = username => createUser(username)
.chain(user => lookupUser(user.username))
$> flow suggest index.jsconst createThenLookupUser = username => createUser(username).chain(user: {uid: number, username: string} => lookupUser(user.uid): Eff<{read: {type: ‘DB’}}, ?{uid: number, username: string}>): Eff<{write: {type: ‘DB’}} & {read: {type: ‘DB’}}, ?{uid: number, username: string}>

(username: string) => Eff<{ write: DB, read: DB }, ?User>

Whitelisting side effects

type Method = 'GET' | 'POST';// web request
type Req = {
body: string,
header: string,
method: Method
};
// web response
type Res = {
body: string,
status: number
};
function signupPage(req: Req): Eff<{ write: DB, read: DB }, Res> {
const username = req.header
return lookupUser(username).chain(user => {
if (user) {
return new Eff(() => ({
body : 'A user with this username already exists!',
status : 400
}))
}
return createUser(username).map(() => ({
body : 'Created User with name ' + username,
status: 200
}))
})
}
type GetRoute = {
type: ‘GetRoute’,
path: string,
handler: (req: Req) => Eff<{ read: DB }, Res>
};
type PostRoute = {
type: ‘PostRoute’,
path: string,
handler: (req: Req) => Eff<{ read: DB, write: DB }, Res>
};
type Route = GetRoute | PostRoute;

const routes: Array<Route> = [
{ type: 'GetRoute', path: '/signup', handler: signupPage }
]
{ type: 'GetRoute', path: '/signup', handler: signupPage }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This type is incompatible with union: GetRoute | PostRoute
property `write`. Property not found inhandler: (req: Req) => Eff<{ read: DB }, Res>
^^^^^^^^^^^^ object type
const routes: Array<Route> = [
{ type: 'PostRoute', path: '/signup', handler: signupPage }
]

The Eff monad

--

--

mathematician and rock climber

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store