[Functional Programming Monad] Apply Stateful Computations To Functions (.ap, .liftA2)
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
State
instance 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)的更多相关文章
- [Functional Programming Monad] Combine Stateful Computations Using Composition
We explore a means to represent the combination of our stateful computations using familiar composit ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [Functional Programming] Monad
Before we introduce what is Monad, first let's recap what is a pointed functor: A pointed functor is ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- Monad (functional programming)
In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...
随机推荐
- Qt精简编译方法总结
原文请看:http://blog.csdn.net/loaden/article/details/6061702 Qt如果采取默认编译安装,一般都要占用上G的空间.当初自己不想涉及Qt的一个原因,就是 ...
- Python:使用正则去除HTML标签(转)
利用正则式处理,不知道会不会有性能问题,没有经过太多测试. 目前我有很多还是使用BeautifulSoup进行这种处理. HTML实体处理的只是用于处理一些常用的实体. # -*- coding: u ...
- HDU 4348.To the moon SPOJ - TTM To the moon -可持久化线段树(带修改在线区间更新(增减)、区间求和、查询历史版本、回退到历史版本、延时标记不下放(空间优化))
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- vue中keep-alive
vue2.0提供了一个keep-alive组件用来缓存组件,避免多次加载相应的组件,减少性能消耗 1.基本用法,缓存整个页面或组件 <keep-alive> <component&g ...
- 线性基【CF845G】Shortest Path Problem?
Description 给定一张 \(n\) 个点 \(m\) 条边的无向图,一开始你在点 \(1\),且价值为 \(0\) 每次你可以选择一个相邻的点,然后走过去,并将价值异或上该边权 如果在点 \ ...
- 洛谷——P2908 [USACO08OPEN]文字的力量Word Power
P2908 [USACO08OPEN]文字的力量Word Power 题目描述 Farmer John wants to evaluate the quality of the names of hi ...
- 50 years, 50 colors HDU - 1498(最小点覆盖或者说最小顶点匹配)
On Octorber 21st, HDU 50-year-celebration, 50-color balloons floating around the campus, it's so nic ...
- 【map】【分解质因数】CDOJ1572 Espec1al Triple
先把公比为1,即前项 中项 末项相同的统计出来.对每一类数C(n,3)即可. 然后我们发现,因为a1*a3=(a2)^2,所以a1和a3进行质因子分解之后,每一个质因子的指数的奇偶性必然相同,否则无法 ...
- [NOIp2016提高组]蚯蚓
题目大意: 给你n个不同长度蚯蚓,每秒从里面取出最长的砍下u/v变成两只,又把剩下的加长q. 问你在m之前的t,2t,3t...的时间上,砍的蚯蚓长度, 以及m秒后剩下所有的蚯蚓长度. 思路: 很容易 ...
- [NOIp2016提高组]组合数问题
题目大意: 给定n,m和k,对于所有的0<=i<=n,0<=j<=min(i,m)有多少对(i,j)满足C(j,i)是k的倍数. 思路: 先预处理出组合数,再预处理一下能整除个 ...