zoukankan      html  css  js  c++  java
  • [Functional Programming] liftA2 and converge

    Sometimes I am confused with 'liftA2' and 'converge' functions. 

    Main difference between those is that:

      liftA2 takes applicative functor as second and third arguements, for example, array

      converge takes functions as second and third arguements

    // liftA2 :: Applicative m => (a -> b -> c) -> m a -> m b -> m c
    // converge :: (b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d
    // liftA2 :: Applicative m => (a -> b -> c) -> m a -> m b -> m c
    // converge :: (b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d
    const { option, curry, liftA2, converge } = require("crocks");
    const { getState } = require("../../helper");
    
    // getColors :: () -> State AppState [String]
    const getColors = () => getState("colors").map(option([]));
    
    // getShapes :: () -> State AppState [String]
    const getShapes = () => getState("shapes").map(option([]));
    
    // buildCard :: String -> String -> Card
    const buildCard = curry((color, shape) => ({
      id: `${color}-${shape}`,
      color,
      shape
    }));
    
    // buildCards :: [String] -> [String] -> [Card]
    const buildCards = liftA2(buildCard);
    
    // generateCards :: () -> State AppState [ Card ]
    const generateCards = converge(liftA2(buildCards), getColors, getShapes);
    
    module.exports = {
      generateCards
    };
  • 相关阅读:
    Spring IoC和AOP使用扩展(二)
    Spring核心概念(一)
    MyBatis的动态SQL(五)
    MyBatis的SQL映射文件(四)
    初始myBatis(三)
    初始myBatis(二)
    微信小程序学习九 事件系统
    微信小程序学习八 wxs
    微信小程序学习七 视图层wxml语法
    微信小程序学习六 模块化
  • 原文地址:https://www.cnblogs.com/Answer1215/p/11308513.html
Copyright © 2011-2022 走看看