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. python版GetTickCount()

    time.clock() return the current processor time as a floating point number expressed in seconds. 即返回一 ...

  2. python3连接使用sqlite3

    一直比较喜欢sqlite,业余爱好不需要大型数据库,原来在windows下最常用的就是access,使用很方便,但是linux下没法用,后 来从php+sqlite2开始使用,编程时间很少,代码量很小 ...

  3. php定位并且获取天气信息

    /** *获取天气预报信息 **/ header("Content-type: text/html; charset=utf-8"); class getWeather{ priv ...

  4. 剑指offer-树中两个节点的最低公共祖先

    普通二叉树 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; ...

  5. python笔记一:函数的参数

    1.默认参数 def fun(x,y,z=3): sum=x+y+z return sum fun(1,2) 2.可变参数(两种方法定义) def fun(n): sum=0 for i in n: ...

  6. Eclipse控制台

    Eclipse中的控制台,是以卡片布局方式来管理的.每运行一个新的程序,创建一个新的控制台,覆盖到原来的控制台之上. 控制台的控制按钮有10个,仅对其中部分作介绍 第1个按钮结束当前程序的运行 第2个 ...

  7. angularjs学习笔记1-angular总体简介及其特点

    以前开发(web或者移动端)前端主要使用jQuery+原生js,如果使用某些前端UI框架的话,它自己还可能提供一些API可以使用.而且目前很多UI框架都是基于jQuery的,所以说一下由jQuery跨 ...

  8. [Codeforces #190] Tutorial

    Link: Codeforces #190 传送门 A: 明显答案为$n+m-1$且能构造出来 #include <bits/stdc++.h> using namespace std; ...

  9. 【CCpp程序设计2017】简单进销存

    题目:简单进销存 功能要求: 实现如下的菜单(按数字选择菜单功能): 1. 显示存货列表 2. 入库 3. 出库 4. 退出程序 实现菜单对应功能(需记录货物的型号.数量等信息): 程序启动时从文件中 ...

  10. 实验三 敏捷开发与XP实践实验报告

    实验三 敏捷开发与XP实践实验报告 实验内容 1. XP基础 2. XP核心实践 3. 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vi ...