Sometimes, we run into situations where we end up with a Maybe within the context of another Maybe. Nested structures like this can get really confusing to work with. In this lesson, we’ll look at an example of this and see how chaincan help us out by flattening the nested Maybe

As we all know, inside an array return another array is nested array.

Inside observable return another observable get Observable of Observable.

The same inside Just return another Just, we get Just of Just:

const prop = require('crocks/Maybe/prop');
const propPath = require('crocks/Maybe/propPath'); /**
* return Maybe type
*/
const getUser = id =>
new Promise((resolve, reject) => {
const result = {
status: ,
body: {
id: ,
username: 'tester',
email: 'test@gmail.com',
address: {
street: '111 E. West St',
city: 'Anywhere',
state: 'PA',
postalCode: '19123-4567'
}
}
}
resolve(prop('body', result)); // return Just {}
}); const getPostalCode = propPath(['address', 'postalCode']); getUser()
.then(user => user.map(getPostalCode)) // map to Just {} --> Just Just '19123-4567'
.then(console.log); // Just Just "19123-4567"

Inside getUser function we return a Just type object. Then from propPath, we get Just Just type object. That's why we need flatten it.

The way to solve the problem is using flat map which is called 'chain' in Crocks.

getUser()
.then(user => user.chain(getPostalCode).option("not available"))
.then(console.log); // "19123-4567"

[Javascript Crocks] Flatten Nested Maybes with `chain`的更多相关文章

  1. [LintCode] Flatten Nested List Iterator 压平嵌套链表迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  2. [Javascript Crocks] Safely Access Nested Object Properties with `propPath`

    In this lesson, we’ll look at the propPath utility function. We’ll ask for a property multiple level ...

  3. [Javascript Crocks] Recover from a Nothing with the `alt` method

    Once we’re using Maybes throughout our code, it stands to reason that at some point we’ll get a Mayb ...

  4. [LeetCode] Flatten Nested List Iterator 压平嵌套链表迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  5. Leetcode: Flatten Nested List Iterator

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  6. JavaScript的语法要点 2 - Scope Chain

    前文所述,JavaScript是基于词法作用域(lexically scoped)的,所以标识符被固定在它们被定义的作用域而不是语法上或是其被调用时的作用域.即全局变量的作用域是整个程序,局部变量的作 ...

  7. [Swift]LeetCode341. 压平嵌套链表迭代器 | Flatten Nested List Iterator

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  8. [leetcode]341. Flatten Nested List Iterator展开嵌套列表的迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  9. Flatten Nested List Iterator

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

随机推荐

  1. Squares(枚举+set 查找)

    http://poj.org/problem?id=2002 题意:给出n组坐标,判断这些坐标能组成的正方形的个数. 思路:参考某大神的想法,先枚举两个点,然后利用公式表示出另外两个点,判断这两个点是 ...

  2. POJ 3083 BFS+DFS 40行

    题意:给你一个迷宫. 先输出当左转优先的时候走的路程长度,再输出当右转优先时走的路程长度,最后输出从起点到终点的最短路程长度. 嗯嗯 奴哥活跃气氛的题.随便写了写.. 此题 知道了思路以后就是水题了. ...

  3. R - Games

    Problem description Manao works on a sports TV. He's spent much time watching the football games of ...

  4. Laravel5.1学习笔记10 系统架构2 应用程序结构

    应用程序结构 简介 根目录 App 目录 为应用程序设置命名空间 简介 默认的 Laravel 应用程序结构是为了给无论构建大型还是小型应用程序都提供一个良好的开始.当然,你可以依照喜好自由地组织应用 ...

  5. vue-cli脚手架安装过程(精简版)

    1:打开node的控制台,并找到对应的文件夹 2:检查node的版本 node -v 3:检查npm的版本 npm -v 4:检查cnpm的版本 cnpm -v 5:安装全局及脚手架 cnpm ins ...

  6. python监听鼠标和键盘

    import PyHook3 def OnMouseEvent(event): print('MessageName:',event.MessageName) print('Message:',eve ...

  7. webSphere

    WebSphere 是 IBM 的软件平台.它包含了编写.运行和监视全天候的工业强度的随需应变 Web 应用程序和跨平台.跨产品解决方案所需要的整个中间件基础设施,如服务器.服务和工具.WebSphe ...

  8. beetl模板入门例子

    加入maven依赖 <dependency> <groupId>org.beetl</groupId> <artifactId>beetl-core&l ...

  9. (转)Arcgis for JS实现台风运动路径与影像范围的显示

    http://blog.csdn.net/gisshixisheng/article/details/42025435 首先,看看具体的效果: 初始化状态 绘制中 绘制完成 首先,组织数据.我组织的数 ...

  10. ssl_protocols和ssl_ciphers应该怎么配置

    http://wiki.nginx.org/HttpSslModule#ssl_ciphers 推荐配置: A) 在Apache 的 SSL 配置中禁用 SSLv3 和 SSLv3SSLProtoco ...