[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 way to replace the state portion of our stateful datatype with a value. We explore the mechanics of how this can be accomplished and introduce the put construction helper. We also see how we can run our instances with execWith to extract the state portion and discard the resultant.
Put state is replace the old state with new state:
//putState :: s -> State s ()
const putState = state => State(() => Pair(Unit(), state));
console.log(
putState('new')
.runWith('OLD') //Pair( (), "put" )
)
This is useful when we want to create 'reset':
//putState :: s -> State s ()
const putState = state => State(() => Pair(Unit(), state));
//reset :: () -> State String ()
const reset = () => putState('default');
console.log(
reset()
.execWith('OLD') // default
)
This opreation is commonly used, therefore there is a well made function 'put':
const { Pair, Unit, curry, objOf, compose, State, mapProps, prop, option } = require("crocks");
const { put, get } = State;
//reset :: () -> State String ()
const reset = () => put('default');
console.log(
reset()
.execWith('OLD') // default
)
console.log(
put('new')
.runWith('OLD') //Pair( (), "new" )
)
[Functional Programming Moand] Update The State Of A State Monad (put)的更多相关文章
- [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 + React] Provide a reasonable default value for mapStateToProps in case initial state is undefined
For example we have a component, it needs to call 'react-redux' connect function. import { compose, ...
- [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 ADT] Initialize Redux Application State Using The State ADT
Not only will we need to give our initial state to a Redux store, we will also need to be able to re ...
- [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] 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 ...
- [Functional Programming] Using ComposeK for both get State and modify State
We have State like this: const state = { cards: [ { id: "green-square", color: "green ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
随机推荐
- 求问asp.net mvc发布问题
正常发布 浏览后如下
- php smtp发送邮件功能
<?php header("Content-Type: text/html; charset=utf-8"); class smtp { /* Public Variable ...
- 最小生成树算法详解(prim+kruskal)
最小生成树概念: 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边. 最小生成树可以用kruskal(克鲁斯卡尔)算法或prim(普里 ...
- mysql source 乱码
mysql -u root -p --default-character-set=utf8 use dbname source /root/newsdata.sql
- 洛谷P2224 [HNOI2001] 产品加工 [DP补完计划,背包]
题目传送门 产品加工 题目描述 某加工厂有A.B两台机器,来加工的产品可以由其中任何一台机器完成,或者两台机器共同完成.由于受到机器性能和产品特性的限制,不同的机器加工同一产品所需的时间会不同,若同时 ...
- JS内存管理与垃圾回收
内存分配 var n = 374; // 为数字分配内存 var s = 'sessionstack'; // 为字符串分配内存 var o = { a: 1, b: null }; // 为对象及其 ...
- Visual Studio找不到adb.exe错误解决
Visual Studio找不到adb.exe错误解决 错误信息:Cannot find adb.exe in specified SDK path.出现这种情况,是因为没有安装Android SDK ...
- Python - 字符和字符值之间的转换
字符和字符值之间的转换 Python中, 字符和字符值, 直接的转换, 包含ASCII码和字母之间的转换,Unicode码和数字之间的转换; 也可以使用map, 进行批量转换, 输出为集合, 使用jo ...
- C#代码规范化(代码风格化)的几个函数
近期由于适配Oracle的缘故,将旺财C#.NET代码生成器增加了风格化的几个函数,具体实现如下功能: 1.转换为Pascal风格,如User_Name/USER_NAME/UserName自动替换下 ...
- Manacher算法总结
部分图片转自:http://www.cnblogs.com/grandyang/p/4475985.html manacher算法(民间称马拉车算法233)是用来找字符串中的最长回文子串的,先来说一下 ...