[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)[0]
  return found ? ys : ys.concat(x);
}

const concatUniq = (x, ys) =>
  fromNullable(ys.filter(y => y === x)[0]) // 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(/..../)
  )
时间: 2024-08-25 02:37:19

[JS Compse] 4. A collection of Either examples compared to imperative code的相关文章

Three.js 教程和示例(Tutorials and Examples)

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 摘录以下部分进行翻译: Tutorials and Examples Now that you are sold on Three.js, whe

QRCode.js网址太长生成二维码报错:Code Length OverFlow Error

当网址的长度有2百多时,使用QRCode生成二维码报错: Code Length OverFlow Error 解决方法: 到 https://github.com/KeeeX/qrcodejs 下载 qrcode.js 或 qrcode.min.js 替换掉原来的,再刷新页面,问题搞定! 原文地址:http://blog.51cto.com/wenguonideshou/2152434

[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-nested expression. For example, we have code: const moneyToFloat = str => { const cost = str.replace(/\$/g, ''); return parseFloat(cost); } const percentT

使用Nightwatch.js做基于浏览器的web应用自动测试

1        安装 1.1   安装Node.js 在http://nodejs.org/ 上下载适合本机系统的安装包运行安装,注意安装选项中选择npm tool以用于后续依赖包的安装. 1.2   通过npm工具安装Nightwatch 命令行运行"npm install nightwatch",如下的提示表明安装成功. 1.3   Npm相关目录结构 所有npm安装的模块都会基于当前cmd窗口的目录,也就是说如果cmd的工作目录是在c:\根目录,则会在该目录下创建node_mo

sea.js,spm学习

安装spm 下载sea.js 运行spm npm install [email protected] -g npm install spm-build -g 下载sea.js git clone https://github.com/seajs/examples.git cd examples/static/hello spm-build #编译到dist make deploy #复制到正式环境 下一步:运用sea.js 构建工具,创建web页面

video.js视频播放器

免费视频播放器videojs中文教程 Video.js是一款web视频播放器,支持html5和flash两种播放方式.更多关于video.js的介绍,可以访问官方网站介绍,我之前也写过一篇关于video.js的使用心得,有兴趣的可以点这里 , 阅读的人数还蛮多的,有些热心的读者甚至还给过我小额打赏,钱虽不多,但是很感动.最几天又收到几位网友的私信,问一些关于videojs使用方面的问题.我自己都不记得videojs长什么模样了,出于别人对我的信任,又回头看了一遍上一篇文章,还是2014年的时候写

THREE.js 学习笔记(一)

一. 获取THREE.js three.js的代码托管在github上面,https://github.com/mrdoob/three.js/ 我们可以用git来获取代码,闲麻烦的话也可以直接下载zip文件. 二. 目录结构 拿到代码后先看下three.js的目录结构 |-build |-custom |-Three.js |-examples |-gui |-src |-cameras |-core |-extras |-lights |-materials |-objects |-rend

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

原文:JS组件系列--表格组件神器:bootstrap table(二:父子表和行列调序) 前言:上篇 JS组件系列——表格组件神器:bootstrap table 简单介绍了下Bootstrap Table的基础用法,没想到讨论还挺热烈的.有园友在评论中提到了父子表的用法,今天就结合Bootstrap table的父子表和行列调序的用法再来介绍下它稍微高级点的用法. bootstrap table系列: JS组件系列——表格组件神器:bootstrap table JS组件系列——表格组件神器

(转载)SQL Reporting Services (Expression Examples)

https://msdn.microsoft.com/en-us/library/ms157328(v=SQL.100).aspx Expressions are used frequently in reports to control content and report appearance. Expressions are written in Microsoft Visual Basic, and can use built-in functions, custom code, glo