When we start to accumulate functions that all work on a given datatype, we end up creating a bunch of boilerplate code in almost every function to handle the changes we need to make to our records’ values. This can lead to not only undesirable boilerplate present in all of our functions, but also can cause us to have to create variables just to manage our stateful changes.

We’ll take a look at a couple patterns that can act as early warning signs that will eventually cause us to not have a good time. Once we know what the smell is, we’ll look at how moving our computations into State can clean up all of our state management code by making it the responsibility of State. This allows our functions to only describe how state should change over time versus us having to change it ourselves.

Imaging we have a user object, if we want to udpate firstName prop, we have to update fullName as well.

const user =  {
firstName: 'John',
lastName: 'Green',
fullName: 'John Green'
}

Code like this:

const _buildFulName = user => {
const {firstName, lastName} = user;
const fullName = joinName(firstName, lastName)
return _updateFullName(fullName, user)
}
const _updateFirstName = curry(
firstName => compose(
_buildFulName,
assign({firstName})
)
)
const _updateFullName = curry(
fullName => assign({fullName})
)

We want to use a more flexiable way to do it:

const getState = (key) => get(prop(key))
const getFirstName = () => getState('firstName').map(option(''));
const getLastName = () => getState('lastName').map(option(''));
const joinName = firstName => lastName => `${firstName}, ${lastName}` const buildFullName = () => liftA2(
joinName,
getFirstName(),
getLastName()
).chain(updateFullName)
const updateFirstName = firstName => modify(
assign({firstName})
).chain(buildFullName);
const updateFullName = fullName => modify(
assign({fullName})
)

[Functional Programming Monad] Refactor Stateful Code To Use A State Monad的更多相关文章

  1. [Functional Programming] Using JS, FP approach with Arrow or State Monad

    Using Naive JS: const {modify, get} = require('crocks/State'); const K = require('crocks/combinators ...

  2. [Functional Programming] Pull Many Random Numbers in a Single State ADT Transaction

    We have the ability to select a single random card from a pile of twelve cards, but we would like to ...

  3. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

  4. Monad (functional programming)

    In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...

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

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

  6. Functional Programming without Lambda - Part 1 Functional Composition

    Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...

  7. a primary example for Functional programming in javascript

    background In pursuit of a real-world application, let’s say we need an e-commerce web applicationfo ...

  8. Functional programming

    In computer science, functional programming is a programming paradigm, a style of building the struc ...

  9. BETTER SUPPORT FOR FUNCTIONAL PROGRAMMING IN ANGULAR 2

    In this blog post I will talk about the changes coming in Angular 2 that will improve its support fo ...

随机推荐

  1. 【剑指offer】面试题 4. 二维数组中的查找

    面试题 4. 二维数组中的查找 题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序. 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该 ...

  2. vmware漏洞之一——转:利用一个堆溢出漏洞实现VMware虚拟机逃逸

    转:https://zhuanlan.zhihu.com/p/27733895?utm_source=tuicool&utm_medium=referral 小结: vmware通过Backd ...

  3. python的递归函数

    1.如果一个函数在内部调用自身本身,这个函数就是递归函数. 例:计算 n! = 1 x 2 x 3 x ... x n,用函数fact(n)表示,可以看出: fact(n) = n! = 1 x 2 ...

  4. 30、Flask实战第30天:cms模版抽离和个人信息页面完成

    cms模版抽离 新建一个cms_base.html文件作为基础模板,把cms_index.html的内容拷贝到cms_base.html中. 编辑 cms_base.html,把在不同页面会变动的部分 ...

  5. 理解Python的迭代器(转)

    原文地址: http://python.jobbole.com/81916/ 另外一篇文章: http://www.cnblogs.com/kaituorensheng/p/3826911.html ...

  6. 【图论】Self-Assembly(6-19)

    [UVA1572]Self-Assembly 算法入门经典第6章6-19(P172) 题目大意:有一些正方形,每条边上都有A-~Z- A+~Z+的编号,或者00,A+的边可以拼A-,反之亦然.00的边 ...

  7. 【线段树】Atlantis

    Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23181   Accepted: 8644 Descrip ...

  8. BZOJ 2733 [HNOI2012]永无乡(启发式合并+Treap+并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2733 [题目大意] 给出n个点,每个点都有自己的重要度,现在有连边操作和查询操作, 查 ...

  9. BZOJ 3632 外太空旅行(最大团)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3632 [题目大意] 求图中的最大团. [题解] 最大团问题是npc问题,因此可以考虑随 ...

  10. Vue实例与渲染

    1 Vue框架 1.1 vue与jQuery区别 jQuery仍然是操作DOM的思想,jQuery主要用来写页面特效 Vue是前端框架(MVVM),对项目进行分层.处理数据 1.2 前端框架 angu ...