[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 ...
随机推荐
- Python - 字典(dict)删除元素
字典(dict)删除元素, 能够选择两种方式, dict.pop(key)和del dict[key]. 代码 # -*- coding: utf-8 -*- def remove_key(d, ke ...
- mk-编译信息的意义
今天第一次看Android.mk文件,内容如下 # Copyright 2007-2008 The Android Open Source Project 2 3 LOCAL_PATH:= $(cal ...
- vim 窗口分割命令
如何在一个窗口下面同时打开两个以及以上的文件,有横向跟纵向两种方式 一.如果在终端中开没有打开vim,可以: 横向分割显示: $ vim -o filename1 filename2 纵向分割显示: ...
- Impala架构
Impala是Cloudera在受到Google的Dremel启发下开发的实时交互SQL大数据查询工具,Impala没有再使用缓慢的 Hive+MapReduce批处理,而是通过使用与商用并行关系数据 ...
- 基于面向对象js的弹窗的组件的开发案例
var aInput = document.getElementsByTagName("input"); 2 aInput[0].onclick = function() { 3 ...
- Windows 下 Sublime Text 默认打开方式问题解决办法
Sublime Text 2 是很受ACMer喜爱的文本编辑器 但是绿色版删除后无法设置为默认打开方式...而且网上也没有给出明确的解决办法 注册表的解决办法: 删除 HKEY_CURRENT_USE ...
- 前端上传文件 后端PHP获取文件
<body> <form action="03-post-file.php" method="post" enctype="mult ...
- Vue路由query传参
1.不要进行过深的嵌套 let id = 'uyu' this.$router.push({ path: '/mrp_detail', query: { re_order_id: id, option ...
- 常见c#正则表达式类学习整理
1.MatchCollection类 用于输入字符串所找到的成功匹配的集合,Regex.Matches 方法返回 MatchCollection 对象 用法 //str:要搜索匹配项的字符串 patt ...
- How to remove a Data Guard Configuration from Primary Database (文档 ID 733794.1)
APPLIES TO: Oracle Database - Enterprise Edition - Version 10.1.0.2 to 11.2.0.3 [Release 10.1 to 11. ...