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. centos 磁盘分区格式化与挂载

    1 查看系统里硬盘信息fdisk -l 2 磁盘分区fdisk /dev/sdc输入m显示帮助a:命令指定启动分区:d:命令删除一个存在的分区:l:命令显示分区ID号的列表:m:查看fdisk命令帮助 ...

  2. Codeforces 429 B. Working out-dp( Codeforces Round #245 (Div. 1))

    B. Working out time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  3. MYSQL注入天书之导入导出介绍

    Background-3 导入导出相关操作的讲解 load_file()导出文件 Load_file(file_name):读取文件并返回该文件的内容作为一个字符串. 使用条件: A.必须有权限读取并 ...

  4. 【BZOJ 1119】 1119: [POI2009]SLO (置换)

    1119: [POI2009]SLO Description 对于一个1-N的排列(ai),每次你可以交换两个数ax与ay(x<>y),代价为W(ax)+W(ay) 若干次交换的代价为每次 ...

  5. 原型开发工具 mockplus

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 原型开发工具  mockplus 微信(演示) - Mockup Plus Web Ap ...

  6. 【矩阵乘法】OpenJ_POJ - C17F - A Simple Math Problem

    算(7+4*sqrt(3))^n的整数部分(mod 1e9+7). 容易想到矩乘快速幂,但是怎么算整数部分呢? (7+4*sqrt(3))^n一定可以写成a+b*sqrt(3),同理(7-4*sqrt ...

  7. springmvc poi实现报表导出

    1.pom文件: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</ ...

  8. Base64序列化和反序列化

    序列化: Dictionary<string, string> sPara = GetRequestPost(ref parameterStr); string serializeStri ...

  9. Python的高级特性(切片,迭代,生成器,迭代器)

    掌握了python的数据类型,语句和函数,基本上就可以编出很多有用的程序了. 但是在python中,并不是代码越多越好,代码不是越复杂越好,而是越简单越好. 基于这个思想,就引申出python的一些高 ...

  10. 最小生成树之Prim算法--蓝白点思想

    Prim算法: 以前一直不是很明白,Prim算法,今天就来终结一下. Prim算法采用与Dijkstra.Bellman-Ford算法一样的“蓝白点”思想:白点代表已经进入最小生成树的点,蓝点代表未进 ...