zoukankan      html  css  js  c++  java
  • [Functional Programming] Function modelling -- 10. Free Monads

    Path: Compose Functors -> Monad Transformers -> Free Monad

    Free monads, it provides a way to modelling functions as Data type. So composing / chaining data types to form the application.

    We also need something called `interpret` which can automaticlly do partten matching and do actual tasks.

    Read more: https://medium.com/@drboolean/free-er-monads-in-js-f5a59e7abc82

    https://medium.com/@drboolean/free-er-monads-in-js-pt2-8ab3b9efa510

    So, part one, how to modelling function as data type:

    const { liftF } = require("../free");
    const { Id } = require("../types");
    const { taggedSum } = require("daggy");
    
    const Http = taggedSum("Http", {
      Get: ["url"],
      Post: ["url", "body"],
    });
    
    // Free Http Get
    const httpGet = (url) => liftF(Http.Get(url));
    // Free Http Post
    const httpPost = (url, body) => liftF(Http.Post(url, body));

    Using 'daggy' to build Http object.

    So if you call:

    Http.Get('/home') // {url: '/home'}

    Then compose to an application:

    const app = () =>
      httpGet("/home").chain((contents) => httpPost("/analytics", contents));
    
    const res = app().foldMap(interpret, Id.of);

    Last thing is the 'interpret':

    const interpret = (x) =>
      x.cata({
        Get: (url) => Id.of(`contents for ${url}`),
        Post: (url, body) => Id.of(`Post ${body} to ${url}`),
      });

    --

    Full code:

    const { liftF } = require("../free");
    const { Id } = require("../types");
    const { taggedSum } = require("daggy");
    
    /**
     * Data type to repersent function:
     *
     *
     * httpGet = url => HttpGet(url)
     *
     * HttpGet(url)
     *  .chain(contents => HttpPost('/ayalytics', contents))
     *
     */
    
    const Http = taggedSum("Http", {
      Get: ["url"],
      Post: ["url", "body"],
    });
    
    // Free Http Get
    const httpGet = (url) => liftF(Http.Get(url));
    // Free Http Post
    const httpPost = (url, body) => liftF(Http.Post(url, body));
    
    const interpret = (x) =>
      x.cata({
        Get: (url) => Id.of(`contents for ${url}`),
        Post: (url, body) => Id.of(`Post ${body} to ${url}`),
      });
    
    const app = () =>
      httpGet("/home").chain((contents) => httpPost("/analytics", contents));
    
    const res = app().foldMap(interpret, Id.of);
    
    console.log(res.extract());
  • 相关阅读:
    linux下文件的复制、移动与删除
    Hbase万亿级存储性能优化总结-配置
    hbase 读写和优化
    hive数据倾斜定位及处理
    flink初识及安装flink standalone集群
    【Linux】用less查看日志文件
    sqoop的详细使用及原理
    HBase删除数据的原理
    hbase数据加盐(Salting)存储与协处理器查询数据的方法
    Hbase内存磁盘大致关系
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13219256.html
Copyright © 2011-2022 走看看