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)的更多相关文章

  1. [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 ...

  2. [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, ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [Functional Programming] Using ComposeK for both get State and modify State

    We have State like this: const state = { cards: [ { id: "green-square", color: "green ...

  9. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

随机推荐

  1. 【解决】win7 64 pip安装scrapy出错

    问题一:microsoft visual c++ 9.0 is required 参考:http://www.cnblogs.com/ldm1989/p/4210743.html 问题二:ERROR: ...

  2. SpringMVC中为什么要配置Listener和Servlet

    一直以来,我们使用SpringMVC的时候习惯性都配置一个ContextLoaderListener,虽然曾经有过疑问,配置的这个监听器和Servlet究竟做了什么,但也没深究. 要说任何Web框架都 ...

  3. Linux用户密码文件/etc/shadow相关

    (1).密码文件 [root@xuexi ~]# head -3 /etc/shadow root:$6$kcgcu794R0VP3fDL$aYN8XUbtWvZ4QQtT2xVW.N2CgE3YLP ...

  4. SqlHelper——只因为在人群中多看了你一眼

    对SQLHelper,还是有一点陌生的,但是大多数人都在使用,我就有一种想了解并使用的意愿,于是查了很多资料,发现一片不错的博客,放在下面,作为自己或读者使用的材料. 一.SqlHelper 出场 不 ...

  5. Codeforces 850B

    题意: 给出一个序列,两种操作: 1.删除一个数,代价为x 2.给一个数+1,代价为y 求最小代价,使这个序列不为空,且所有的数的gcd>1 n<=5e5,a[i]<=1e6 其实思 ...

  6. 【递推】【推导】【乘法逆元】UVA - 11174 - Stand in a Line

    http://blog.csdn.net/u011915301/article/details/43883039 依旧是<训练指南>上的一道例题.书上讲的比较抽象,下面就把解法具体一下.因 ...

  7. 【置换群】poj3270 Cow Sorting

    并不应该叫置换群……只是用到了置换而已,并没有群. 题解看这个吧,我就不写了:http://www.cnblogs.com/kuangbin/archive/2012/09/03/2669013.ht ...

  8. (原创)Stanford Machine Learning (by Andrew NG) --- (week 5) Neural Networks Learning

    本栏目内容来自Andrew NG老师的公开课:https://class.coursera.org/ml/class/index 一般而言, 人工神经网络与经典计算方法相比并非优越, 只有当常规方法解 ...

  9. <摘录>cocos2d-x 从环境搭建到win32项目移植android平台

    软件:cocos2d-x-2.2.3:android-ndk-r9d:adt-bundle-windows-x86_64-20131030:python-2.7.6: 1安装配置python 安装没什 ...

  10. Android 垃圾回收,用软引用建立缓存

    内存对于手机来说是非常重要的. 下面总结了我们在注意创建对象时的规则,以及怎么更好更快的实行GC回收,和怎么构建高速的对象cace缓冲. 1 避免循环遍历的创建对象,哪怕对象很小,也是要占资源的. 2 ...