When building our stateful computations, there will come a time when we’ll need to combine two or more state transactions at the same time to come up with a new result. Usually this occurs when want to use plain ol’ JavaScript functions with two or more a arguments as part of our stateful computations.

We first look at how this can be accomplished by using chain and closure to get access to both functions. Then we will explore how we can leverage the of construction helper and the ap Stateinstance method to clean this type of interaction up a bit. We then conclude with an even cleaner approach that takes full advantage of a crocks helper function named liftA2.

For example, we have a function:

const namefiy = firstName => lastName => `${lastName}, ${firstName}`;

It should receive two params to return a string.

one way is using .ap(), it takes the same input, run with the given functions and return its value, then combine those:

var _getFullName = State.of(namefiy)
.ap(getFirstName)
.ap(getLastName)

Or we can use .liftA2, it lift the function into State automaticlly:

var getFullName = liftA2(
namefiy,
getFirstName,
getLastName
)

----

const { liftA2, composeK, Unit, curry, objOf, compose, State, mapProps, prop, option } = require("crocks");

const { put, get, modify } = State;

const namefiy = firstName => lastName => `${lastName}, ${firstName}`;
const getWord = number => name => name.split(' ')[number]; const getFirstName = get(getWord());
const getLastName = get(getWord()); var _getFullName = State.of(namefiy)
.ap(getFirstName)
.ap(getLastName) var getFullName = liftA2(
namefiy,
getFirstName,
getLastName
)
console.log(
getFullName
.evalWith("John Green")
)

[Functional Programming Monad] Apply Stateful Computations To Functions (.ap, .liftA2)的更多相关文章

  1. [Functional Programming Monad] Combine Stateful Computations Using Composition

    We explore a means to represent the combination of our stateful computations using familiar composit ...

  2. [Functional Programming Monad] Combine Stateful Computations Using A State Monad

    The true power of the State ADT really shows when we start combining our discrete, stateful transact ...

  3. [Functional Programming Monad] Refactor Stateful Code To Use A State Monad

    When we start to accumulate functions that all work on a given datatype, we end up creating a bunch ...

  4. [Functional Programming Monad] Map And Evaluate State With A Stateful Monad

    We explore our first stateful transaction, by devising a means to echo our state value into the resu ...

  5. [Functional Programming Monad] Substitute State Using Functions With A State Monad (get, evalWith)

    We take a closer look at the get construction helper and see how we can use it to lift a function th ...

  6. [Functional Programming Monad] Modify The State Of A State Monad

    Using put to update our state for a given state transaction can make it difficult to modify a given ...

  7. [Functional Programming] Monad

    Before we introduce what is Monad, first let's recap what is a pointed functor: A pointed functor is ...

  8. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

  9. Monad (functional programming)

    In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...

随机推荐

  1. Thinkphp命名规范

    1.类文件都是以.class.php为后缀(这里是指的ThinkPHP内部使用的类库文件,不代表外部加载的类库文件),使用驼峰法命名,并且首字母大写,例如 DbMysql.class.php: 2.类 ...

  2. java中的object... args参数

    关于java方法中Object... args参数的含义 在阅读google发布的volley源码时,突然看到一个方法中存在这样的写法,如 :v(String format, Object... ar ...

  3. HDU 1561 The more, The Better【树形DP/有依赖的分组背包】

    ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先 ...

  4. 洛谷P4145 上帝造题的七分钟2/花神游历各国 [树状数组,并查集]

    题目传送门 题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是 ...

  5. qwb与学姐 (带秩并查集)

    qwb与学姐 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 149  Solved: 54[Submit][Status][Web Board] Des ...

  6. 【hihoCoder 第133周】【hihoCoder 1467】2-SAT·hihoCoder音乐节

    http://hihocoder.com/problemset/problem/1467 2-sat模板...详细的题解请看题目里的提示. tarjan模板打错again致命伤qwq #include ...

  7. [USACO 2016 Dec Gold] Tutorial

    Link: 传送门 A: 贪心从小到大插入,用并查集维护连通性 #include <bits/stdc++.h> using namespace std; #define X first ...

  8. AGC 012 C - Tautonym Puzzle

    题面在这里! 神仙构造啊qwqwq. 窝一开始只想到一个字符串长度是 O(log(N)^2) 的做法:可以发现一段相同的长度为n的字符串的贡献是 2^(n-1)-1 ,可以把它看成类二进制,枚举用了多 ...

  9. 【折半枚举】Ural Championship April 30, 2017 Problem G. Glasses with solutions

    题意:有n杯盐溶液,给定每杯里面盐的质量以及盐溶液的质量.问你有多少种方案选择一个子集,使得集合里面的盐溶液倒到一个被子里面以后,浓度为A/B. 折半枚举,暴力搜索分界线一侧的答案数,跨越分界线的答案 ...

  10. 【动态规划+二分查找】POJ2533&POJ1631最长上升子序列(LIS)

    POJ2533裸的LIS,时间复杂度为O(n^2) #include<iostream> #include<cstdio> using namespace std; +; in ...