We take a closer look at the get construction helper and see how we can use it to lift a function that maps the state portion and updates the resultant with the result. Using get in this fashion, we then demonstrate how we can make accessors that can then be extended to create more complex interactions.

As there are times that we only want to pull the resultant for a given computation, we take a look at running our instances with the evalWith method. evalWith will run our computations, throwing away the state, returning the resultant.

get() function return a Pair(a, s). 'a' is the return value (variable) and 's' is the orginal state.

const { curry, compose, State, mapProps, prop, option } = require("crocks");

const { modify, get } = State;

const burgers = {
burgers:
} const taocs = {
taocs:
} const res = get()
.map(prop('burgers'))
.runWith(burgers) console.log(res); // Pair( Just 4, { burgers: 4 } )

One thing we can improve from the code above is combine

// From
get()
.map(prop('burgers')) // To
get(prop('burgers'))

Second the return value for the 'a' is wrapped in Maybe, we want to provided some default value for Nothing case:

const res = get(prop('taocs'))
.runWith(burgers) console.log(res); // Pair( Nothing, { burgers: 4 } )

To do that we can create 'defaultProp':

const defaultProp = (key, def) => get(prop(key)).map(option(def));
const res = defaultProp('taocs', )
.runWith(burgers) console.log(res); // Pair( 0, { burgers: 4 } )

Thrid, we can  'compose' to simply the code further:

const defaultProp = (key, def) => compose(
option(def),
prop(key)
)
const getBurgers = get(defaultProp('taocs', ))
const res = getBurgers
.runWith(burgers)
// Pair( 0, { burgers: 4 } )

Last, we don't really care about the Pair(0, {burgers: 4}), we only interest the value: we can replace 'runWith' with 'evalWith':

const defaultProp = (key, def) => compose(
option(def),
prop(key)
)
const getBurgers = get(defaultProp('burgers', ))
const res = getBurgers
.evalWith(burgers) //

----

const { curry, objOf, compose, State, mapProps, prop, option } = require("crocks");

const { modify, get } = State;

const burgers = {
burgers:
} const taocs = {
taocs:
}
const defaultProp = (key, def) => compose(
option(def),
prop(key)
)
const getBurgers = get(defaultProp('burgers', ))
const burgerToTacos = getBurgers.map(objOf('tacos'));
const res = burgerToTacos
.evalWith(burgers) // console.log(res); // { tacos: 4 }

[Functional Programming Monad] Substitute State Using Functions With A State Monad (get, evalWith)的更多相关文章

  1. [Functional Programming] Using JS, FP approach with Arrow or State Monad

    Using Naive JS: const {modify, get} = require('crocks/State'); const K = require('crocks/combinators ...

  2. [Functional Programming] Pull Many Random Numbers in a Single State ADT Transaction

    We have the ability to select a single random card from a pile of twelve cards, but we would like to ...

  3. [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 ...

  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 Moand] Update The State Of A State Monad (put)

    Stateful computations require the ability for their state to change overtime. We take a look on one ...

  6. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

  7. Monad (functional programming)

    In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...

  8. [Functional Programming] Introduction to State, thinking in State

    Recently, I am learning Working with ADT. Got some extra thought about State Monad. Basiclly how to ...

  9. [Functional Programming Monad] Combine Stateful Computations Using Composition

    We explore a means to represent the combination of our stateful computations using familiar composit ...

随机推荐

  1. 智能社官网顶部导航实现demo

    从智能社的blue老师公开课中学习到了很多,在此表示感谢. 这个导航很好玩,于是就想实现一个. html <div id="box"> <ul> <l ...

  2. gulp配合vue压缩代码格式化

    实际项目就是一个单页面.因此,我觉得用gulp足够,并且不需要webpack和vue-cli因为没有必要使用组件. 先来说一下项目结构 1. 然后来看看我的包管理package.json都用了啥,你也 ...

  3. <<持续交付>>终点的精彩

    1,向敏捷转变的过程是一个很容易出乱子的过程, 尤其对项目的领导力来说.在实施敏捷的过程中,会突然释放出一些有用的信息,将原来隐蔽起来的真相推倒聚光灯下. 2,假如执行者忽略了技术实践(比如测试驱动开 ...

  4. 公司gitlab不支持ssh时,用http提交代码免密输入方法

    由于公司内网22端口被封,只能拨vpn 才能用ssh 提交代码.因此记录以下免密码http(https)提交方式. 修改项目下.git/config 将原来的 http://git.xxx.com/x ...

  5. shell脚本报错退出

    在shell脚本中,比如有以下的代码: cd /root/test88 rm -rf  backup 如果目录/root/test88不存在,脚本不会停止,依然会执行rm -rf backup这个命令 ...

  6. 破解 apk

    韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com 使用 ida pro 直接分析 apk   . 有些代码 混淆 处理过, 就需要 动态调试 辅助 ...

  7. luogu P1440 求m区间内的最小值

    题目描述 一个含有n项的数列(n<=2000000),求出每一项前的m个数到它这个区间内的最小值.若前面的数不足m项则从第1个数开始,若前面没有数则输出0. 输入输出格式 输入格式: 第一行两个 ...

  8. 扫描线三巨头 hdu1928&&hdu 1255 && hdu 1542 [POJ 1151]

    学习链接:http://blog.csdn.net/lwt36/article/details/48908031 学习扫描线主要学习的是一种扫描的思想,后期可以求解很多问题. 扫描线求矩形周长并 hd ...

  9. BZOJ 1711:[Usaco2007 Open]Dining吃饭(最大流)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1711 [题目大意] 每头牛都有一些喜欢的饮料和食物, 现在有一些食物和饮料,但是每样只 ...

  10. [xsy2880]取石子游戏

    题意:有$n$堆石子,每堆石子数量相同,以质因数分解给出,不停地从$1$到$n$依次拿石子,使得取完后石子个数为原来的因数(不能不取),当一堆只剩$1$个时结束,问在每堆石子结束的方案数 记石子个数为 ...