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. nginxhttp请求限制丶tcp会话限制和下载速度限制

    (1)nginx请求限制 ngx_http_limit_req_module:开启对单个ip丶单个会话在单位时间内请求的限制rate表示限制的速率 1.修改nginx配置文件 #vim /usr/lo ...

  2. Linux rsync数据定时增量备份

    一.安装rsync服务端 1.查看是否安装rsync ps -ef | grep rsync 系统一般默认已安装,安装方法: yum -y install rsync 2.添加配置文件 rsync没有 ...

  3. python模式匹配,提取指定字段

    re匹配时分多行模式(re.M)与单行模式(rs.S),多行模式是每一行单独匹配,单行模式是把所有的行当成一行来匹配. 单行模式下.可以匹配换行符. ^$匹配所有字符 import re s='1_2 ...

  4. 【ASP.NET MVC】HTML5+MVC上传文件显示进度

    head> <title>Index</title> <style type="text/css"> #statusBorder { po ...

  5. PTA L1-020 帅到没朋友 团体程序设计天梯赛-练习集

    L1-020 帅到没朋友(20 分)   当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友.本题就要求你找出那些帅到没有朋友的人. 输入格式: 输入第一行给出一个正整数N(≤),是已 ...

  6. django-BBS(2)

    昨天设计了数据库和数据表,今天来进行页面前端的设计, 1.首先去bootstarp上,下载相应的模板和配置文件,添加到对应的位置 2.在templates中添加许多许多的html页面 如下     并 ...

  7. 生成自签名CA+SSL证书

    1.创建CA证书配置CA.cnf文件 [ req ] distinguished_name = req_distinguished_name x509_extensions = root_ca [ r ...

  8. FZOJ 2245 动态树(离散+离线+ 树状数组)

    Problem 2245 动态树 Accept: 17    Submit: 82Time Limit: 3000 mSec    Memory Limit : 65536 KB  Problem D ...

  9. WebService协议

    http://www.cnblogs.com/lm3515/archive/2011/03/17/1987009.html http://blog.csdn.net/chjttony/article/ ...

  10. UML动态模型(顺序图、协作图、状态图)

    顺序图:用来表示用例中的行为顺序,当执行一个用例行为时,顺序图中的每条信息 对应了一个类操作或状态机中引起转换的事件.顺序图展示对象之间的交互,这些交互是指在场景或用例的时间六中发生的,顺序图属于动态 ...