[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 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)的更多相关文章
- [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, ...
随机推荐
- hdu 多校第一场
1001 思路:打表可以发现只有3|n 和 4|n 的情况有解,判一下就好啦. #include<bits/stdc++.h> #define LL long long #define f ...
- CentOS 6.7下配置 yum 安装 Nginx
CentOS 6.7下配置 yum 安装 Nginx. 转载:http://www.linuxidc.com/Linux/2016-07/133283.htm 第一步,在/etc/yum.repos. ...
- 最小生成树算法详解(prim+kruskal)
最小生成树概念: 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边. 最小生成树可以用kruskal(克鲁斯卡尔)算法或prim(普里 ...
- 转:windbg常用命令
- CodeForces888E Maximum Subsequence(折半枚举+two-pointers)
题意 给定一个包含\(n\)个数的序列\(a\),在其中任选若干个数,使得他们的和对\(m\)取模后最大.(\(n\leq 35\)) 题解 显然,\(2^n\)的暴枚是不现实的...,于是我们想到了 ...
- Flask实战第47天:首页导航条首先和代码抽离
新建一个前台页面的父模板front_base.html 导航条是总boostrap v3中文站拷贝过来的,然后根据自己的需求做一些修改 <!DOCTYPE html> <html l ...
- RabbitMQ (十一) 消息确认机制 - 消费者确认
由于生产者和消费者不直接通信,生产者只负责把消息发送到队列,消费者只负责从队列获取消息(不管是push还是pull). 消息被"消费"后,是需要从队列中删除的.那怎么确认消息被&q ...
- 更改Xamarin Android App名称
更改Xamarin Android App名称 Xamarin Android生成的App名称默认和项目名一致.修改该名称有两种方式. 第一种方式:右击Android项目,选择“属性”命令,然 ...
- MAC安装go
下载官方pkg文件,一路傻瓜next就行了. pkg默认是安装到/usr/local/go 安装完成,接下来是配置mac下的环境变量.打开终端(Terminal),敲入一下代码:cd ~ #进入当前用 ...
- [UOJ407]Werewolf
题意:给一个无向图和一些询问$(S,E,L,R)$,问能否实现:从$S$出发,经过一些编号$\geq L$的节点后再通过编号$\leq R$的节点到达$E$ 先对每条边$(x,y)$以$\max(x, ...