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. jquery中的jsonp和js中的jsonp还有配合php实现的jsonp。

    最近在做一个自己的网站,才知道一个前端需要学习的东西到底有多少. 来吧~步入正题,我的网站空间很小.所以在资源上,就有点吼不住了.跨域拿数据肯定是不可避免的了. 一.原生js: 我们知道他的原理,就是 ...

  2. 在Pygtk和Glade使用Gtkbuilder

    最近开始学习python的GUI,选择了Pygtk,试着用Glade设计界面,项目文件采用Gtkbuilder格式,网上的教程大部分是使用Libglade,所以用xml方式读取.glade文件: wT ...

  3. python的版本会导致IBus设置(中文输入法)出错

    最近在学习python,可是,发现我的输入法 IBus-pinyin ,不能用了, 现象: 发现 “首选输入法”,根本点击不进去IBus设置的窗口,想去设置输入法都不行, IBus设置的窗口: 原因是 ...

  4. NBUT 1218 You are my brother

    $dfs$. 记录一下每一个节点的深度就可以了. #include<cstdio> #include<cstring> #include<cmath> #inclu ...

  5. [AGC019E]Shuffle and Swap

    [AGC019E]Shuffle and Swap 题目大意: 给出两个长度为\(n(n\le10000)\)的\(01\)串\(A_{1\sim n}\)和\(B_{1\sim n}\).两个串均有 ...

  6. http请求 url 传递参数(特殊字符)解决方法

    有些符号在URL中是不能直接传递的,如果要在URL中传递这些特殊符号,那么就要使用他们的编码了.下表中列出了一些URL特殊符号及编码       十六进制值 1. + URL 中+号表示空格 %2B ...

  7. [bzoj1012](JSOI2008)最大数maxnumber(Fenwick Tree)

    Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度. 2. ...

  8. CDOJ 1277 智商杯考试 每周一题 div2 二分+数学

    智商杯考试 题目连接: http://acm.uestc.edu.cn/#/problem/show/1277 Description 你是一个挂科选手. 你现在正在考试,你很方. 你参加的考试叫做智 ...

  9. SQL SERVER 2008 数据库置疑处理办法

    1.修改数据库为紧急模式 ALTER DATABASE Zhangxing SET EMERGENCY 2.使数据库变为单用户模式 ALTER DATABASE Zhangxing SET SINGL ...

  10. 微信小程序,工具未检测合法域名,400 (Bad Request)错误

    1.工具未检测合法域名 这个我就不多说了,如果你新建的时候没有填写AppID的话这个我就不太清楚了 如果新建项目的时候填写了AppID的话,出现这种错误,那你应该去小程序后台 设置->开发者设置 ...