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. hash_set和hash_map

    1.hash_set集合容器 hash_set利用链式哈希表,进行数据的插入.删除和搜索.与set容器同样,不同意插入反复键值的元素.SGIC++哈希表是一个链式的结构,由表头和一系列单链组成.表头是 ...

  2. erlang虚拟机代码运行原理

    erlang是开源的,非常多人都研究过源码.可是.从erlang代码到c代码.这是个不小的跨度.并且代码也比較复杂. 所以这里,我利用一些时间,整理下erlang代码的运行过程.从erlang代码编译 ...

  3. worktools-git 工具的使用总结(2)

    1.创建分支 git branch son parent //创建分支,是在master 分支的基础上创建 :~/myGit$ git st # On branch master nothing to ...

  4. 项目列表dl、dt、dd使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. BZOJ2527: [Poi2011]Meteors(整体二分)

    Description Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby gala ...

  6. 设计模式六大原则(四):接口隔离原则(Interface Segregation Principle)

    接口隔离原则(ISP)定义: 客户端不应该依赖它不需要的接口:一个类对另一个类的依赖应该建立在最小的接口上. 问题由来: 类A通过接口I依赖类B,类C通过接口I依赖类D,如果接口I对于类A和类B来说不 ...

  7. unity3d 改动gui label颜色,定义颜色需除以256

    GUIStyle titleStyle2 = new GUIStyle(); titleStyle2.fontSize = 20; titleStyle2.normal.textColor = new ...

  8. python 命令行參数解析

    本文是从我还有一个博客转载过来的,欢迎大家点击进去看一下,帮我添加点人气^_^ ImPyy 选择模块 依据python參考手冊的提示,optparse 已经废弃,应使用 argparse 教程 概念 ...

  9. eclipse-hierarchyviewer 不能使用

    今天安装了adt-bundle以后,发现hierarchyviewer不能用.点开了以后连手机没有效果.后来发现,还需要进入hierarchyviewer所在的sdk目录进行下权限的设置 chmod ...

  10. ORACLE10g R2【RAC+ASM→单实例FS】

    ORACLE10g R2[RAC+ASM→单实例FS] 10g R2 RAC+ASMà单实例FS的DG,建议禁用OMF. 本演示案例所用环境:   primary standby OS Hostnam ...