Skip to content

Instantly share code, notes, and snippets.

@AcidLeroy
Last active July 9, 2018 23:09
Show Gist options
  • Save AcidLeroy/7808a3bde1b5589b7fd120abf3842dac to your computer and use it in GitHub Desktop.
Save AcidLeroy/7808a3bde1b5589b7fd120abf3842dac to your computer and use it in GitHub Desktop.
Exploring Functions
const {Either, IO} = require('monet')
const {compose, map} = require('ramda')
// Pretend to get a database entry
// getUser :: String -> IO ( JSON )
var getUser = username => IO(() => (username === 'Marge') ? {name: 'Marge Simpson', address: '123 Fake St'} : {error: 'user ' + username + ' not found!'})
// Pretend to upload some information
// uploadUser :: JSON -> IO ()
var uploadUser = user => IO(() => console.log('Uploading user: ', user.name, ' at address', user.address))
// Validate that we got a good entry from the database
// validateOutput :: JSON -> Either String JSON
const validateOutput = x => x.name ? Either.Right(x) : Either.Left('The user could not be found')
// Compose functions
var getAndPost = compose(
map(map(uploadUser)), // returns an IO(Either(IO))
map(validateOutput), // Returns an Either(IO)
getUser) // Returns an IO
var homer = getAndPost('Homer')
var marge = getAndPost('Marge')
console.log('Homer = ', homer.run()) // Homer = { isRightValue: false, value: 'The user could not be found' }
console.log('Marge = ', marge.run().join().run()) // Uploading user: Marge Simpson at address 123 Fake St\nMarge=undefined
debugger;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment