zoukankan      html  css  js  c++  java
  • [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: 40
    };
    const add = x => y => x+ y;
    console.log(
        modifyState(mapProps({bubbles: add(1)}))
            .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: 40
    };
    const add = x => y => x+ y;
    console.log(
        modify(mapProps({bubbles: add(1)}))
            .runWith(bubble) // Pair( (), { bubbles: 41 } )
    )
  • 相关阅读:
    笔记本
    物料主档建立(PP模组)
    烦!烦!烦!
    Windows Live Writer试用
    SAP系统中发送公告的几种办法
    [CSS样式表之] 渐变色的实现
    今天终于开通了这个博客了
    MFC消息映射机制过程
    绘图
    C++ 内存分配和指针
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10334915.html
Copyright © 2011-2022 走看看