• [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 } )
    )
  • 相关阅读:
    H5基础
    函数
    分支结构/循环结构
    图解 idea打jar包的步骤
    jmeter查看使用文档后总结
    Ride的使用
    Robot Framework
    jmeter+ant+jenkins
    ant
    Mysql选择合适的数据类型
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10334915.html
Copyright © 2020-2023  润新知