[Functional Programming] Function modelling -- 4. Reader Monda example

const Reader = run => ({
  run,
  map: f => Reader(x => f(run(x))),
  chain: f => Reader(x => f(run(x)).run(x)),
  concat(o) {
    return Reader(x => run(x).concat(o.run(x)));
  }
});
Reader.of = x => Reader(() => x);
Reader.ask = Reader(x => x);

const prefix = s => m => `${s}${m}`;
const prefixHttps = prefix("https://");
const prefixHttp = prefix("http://");

const res = Reader.of("localhost")
  .chain(host =>
    Reader.ask.map(config =>
      config.https ? prefixHttps(host) : prefixHttp(host)
    )
  )
  .chain(domain =>
    Reader.ask.map(config => `${domain.concat(":").concat(config.port)}`)
  )
  .run({ port: 3000, https: true });

console.log(res); // https://localhost:3000

 

posted @ 2020-03-14 05:07  Zhentiw  阅读(143)  评论(0编辑  收藏  举报