zoukankan      html  css  js  c++  java
  • [Javascript] Monads

    Monads allow you to nest computations. They are a pointed functor that adds mjoin and chain functions to combine other functors. Brian shows a number of code examples of different monads in action.

    functions: "mjoin", "chain"

    mjoin:

    var setSearchInput = function(x){ return ("#input").val(x); }.toIO()
    var getSearchTerm = function(){ return getParam("term", location.search) }.toIO()
    var initSearchForm = compose(mjoin, map(setSearchInput),  getSearchTerm)
    
    initSearchForm()
    //=> IO(Dom)
    
    runIO(initSearchForm())

    "getSearchTerm" return an IO, so in "setSearchInput" we need to use map(), but itself will return another IO, so the result is IO(IO()), then we apply mjoin, the result will be "IO()".

    chain:

    var chain = function(f){
     return compose(mjoin, map(f))
    }
    var setSearchInput = function(x){ return ("#input").val(x); }.toIO()
    var getSearchTerm = function(){ return getParam("term", location.search) }.toIO()
    var initSearchForm = compose(chain(setSearchInput),  getSearchTerm)
    
    initSearchForm()
    //=> IO(Dom)
    
    runIO(initSearchForm())
    var sendToServer = httpGet('/upload')
    var uploadFromFile = compose(chain(sendToServer), chain(readFile), askUser)
    
    uploadFromFile('what file?').fork(logErr, alertSuccess)
    requirejs.config({
      shim: {},
      paths: {
        domReady: 'https://cdnjs.cloudflare.com/ajax/libs/require-domReady/2.0.1/domReady.min',
        ramda: '//cdnjs.cloudflare.com/ajax/libs/ramda/0.8.0/ramda.min',
        maybe: 'http://looprecur.com/hostedjs/v2/maybe',
        io: 'http://looprecur.com/hostedjs/v2/io',
        future: 'http://looprecur.com/hostedjs/v2/data.future.umd',
        hcjs: 'http://looprecur.com/hostedjs/v2/hcjs'
      }
    });
    
    require(
      [
        'ramda',
        'maybe',
        'io',
        'future',
        'hcjs',
        'domReady!'
      ],
      function (_, Maybe, io, Future) {
        console.clear();
    
    
        var runIO = io.runIO;
    
    
    
        // Exercise 1
        // ==========
        // Use safeGet and mjoin or chain to safetly get the street name
        console.log("--------Start exercise 1--------");
    
        var safeGet = _.curry(function (x, o) {
          return Maybe(o[x]);
        });
        var user = {
          id: 2,
          name: "Albert",
          address: {
            street: {
              number: 22,
              name: 'Walnut St'
            }
          }
        };
        function log (x){
          console.log(x.toString());
          return x;
        }
    
        var ex1 = compose(mjoin, map(safeGet('name')) ,mjoin, map(safeGet('street')) ,safeGet('address'));
        var ex1 = compose(chain(safeGet('name')), chain(safeGet('street')), safeGet('address'));
        assertEqual(Maybe('Walnut St'), ex1(user));
        console.log("exercise 1...ok!");
    
    
    
    
        // Exercise 2
        // ==========
        // Use monads to get the href, then purely log it.
    
        console.log("--------Start exercise 2--------");
    
        var getHref = function () {
          return location.href;
        }.toIO();
        var pureLog = function (x) {
          console.log(x);
          return x;
        }.toIO();
    
        var ex2 = compose(chain(pureLog), getHref);
    
        assertEqual("http://run.jsbin.com/runner", runIO(ex2(null)));
        console.log("exercise 2...ok!");
    
    
    
    
        // Exercise 3
        // ==========
        // Use monads to first get the Post with getPost(), then pass it's id in to getComments().
        console.log("--------Start exercise 3--------");
    
        var ex3 = compose(chain(compose(getComments ,_.get('id'))) , getPost);
        var ex3 = compose(mjoin, map(compose(getComments, _.get('id'))), getPost)
            
        ex3(13).fork(log, function (res) {
          assertEqual(2, res.length);
          console.log("exercise 3...ok!");
        });
    
    
    
    
        // HELPERS
        // =====================
    
        function getPost(i) {
          return new Future(function (rej, res) {
            setTimeout(function () {
              res({
                id: i,
                title: 'Love them futures'
              });
            }, 300);
          });
        }
    
        function getComments(i) {
          return new Future(function (rej, res) {
            setTimeout(function () {
              res(["This class should be illegal", "Monads are like space burritos"]);
            }, 300);
          });
        }
    
      });
  • 相关阅读:
    JDK,JRE,JVM,三者,你知道它们的关系么
    Linux防火墙机制
    mysql数据可以连接到myeclipse当中需要知道的语法
    mysql数据库常用命令
    修改mysql默认编码重启后又还原,且在修改my.ini配置文件出现1067错误的解决办法。
    MySQL 5.5 Command Line Client 打开出现闪退(错误)的解决办法
    修改mysql 的初始密码
    linux命令的续行,linux shell 参数换行(标准说法:续行)
    java面试(反射)05
    java面试(进程和线程)04
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5863417.html
Copyright © 2011-2022 走看看