加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

[Functional Programming] liftA2 and converge

发布时间:2020-12-13 23:31:35 所属栏目:Linux 来源:网络整理
导读: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 ar

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
};

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读