[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 state based on its previous value. In this lesson we will explore a means to lift specialized functions that can be used modify our state’s value. These specialized functions must have the same type in their input and output. We take a look at a construction helper named modify that is used to remove the boilerplate of this type of construction. We also start to see how we can extend simple discrete stateful transactions to create other stateful transactions.
Modify state is apply a function to the original state to get new state:
// modifyState :: (s -> s) -> State s ()
const modifyState = fn => State(s => Pair(Unit(), fn(s)));
// modifyState :: (s -> s) -> State s ()
const modifyState = fn => State(s => Pair(Unit(), fn(s)));
const bubble = {
bubbles:
};
const add = x => y => x+ y;
console.log(
modifyState(mapProps({bubbles: add()}))
.runWith(bubble) // Pair( (), { bubbles: 41 } )
)
Of couse, we have the 'modify' function already in the library, so the code can be changed to:
const bubble = {
bubbles:
};
const add = x => y => x+ y;
console.log(
modify(mapProps({bubbles: add()}))
.runWith(bubble) // Pair( (), { bubbles: 41 } )
)
[Functional Programming Monad] Modify The State Of A State Monad的更多相关文章
- [Functional Programming] Read and Transform Values from a State ADT’s State (get)
Many times we need to access and transform state, either in part or in full, to be used when calcula ...
- [Functional Programming Monad] Combine Stateful Computations Using A State Monad
The true power of the State ADT really shows when we start combining our discrete, stateful transact ...
- [Functional Programming] Using ComposeK for both get State and modify State
We have State like this: const state = { cards: [ { id: "green-square", color: "green ...
- [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 ...
- [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 ...
- Monad (functional programming)
In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...
- [Functional Programming Monad] Combine Stateful Computations Using Composition
We explore a means to represent the combination of our stateful computations using familiar composit ...
- [Functional Programming] Randomly Pull an Item from an Array with the State ADT (Pair)
Functor composition is a powerful concept that arises when we have one Functor nested in another Fun ...
- [Functional Programming] Transition State based on Existing State using the State ADT (liftState, composeK)
While sometimes outside input can have influence on how a given stateful transaction transitions, th ...
随机推荐
- ubuntu16.04下安装docker
原文地址:传送门 本文开发环境为Ubuntu 16.04 LTS 64位系统,通过apt的docker官方源安装最新的Docker CE(Community Edition),即Docker社区版,是 ...
- NET牛人应该知道些什么?(瞬间觉得自己弱爆了)
任何一个使用.NET的人 描述线程与进程的区别? 什么是Windows服务,它的生命周期与标准的EXE程序有什么不同 Windows上的单个进程所能访问的最大内存量是多少?它与系统的最大虚拟内存一样吗 ...
- Codeforces 714B. Filya and Homework
题目链接:http://codeforces.com/problemset/problem/714/B 题意: 给你一个含有 n 个数的数组, 问你是否存在一个 x, 使得这个数组中的某些数加上 x, ...
- CodeForces 779B Weird Rounding
简单题. 删去结尾的不是$0$的数字,保证结尾连续的$k$个都是$0$,如果不能做到,就保留一个$0$. #include<map> #include<set> #includ ...
- 训练continue
11.16 树状数组 http://codeforces.com/contest/1070/problem/C digit sum+% dp http://codeforces.com/contest ...
- 【最短路径】 常用算法图解+1376:信使(msner)六解
进入图之后,最短路径可谓就是一大重点,最短路径的求法有很多种,每种算法各有各的好处,你会几种呢?下面来逐个讲解. 1 floyed算法 1)明确思想及功效:在图中求最短路还是要分开说的,分别是单源最短 ...
- 【树形dp】TELE
[POJ1155]TELE Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5376 Accepted: 2973 Des ...
- 【最小割】BZOJ3438-小M的作物(Rank 2???!!!)(含新款Dinic模板)
一开始被T掉了之后,才害怕地发现之前写的Dinic基本上都是错的??!!!正确的写在注释里了,注意一下(;3<)馬鹿やろ 一个丧心病狂的优化前后效率对比:
- ERROR 1044: Access denied for user: 'songyan' to database 'yikexiao' 的错误。
问题描述:新买的服务器,刚安装了mysql,创建了一个用户,也忘记了给他分配了什么权限,今天在建库的时候出现了这个问题. 出错原因:度娘告诉我是因为songyan用户没有建库的权限报的错. 解决: ( ...
- [典型漏洞分享]YS的防暴力破解设计存在缺陷
YS使用的防暴力破解机制存在缺陷,该缺陷可被用于暴力破解其它用户密码[高] 问题描述: YS在用户登录页面设置了验证码机制,当用户输入密码错误次数达到3次时,再次登录需要验证码以防止攻击者进行暴力破解 ...