Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Understanding the reader monad #160

Open
ghost opened this issue Mar 13, 2018 · 2 comments
Open

Understanding the reader monad #160

ghost opened this issue Mar 13, 2018 · 2 comments

Comments

@ghost
Copy link

ghost commented Mar 13, 2018

After reading the docs, I was still trying to understand the purpose of Reader monad.

import { Reader } from 'monet'

interface Config {
  greeting: string
  surround: string
}

function greetName(name: string): Reader<Config, string> {
  return Reader(config => `${config.greeting} ${name}`)
}

function surround(input: string): Reader<Config, string> {
  return Reader(config => `${config.surround}${input}${config.surround}`)
}

function render(name: string) {
  return greetName(name).flatMap(surround)
}

console.log(render('Tom').run({ greeting: 'Hello', surround: '~' }))

I understand the render function doesn't have to make reference to the config, but it still feels like additional boilerplate over currying. Is there a better use of the reader monad?

@ulfryk
Copy link
Member

ulfryk commented Nov 17, 2018

@khoomeister one of basic examples is communication with db - you can create a bunch of platform agnostic operations on db and then pass proper driver when calling run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants