[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 ...
随机推荐
- Repractise基础篇:Web应用开发七日谈
Repractise基础篇:Web应用开发七日谈 本来想的仅仅是画一个例如以下的七日图来说说Web开发的.随后又想了想这似乎是一个非常棒的Web开发相关的知识介绍.应用开发是一个非常有意思的循环,多数 ...
- JSTL之C标签学习
JSTL 核心标签库标签共有13个,功能上分为4类: 1.表达式控制标签:out.set.remove.catch 2.流程控制标签:if.choose.when.otherwise 3.循环标签:f ...
- golang pipe
===============golang pipe============== package main import ( "fmt" "io" ) func ...
- mysql 实行模糊查询 一个输入值匹配多个字段和多个输入值匹配一个字段
mysql 实行模糊查询 一个输入值匹配多个字段 MySQL单表多字段模糊查询可以通过下面这个SQL查询实现 为啥一定要150字以上 真的麻烦 还不让贴代码了 SELECT * FROM `ma ...
- 【AIM Tech Round 4 (Div. 2) B】Rectangles
[链接]http://codeforces.com/contest/844/problem/B [题意] 也是道计数水题,没什么记录意义 [题解] 枚举每个点的位置在,然后往右往下 枚举和它一样颜色的 ...
- Android 内存监测工具
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 文/幻影浪子 [博主导读]俗话说:工欲善其事必先利其器!我们先来了解下内存监测工具是怎么使用的?为内 ...
- COGS——T 2057. [ZLXOI2015]殉国
http://cogs.pro/cogs/problem/problem.php?pid=2057 ★☆ 输入文件:BlackHawk.in 输出文件:BlackHawk.out 评测插件 ...
- [TS] Swap two element in the array (mutation)
Shuffling is a common process used with randomizing the order for a deck of cards. The key property ...
- Android .getRGB得到是负数,解决方案
情景:ava.awt.color 下面的getRGB怎么得出的是负数???本来想通过getRGB得到一个整数,在另外的一个部分在根据这个整数构件一个color,因为参数规定只能能传整数!!!color ...
- VUE笔记 - 列表过渡动画 v-enter, v-leave-to | v-enter-active, v-leave-active | v-move
本例要结合过渡动画四个过程的示意图一起理解. https://cn.vuejs.org/v2/guide/transitions.html 疑问: v-for="(item,i) in li ...