zoukankan      html  css  js  c++  java
  • [Functional Programming] Function modelling -- 5. Endo Monoid

    Endo:

    It takes a type as string and output is the same type. It's concat methods works like composion. All the transform functions on the same input.

    const { List } = require("immutable-ext");
    
    const toUpper = s => s.toUpperCase();
    const exclaim = s => `${s}!`;
    
    // Endo functor
    // a -> a
    // string -> string
    const Endo = run => ({
      run,
      concat(otherM) {
        return Endo(x => run(otherM.run(x)));
      }
    });
    Endo.empty = () => Endo(x => x);
    
    const res = List([toUpper, exclaim])
      .foldMap(Endo, Endo.empty())
      .run("hello"); // HELLO!
  • 相关阅读:
    Python Day7(相关补充)
    Python Day7
    Python Day6
    Python Day5
    Python Day4
    Python Day3
    Python Day2
    Python Day1
    复杂装饰器原理分析
    Unity 坐标
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12499023.html
Copyright © 2011-2022 走看看