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, ...
随机推荐
-
address_add
<include file="Header:header-address_add" /> <include file="Header:header-pu ...
-
使用CSS/JS代码修改博客模板plus
之前对CSS/JavaScript了解还不深,只是把模板的CSS胡乱修改了几个属性.最近正好也在做一个网站的前端,学习了不少东西,再来改一改~ 上次最后之所以铩羽而归,是因为从CSS里找不到那些#和. ...
-
dup和dup2函数以及管道的实现
疑问:管道应该不是这样实现的,因为这要求修改程序的代码 dup和dup2也是两个非常有用的调用,它们的作用都是用来复制一个文件的描述符.它们经常用来重定向进程的stdin.stdout和stderr. ...
-
从Decorator,Adapter模式看Java的IO库
我想任何一本介绍模式的书在讲到Decorator模式的时候不能不提到它的实际应用--在Java/IO库里面的应用,<<Java与模式>>这本书也不例外,有点不一样的是,这本书在 ...
-
Linux定时任务Crontab命令详解
linux 系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的.另 外, 由于使用者自己也可以设置计划任务,所以, ...
-
stringstream clear()的疑问 - yuanshuilee的日志 - 网易博客
stringstream clear()的疑问 - yuanshuilee的日志 - 网易博客 stringstream clear()的疑问 2013-09-05 08:43:13| 分类: ...
-
c#-Artificial Intelligence Class
NET Artificial Intelligence Class http://www.codeproject.com/KB/recipes/aforge_neuro/neuro_src.zip
-
zf-关于统计分析表单导出(写这个的 太麻烦了)
一个类里面写了2个一样的方法 如果是我 会重复利用 而不是这样写 今天改bug的时候我把一个类修改了2次 差点以为进错了类
-
CDH版本的oozie安装执行bin/oozie-setup.sh prepare-war,没生成oozie.war?
不多说,直接上干货! 前期博客 Oozie安装部署 问题描述 bin/oozie-setup.sh prepare-war 解决办法 [hadoop@bigdatamaster bin]$ pwd / ...
-
Cannot find a valid baseurl for repo: base
Linux下执行yum命令的时候一直报错:Cannot find a valid baseurl for repo: base 网上找了好多办法都没有解决... 我之前也遇到过一次, vi /etc/ ...