Last active
October 28, 2020 22:25
-
-
Save dypsilon/b5c9c72850a721e271fcf2afe3baf954 to your computer and use it in GitHub Desktop.
A simple example of using Reader transformer to combine reader and future.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {tagged} = require('daggy'); | |
// const | |
const K = (a) => (b) => a; | |
// Reader Transformer | |
module.exports = (M) => { | |
const ReaderT = tagged('run'); | |
ReaderT.lift = (m) => ReaderT(K(m)); | |
ReaderT.of = ReaderT.prototype.of = (a) => { | |
return ReaderT((e) => M.of(a)); | |
}; | |
ReaderT.ask = ReaderT((e) => M.of(e)); | |
ReaderT.prototype.chain = function(f) { | |
return ReaderT((e) => { | |
return this.run(e).chain((a) => f(a).run(e)); | |
}); | |
}; | |
ReaderT.prototype.map = function(f) { | |
var m = this; | |
return m.chain(a => m.of(f(a))) | |
} | |
return ReaderT; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This short program will encrypt the user password | |
* and insert a new record into a mock database. | |
*/ | |
const ReaderT = require('./lib/reader-t'); | |
const Future = require('fluture'); | |
const R = require('ramda'); | |
const crypto = require('crypto'); | |
const FutureReader = ReaderT(Future); | |
// our mock database | |
const database = [ | |
{ email: '[email protected]', password: 'e0538fd8f022bb3b139d72cf12766cb0e31690ff' }, | |
{ email: '[email protected]', password: '42c4fbf6fec201c66b82c97833b08d936d2cd526' } | |
] | |
// creates a statefull database connection | |
const connectTo = (db) => { | |
return { | |
insert: (doc) => Future.of(db.push(doc)), | |
get: (i) => Future.of(db[i]), | |
delete: (i) => Future.of(db.splice(i, 1)), | |
list: () => Future.of(db) | |
} | |
} | |
// some utility functions | |
const encrypt = (i) => crypto.createHash('sha1').update(i).digest('hex'); | |
const encPassword = R.evolve({password: encrypt}) | |
// utility function which makes it easier | |
// to ask for some dependency | |
// dependencies should reside in an object { dep: obj } | |
const inject = R.curry((dep, f, reader) => { | |
return reader.chain((v) => { | |
return FutureReader.ask.chain((env) => { | |
return FutureReader.lift(f(env[dep], v)); | |
}); | |
}); | |
}); | |
// this is how you access the db connection inside the reader | |
const save = (db, user) => { | |
return db.insert(user).chain(_ => db.list()); | |
} | |
// the body of the program | |
const handler = R.pipe( | |
R.map(encPassword), | |
inject('db', FutureReader.lift(save)), | |
R.map(R.map(R.dissoc('password'))) | |
); | |
// this is our db connection now | |
const dbCon = connectTo(database); | |
const request = { email: '[email protected]', password: 'secret' }; | |
const result = handler(FutureReader.of(request)) | |
.run({ 'db': dbCon }) // this is how you pass the db connection in | |
.fork(console.error, console.log); // now fork the future |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment