[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 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)
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)的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- Monad (functional programming)
In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...
- [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 ...
- [Functional Programming Monad] Combine Stateful Computations Using Composition
We explore a means to represent the combination of our stateful computations using familiar composit ...
随机推荐
- css3玩转各种效果【资源】
css3玩转各种按钮效果[资源] 点击下载 css3各种拐弯箭头-包括循环旋转 点击下载 不定期更新,下班了……
- android ListView 在初始化时多次调用getView()原因分析
今天在做一个功能:在初始化ListView时,把第一行背景置为黄色,同时保存第一行对象,用于在点击其他行时将该行重新置为白色. if(position==0){ convertView.setBack ...
- python No migrations to apply
错误显示: “No migrations to apply” 错误情况:python在通过model同步数据库时,提示 No migrations to apply 查看数据库,新表没有被创建,只是 ...
- JS中==与===操作符的比较
摘要: ===操作符: 要是两个值类型不同,返回false 要是两个值都是number类型,并且数值相同,返回true 要是两个值都是stirng,并且两个值的String内容相同,返回true 要是 ...
- 19. Remove Nth Node From End of List【Medium】【删除单链表倒数第n个结点】
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- 初见Python<1>:基础语法
1.两个整数相除,计算结果的小数部分被截除,结果仍然是一个整数: 如:1/2=0 2.整数和浮点数相除.或者浮点数之间相除,结果有小数部分,仍然是一个浮点数: 如:1/2.0=0.5 1.0/2=0 ...
- JZYZOJ1502 [haoi2008]下落的圆盘 计算几何 贪心
http://172.20.6.3/Problem_Show.asp?id=1502这种题用了快一天才写出来也是真的辣鸡.主要思路就是计算一下被挡住的弧度然后对弧度进行贪心.最开始比较困扰的是求弧度值 ...
- Problem C: 输入10个数,根据提示进行从小到大输出或从大到小输出
#include<stdio.h> int main() { char ch; ]; while(scanf("%c",&ch)!=EOF) { int i,j ...
- Codeforces Gym 100269G Garage 数学
Garage 题目连接: http://codeforces.com/gym/100269/attachments Description Wow! What a lucky day! Your co ...
- MATLAB/Octave warning: mx_el_eq: automatic broadcasting operation applied 错误分析
在进行对一个mXn的矩阵与mX1的矩阵进行==比较时,原意是想让mXn的矩阵的每一行分别与mX1的矩阵每一行进行比较,得到的结果虽然是对的,但会报一个warning: mx_el_eq: automa ...