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. 初步探究ES6之箭头函数

    今天要介绍的是ES6中的箭头函数. 语法 我们先来看看箭头函数的语法: ([param] [, param]) => { statements } param => expression ...

  2. PyInstaller:把你的Python转为Exe

    把Python程序转为可执行的EXE文件,之前已经介绍过,像py2exe,bbfreeze. 以我自己使用的经历来看,这两款都还不错,比较适合简单的Python程序,如果你加载的第三方类库比较多的话, ...

  3. 关于php的session.serialize_handler的问题

    前言 php的session信息是储存在文件中的 session.save_path="" 指定储存的路径 session.save_handler="" 指定 ...

  4. PHP超全局变量数组

    以下8个变量都是数组变量,又称为“预定义变量” 1.$_GET : 把数据通过地址栏传递到服务器,这是方式必须是$_GET方式传递: 2.$_POST : 通过表单发送的数据必须是POST方式: 3. ...

  5. Git Bash 将本地代码提交到Github

    前提:已拥有Token,并且把本地的Token配置到了自己的Github里面(没有Token的自行去百度如何配置Token) 测试一下自己的连接 ssh -T git@github.com 本地操作: ...

  6. 【BZOJ 2809】2809: [Apio2012]dispatching (左偏树)

    2809: [Apio2012]dispatching Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Maste ...

  7. eclipse汉化 adt汉化

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha

  8. noip2012开车旅行 题解

    题目大意: 给出n个排成一行的城市,每个城市有一个不同的海拔.定义两个城市间的距离等于他们的高度差的绝对值,且绝对值相等的时候海拔低的距离近.有两个人轮流开车,从左往右走.A每次都选最近的,B每次都选 ...

  9. Codeforces 550 D. Regular Bridge

    \(>Codeforces \space 550 D. Regular Bridge<\) 题目大意 :给出 \(k\) ,让你构造出一张点和边都不超过 \(10^6\) 的无向图,使得每 ...

  10. Codeforces 449D Jzzhu and Numbers(高维前缀和)

    [题目链接] http://codeforces.com/problemset/problem/449/D [题目大意] 给出一些数字,问其选出一些数字作or为0的方案数有多少 [题解] 题目等价于给 ...