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的更多相关文章

  1. [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 ...

  2. backbone.js 教程(1) View & Model & Collection

    Backbone.js Overview 它由Jeremy Ashkenas开发,最初发行于2010-10-13 它是一个轻量的JavaScript类库,只依赖于underscore.js,非强制依赖 ...

  3. (转载)SQL Reporting Services (Expression Examples)

    https://msdn.microsoft.com/en-us/library/ms157328(v=SQL.100).aspx Expressions are used frequently in ...

  4. JS组件系列——表格组件神器:bootstrap table(二:父子表和行列调序)

    前言:上篇 JS组件系列——表格组件神器:bootstrap table 简单介绍了下Bootstrap Table的基础用法,没想到讨论还挺热烈的.有园友在评论中提到了父子表的用法,今天就结合Boo ...

  5. 2.MongoDB 基于node.js访问和操作集合

    对于频繁使用的Node.js来说,常见的任务是集合的动态操控. 较大的安装给每个大客户一个单独的集合,以便客户登入或离开时.根据需要添加或删除集合. MongoDB Node.js 驱动程序 Db和C ...

  6. 用Backbone.js创建一个联系人管理系统(一)

    原文 Build a Contacts Manager Using Backbone.js: Part 1 在这个教程里我们将会使用Backbone.js,Underscore.js,JQuery创建 ...

  7. 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 ...

  8. sea.js,spm学习

    安装spm 下载sea.js 运行spm npm install spm@2.x -g npm install spm-build -g 下载sea.js git clone https://gith ...

  9. 使用Three.js网页引擎创建酷炫的3D效果的标签墙

    使用Three.js引擎(这是开源的webgl三维引擎,gitgub)进行一个简单应用. 做一个酷炫的3d效果的标签墙(已经放在我的博客首页,大屏幕可见), 去我的博客首页看看实际效果 www.son ...

随机推荐

  1. C++里面virtual函数及虚表大小

    实验了下面的函数: #include <vector> #include <iostream> using namespace std; class A { public: v ...

  2. UVA 11346 Probability (几何概型, 积分)

    题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=2321">https://uva ...

  3. Mysql基本增删改查

    1登陆服务器 mysql -h localhost -u username -p password 2查看存在数据库 show databases; 3创建一个数据库(例如名字为class1,以下都是 ...

  4. 最小生成树(MST,minimum spanning tree)

    生成树:由图生成的树,由图转化为树,进一步可用对树的相关操作来对图进行操作.最小指的是权值最小: 生成树是边的集合,如下图所示的最小生成树:MST={{a,b},{a,f},{f,c}} 本文主要探讨 ...

  5. Spider_scrapy

    多线程爬虫 进程线程回顾 进程 系统中正在运行的一个应用程序 1个CPU核心1次只能执行1个进程,其他进程处于非运行状态 N个CPU核心可同时执行N个任务 线程 进程中包含的执行单元,1个进程可包含多 ...

  6. CISP/CISA 每日一题 14

    CISA 每日一题(答) 自动无人值守运行(LIGHTS-OUT)优势:1.信息系统运行成本的遏制/减少:2.持续运行(24/7):3.减少系统错误和中断次数. I/O 控制人员负责保证:1.批处理信 ...

  7. RFID的工作流程

    工作流程 1.阅读器通过发射天线发送一定频率的射频信号, 2.当射频卡进入发射天线工作区域时产生感应电流,射频卡获得能量被激活: 3.射频卡将自身编码等信息通过卡内置发送天线发送出去 4.系统接收天线 ...

  8. 洛谷 P1416 攻击火星

    P1416 攻击火星 题目描述 一群外星人将要攻击火星. 火星的地图是一个n个点的无向图.这伙外星人将按照如下方法入侵,先攻击度为0的点(相当于从图中删除掉它),然后是度为1的点,依此类推直到度为n- ...

  9. Lamp(linux+apache+mysql+php)环境搭建

    Lamp(linux+apache+mysql+php)环境搭建 .安装apache2:sudo apt-get installapache2 安装完毕后.执行例如以下命令重新启动apache:sud ...

  10. Python的数组合并

    https://blog.csdn.net/hustqb/article/details/78090365 TypeError: can only concatenate list (not &quo ...