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. vue2.0--组件通信(非vuex法)

    写在前面: 1.父组件的data写法与子组件的data写法不同 //父组件 data:{ //对象形式 } //子组件 data:function(){ return { //函数形式 } } 2.引 ...

  2. Eclipse中快速 打出 main方法的签名

    有时,我们创建一个空白类,需要打出main方法 public static void main(String [] args){ } 在Eclipse先敲main字符,然后按住ALT+/,再按回车即可 ...

  3. 运行hadoop的时候提示物理内存或虚拟内存溢出的解决方案running beyond physical memory或者beyond vitual memory limits

    当运行中出现Container is running beyond physical memory这个问题出现主要是因为物理内存不足导致的,在执行mapreduce的时候,每个map和reduce都有 ...

  4. AtCoder Regular Contest 103 Problem D Robot Arms (构造)

    题目链接  Problem D 给定$n$个坐标,然后让你构造一个长度为$m$的序列, 然后给每个坐标规定一个长度为$m$的序列,ULRD中的一个,意思是走的方向, 每次从原点出发按照这个序列方向,每 ...

  5. div的border & width

    长时间不用css,发现有些基础知识竟有些遗忘,由于项目中的一些css样式,进行了以下相关测试. div的width及height均设置为0后,div的border会怎样显示.经过测试后,发现borde ...

  6. mysql索引之七:组合索引中选择合适的索引列顺序

    组合索引(concatenated index):由多个列构成的索引,如create index idx_emp on emp(col1, col2, col3, ……),则我们称idx_emp索引为 ...

  7. Bzoj1101 Zap(莫比乌斯反演)

    题面 Bzoj 题解 先化式子 $$ \sum_{x=1}^a\sum_{y=1}^b\mathbf f[gcd(x,y)==d] \\ = \sum_{x=1}^a\sum_{y=1}^b\sum_ ...

  8. Outlook Font

  9. uva 10910(子集和问题)

    Marks Distribution Time limit: 3.000 seconds In an examination one student appeared in N subjects an ...

  10. python 写文件write(string), writelines(list) ,读文件

    read()方法用于直接读取字节到字符串中,可以接参数给定最多读取的字节数,如果没有给定,则文件读取到末尾. readline()方法读取打开文件的一行(读取下个行结束符之前的所有字节),然后整行,包 ...