zoukankan      html  css  js  c++  java
  • [Functional Programming Monad] Substitute State Using Functions With A State Monad (get, evalWith)

    We take a closer look at the get construction helper and see how we can use it to lift a function that maps the state portion and updates the resultant with the result. Using get in this fashion, we then demonstrate how we can make accessors that can then be extended to create more complex interactions.

    As there are times that we only want to pull the resultant for a given computation, we take a look at running our instances with the evalWith method. evalWith will run our computations, throwing away the state, returning the resultant.

    get() function return a Pair(a, s). 'a' is the return value (variable) and 's' is the orginal state.

    const { curry, compose, State, mapProps, prop, option } = require("crocks");
    
    const { modify, get } = State;
    
    const burgers = {
        burgers: 4
    }
    
    const taocs = {
        taocs: 10
    }
    
    const res = get()
        .map(prop('burgers'))
        .runWith(burgers)
    
    console.log(res); // Pair( Just 4, { burgers: 4 } )

    One thing we can improve from the code above is combine

    // From
    get()
        .map(prop('burgers'))
    
    // To
    get(prop('burgers'))

    Second the return value for the 'a' is wrapped in Maybe, we want to provided some default value for Nothing case:

    const res = get(prop('taocs'))
        .runWith(burgers)
    
    console.log(res); // Pair( Nothing, { burgers: 4 } )

    To do that we can create 'defaultProp':

    const defaultProp = (key, def) => get(prop(key)).map(option(def));
    const res = defaultProp('taocs', 0)
        .runWith(burgers)
    
    console.log(res); // Pair( 0, { burgers: 4 } )

    Thrid, we can  'compose' to simply the code further:

    const defaultProp = (key, def) => compose(
        option(def),
        prop(key)
    )
    const getBurgers = get(defaultProp('taocs', 0))
    const res = getBurgers
        .runWith(burgers)
    // Pair( 0, { burgers: 4 } )
    
    

    Last, we don't really care about the Pair(0, {burgers: 4}), we only interest the value: we can replace 'runWith' with 'evalWith':

    const defaultProp = (key, def) => compose(
        option(def),
        prop(key)
    )
    const getBurgers = get(defaultProp('burgers', 0))
    const res = getBurgers
        .evalWith(burgers) // 4

     ----

    const { curry, objOf, compose, State, mapProps, prop, option } = require("crocks");
    
    const { modify, get } = State;
    
    const burgers = {
        burgers: 4
    }
    
    const taocs = {
        taocs: 10
    }
    const defaultProp = (key, def) => compose(
        option(def),
        prop(key)
    )
    const getBurgers = get(defaultProp('burgers', 0))
    const burgerToTacos = getBurgers.map(objOf('tacos'));
    const res = burgerToTacos
        .evalWith(burgers) // 4
    
    console.log(res); // { tacos: 4 }
  • 相关阅读:
    或得最新的采购单号并改变为定长
    javaScript学习之路(1)词法结构
    如何让IE下载时下载内容自动跳转到迅雷等下载软件中
    简单的数据库查询操作
    1.1软件工程概述之软件危机
    SQL基础(巧记范式)
    给程序员的五点建议--如何成为编程高手并以此创业
    License控制解决方案
    MVC+三层+ASP.NET简单登录验证
    我曾七次鄙视自己的灵魂
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10334584.html
Copyright © 2011-2022 走看看