[JS Compse] 4. A collection of Either examples compared to imperative code
For if..else:
const showPage() {
if(current_user) {
return renderPage(current_user);
} else {
return showLogin();
}
} const showPage() {
fromNullable(current_user)
.fold(showLogin, renderPage)
}
const getPrefs = user => {
if(user.premium) {
return loadPrefs(user.preferences)
} else {
return defaultPrefs;
}
} const getPrefs = user =>
(user.premium ? Right(user): Left('not premium'))
.map(p => user.preferences)
.fold(
x => defaultPrefs,
x => loadPrefs(x)
)
const streetName = user => {
const address = user.address; if(address) {
const street = address.street; if(street) {
return street.name;
}
}
return 'no street';
} cosnt streetName = user =>
fromNullable(user.address)
.flatMap(address => fromNullable(address.street))
.map(street => street.name)
.fold(e => 'no street', n => n)
const concatUniq = (x, ys) => {
const found = ys.filter(y => y === x)[]
return found ? ys : ys.concat(x);
} const concatUniq = (x, ys) =>
fromNullable(ys.filter(y => y === x)[]) // fromNullable needs value
.fold(() => ys.concat(x), y => ys)
const wrapExamples = example => {
if(example.previewPath){
try {
example.preview = fs.readFileSync(example.previewPath)
} catch(e) { } return example;
}
} const readFile = x => tryCatch(() => readFileSync(x));
const wrapExample = example =>
fromNullabel(exampe.previewPath)
.flatMap(readFile)
.fold(() => example,
ex => Object.assign({preview: p}, ex);
)
const parseDbUrl = cfg => {
try {
const c = JSON.parse(cfg);
if(c.url) {
return c.url.match(/..../)
} catch(e) {
return null
}
}
} const parseDbUrl = cfg =>
tryCatch(() => JSON.parse(cfg))
.flatMap(c => fromNullable(c.url))
.fold(
e => null,
u => u.match(/..../)
)
[JS Compse] 4. A collection of Either examples compared to imperative code的更多相关文章
- [JS Compose] 1. Refactor imperative code to a single composed expression using Box
After understanding how Box is, then we are going to see how to use Box to refacotr code, to un-nest ...
- backbone.js 教程(1) View & Model & Collection
Backbone.js Overview 它由Jeremy Ashkenas开发,最初发行于2010-10-13 它是一个轻量的JavaScript类库,只依赖于underscore.js,非强制依赖 ...
- (转载)SQL Reporting Services (Expression Examples)
https://msdn.microsoft.com/en-us/library/ms157328(v=SQL.100).aspx Expressions are used frequently in ...
- JS组件系列——表格组件神器:bootstrap table(二:父子表和行列调序)
前言:上篇 JS组件系列——表格组件神器:bootstrap table 简单介绍了下Bootstrap Table的基础用法,没想到讨论还挺热烈的.有园友在评论中提到了父子表的用法,今天就结合Boo ...
- 2.MongoDB 基于node.js访问和操作集合
对于频繁使用的Node.js来说,常见的任务是集合的动态操控. 较大的安装给每个大客户一个单独的集合,以便客户登入或离开时.根据需要添加或删除集合. MongoDB Node.js 驱动程序 Db和C ...
- 用Backbone.js创建一个联系人管理系统(一)
原文 Build a Contacts Manager Using Backbone.js: Part 1 在这个教程里我们将会使用Backbone.js,Underscore.js,JQuery创建 ...
- Understanding Asynchronous IO With Python 3.4's Asyncio And Node.js
[转自]http://sahandsaba.com/understanding-asyncio-node-js-python-3-4.html Introduction I spent this su ...
- sea.js,spm学习
安装spm 下载sea.js 运行spm npm install spm@2.x -g npm install spm-build -g 下载sea.js git clone https://gith ...
- 使用Three.js网页引擎创建酷炫的3D效果的标签墙
使用Three.js引擎(这是开源的webgl三维引擎,gitgub)进行一个简单应用. 做一个酷炫的3d效果的标签墙(已经放在我的博客首页,大屏幕可见), 去我的博客首页看看实际效果 www.son ...
随机推荐
- poj 2503 哈希 Map 字典树
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 36967 Accepted: 15749 Descr ...
- WINDOWS 安装 M2Crypto for Python2.7
WINDOWS 安装 M2Crypto for Python2.7运行环境 WIN8.1 + Python2.7 + VS2008(Microsoft Visual C++ 9.0) VS2008 可 ...
- 单位阶跃函数(Heaviside/unit step function)—— 化简分段函数
注意,单位阶跃函数一种不连续函数. 1. 常见定义 最经典的定义来自于 Ramp function(斜坡函数,max{x,0})的微分形式: H(x)=ddxmax{x,0} 2. 化简分段函数 如对 ...
- 进阶攻略|最全的前端开源JS框架和库
新的 Javascript 库层出不穷,从而Web 社区愈发活跃.多样.在多方面快速发展.详细去描述每一种主流的 Javascript框架和库近乎不可能,所以在这篇文章中主要介绍一些对前端发展最具影响 ...
- 设计模式六大原则(四):接口隔离原则(Interface Segregation Principle)
接口隔离原则(ISP)定义: 客户端不应该依赖它不需要的接口:一个类对另一个类的依赖应该建立在最小的接口上. 问题由来: 类A通过接口I依赖类B,类C通过接口I依赖类D,如果接口I对于类A和类B来说不 ...
- ASP.net 环境搭建
https://www.cnblogs.com/leizhanjun/p/6081928.html
- metabase实施文档
安装提前:需要安装JDK1.8以上 软件下载地址: https://metabase.com 还需要下载 ojdbc7.jar,以支持Oracle驱动 下载地址:http://www.oracle.c ...
- swiper轮播控件配置项
var mySwiper = new Swiper ('.swiper-container', { direction: 'horizontal', loop: true, auto ...
- 在linux环境下增加别名
编辑.cshrc文件:gvim ~/.cshrc 增加要添加的别名,例如:alias la 'ls -a' qw保存退出 source ~/.cshrc即可生效
- 删除dataGridview中选中的一行或多行
一.实现的功能:可以删除一行或者多行数据,并在删除前提醒是否确定进行删除! DialogResult RSS = MessageBox.Show(this,"确定要删除选中行数据码?&quo ...