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. CentOS7.5安装截图软件

    一.Screenshot tool插件 这个插件直接在https://extensions.gnome.org/搜索,然后打开ON,等待安装完毕,就可以在你桌面的顶栏的右侧看到一个相机一样的小东西 缺 ...

  2. 【面试题】2018年最全Java面试通关秘籍汇总集!

    [面试题]2018年最全Java面试通关秘籍汇总集!(转载于互联网)   前几天在交流群里有些小伙伴问面试相关的试题,当时给出了一些问题,苦于打字太累就没写下去了,但觉得这是一个很不负责任的表现,于是 ...

  3. python request post上传文件额外注意点

    通用用法 但上图的字段名,类型需要根据不同接口填写,如某服务接口: 因而对应的上传代码如下: # 输出参数:请求响应报文import requestsrequest_url = 'https://XX ...

  4. 【转载】Scroller源码解析

    原文地址:https://github.com/Skykai521/AndroidSdkSourceAnalysis/blob/master/article/Scroller%E6%BA%90%E7% ...

  5. js数组,在遍历中删除元素

    /** * 有效的方式 - 改变下标,控制遍历 */ for (var i = 0; i < arr.length; i++) { if (...) { arr.splice(i, 1); // ...

  6. oracle 查看16进制

    DUMP function is useful for this purpose. SQL> select dump(C1) from test; DUMP(C1)--------------- ...

  7. 设计模式-外观模式(Facade Pattern)

    本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 外观模式简介 外观模式的作用用一句话说就是简化接口,举个例子楼主每次编程的时候都要点开IDE.点 ...

  8. hdu 6047 Maximum Sequence 贪心

    Description Steph is extremely obsessed with “sequence problems” that are usually seen on magazines: ...

  9. mac下使用github 上传代码

    提起github相信大家都不会陌生,在这里就不再赘述了.作为开源代码库以及版本控制系统,使用好了确实会非常受益,再说的势利点,你找工作时给面试官说你经常维护自己的技术博客和github,相信你给他的印 ...

  10. Unity3D 避免玩家作弊

    如果你的Unity项目快上线了,我强烈建议你看一下Anti-Cheat这个插件.因为IOS和Android分别越狱和Root后玩家可以使用 @八门神器 @烧饼修改器 等一些列作弊的软件来修改游戏内存, ...