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. 五十三 网络编程 TCP/IP简介

    虽然大家现在对互联网很熟悉,但是计算机网络的出现比互联网要早很多. 计算机为了联网,就必须规定通信协议,早期的计算机网络,都是由各厂商自己规定一套协议,IBM.Apple和Microsoft都有各自的 ...

  2. 转:java-Servlet开发

    转:http://www.cnblogs.com/xdp-gacl/p/3760336.html 一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司 ...

  3. 如何判断图中存环(正&负)

    1.正环 用 SPFA不断的进行松弛操作,发现当前金额可以比本身大就更新,同时记录更新次数.如果更新次数超过n次,说明存在”正“环. 2.负环 这里先说明下负环.(求最短距离的时候) 在我们用SPFA ...

  4. CodeForces 738C Road to Cinema

    二分答案. 油量越多,显然通过的时间越少.可以二分找到最小的油量,可以在$t$时间内到达电影院. 一个油箱容量为$v$的车通过长度为$L$的路程需要的最小时间为$max(L,3*L-v)$.计算过程如 ...

  5. Java实现下载BLOB字段中的文件

    概述 web项目的文件下载实现:servlet接收请求,spring工具类访问数据库及简化大字段内容获取. 虽然文章的demo中是以sevlet为平台,想必在spring mvc中也有参考意义. 核心 ...

  6. 洛谷P4609 [FJOI2016]建筑师(第一类斯特林数+组合数)

    题面 洛谷 题解 (图片来源于网络,侵删) 以最高的柱子\(n\)为分界线,我们将左边的一个柱子和它右边的省略号看作一个圆排列,右边的一个柱子和它左边的省略号看作一个圆排列,于是,除了中间的最高的柱子 ...

  7. 设计模式-组合模式(Composite Pattern)

    本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 前置技能:认识数据结构中的树形结构. 组合模式简介 组合模式是将对象组合成树形结构以表示“部分- ...

  8. Section One

    1.1.1 #include <iostream> using namespace std; int main() { int a,b,N; cin >> N; while ( ...

  9. java-多线程-一道阿里面试题分析

    这段代码大多数情况下运行正常,但是某些情况下会出问题.什么时候会出现什么问题?如何修正?可见博客 http://yueyemaitian.iteye.com/blog/1387901 1.public ...

  10. POJ2234 Matches Game 尼姆博弈 博弈论

    http://poj.org/problem?id=2234 尼姆博弈(Nimm's Game) 指的是这样一个博弈游戏:有任意堆物品,每堆物品的个数是任意的,双方轮流从中取物品,每一次只能从一堆物品 ...