[Functional Programming Monad] Refactor Stateful Code To Use A State Monad
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的更多相关文章
- [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 ...
- [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 ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- Monad (functional programming)
In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
- 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 ...
- 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 ...
- Functional programming
In computer science, functional programming is a programming paradigm, a style of building the struc ...
- 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 ...
随机推荐
- 前端读者 | 前端用户体验-UI动效设计
本文来自互联网 @羯瑞 整理 UI动效现如今在 APP 和网页中几乎已经成为了基本的组成部分,经过仔细打磨的 UI动效对于整个界面的提升是显著的. 动效呈现出状态切换的过程,展现了元素之间的逻辑关系, ...
- 禁止网页右键和复制,ctrl+a都不行。取消页面默认事件【全】。
document.oncontextmenu=new Function("event.returnValue=false");document.onselectstart=new ...
- 在 Ubuntu 系统安装 Redi
在 Ubuntu 系统安装 Redi 可以使用以下命令: $sudo apt-get update $sudo apt-get install redis-server 启动 Redis $ redi ...
- 【转】Python高级特性——切片(Slice)
摘录廖雪峰网站 定义一个list: 1 L = ['haha','xixi','hehe','heihei','gaga'] 取其前三个元素: >>> L[0],L[1],L[2] ...
- PTA L1-020 帅到没朋友 团体程序设计天梯赛-练习集
L1-020 帅到没朋友(20 分) 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友.本题就要求你找出那些帅到没有朋友的人. 输入格式: 输入第一行给出一个正整数N(≤),是已 ...
- php中网页生成图片的方式
在网上找了很多方法,发现与自己最初的思路也是大同小异,那就是HTML——>PDF——>JPG.从上午9点钟一直搞到下午6点钟,代码方面其实很简单,更多的还是环境和PHP拓展上面,忙了一天的 ...
- 洛谷P3620 [APIO/CTSC 2007] 数据备份 [堆,贪心,差分]
题目传送门 题目描述 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味的,因此你想设计一个系统让不同的办公楼彼此之间互相备份,而你则坐在家中尽 ...
- 洛谷P1850换教室
题目传送门 理解题意:给定你一个学期的课程和教室数量以及教室之间的距离还有换教室成功的概率,求一个学期走的距离的期望最小值 题目是有够恶心的,属于那种一看就让人不想刷的题目...很明显的动规,但是那个 ...
- JavaScript Output
JS can "display" data in different ways: (1)Writing into an alert box, using window.alert( ...
- 初识C#设计模式
利用设计模式可以使我们的代码更灵活,更容易扩展,更容易维护.各种面向对象的程序设计语言都提供了基本相同的机制:比如类.继承.派生.多态等等.但是又有各自的特色,C# 中的反射机制便是一个很重要的工具, ...