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. jsp页面动态展示list-使用<select>和<c:forEach>标签

    转自:https://blog.csdn.net/zhugewochuang/article/details/80276466 后台:搜索数据放入list,然后为这个list提供响应的get和set方 ...

  2. 优先队列 + 并查集 + 字典树 + 欧拉回路 + 树状数组 + 线段树 + 线段树点更新 + KMP +AC自动机 + 扫描线

    这里给出基本思想和实现代码 . 优先队列 : 曾经做过的一道例题       坦克大战 struct node { int x,y,step; friend bool operator <(no ...

  3. BZOJ 1511 KMP

    题意:求出每个前缀的最长周期之和(等于本身的算0) 思路: 求出来next数组  建出next树 找到不为0的最小的 n减去它就是答案 //By SiriusRen #include <cstd ...

  4. BZOJ 4800 折半暴搜

    思路: 把它拆成两半  分别搜一发 两部分分别排好序 用two-pointers扫一遍 就可以了. (读入也要用long long) //By SiriusRen #include <cstdi ...

  5. B - Alyona and mex(构造)

    Problem description Alyona's mother wants to present an array of n non-negative integers to Alyona. ...

  6. Kali linux 2016.2(Rolling)之 Nessus安装及Plugins Download Fail 解决方法

    最近,因科研需要,学习Nessus. Nessus是一款优秀的漏洞扫描软件,在其v6 HOME版本中在线更新漏洞插件不成功,采用离线更新,成功地更新了插件,在此将更新方法进行分享. 1.Nessus软 ...

  7. Android 微博sdk接入授权指南

    1:首先在微博官方注册账号,官方地址是:http://open.weibo.com/然后创建一个新应用.     2:当然我们得现在自己电脑上创建一个应用,例如包名叫com.winorout.weib ...

  8. VMWare 在物理机待机后,报错“该虚拟机似乎正在使用中”

    在物理机待机后,刚打开虚拟机,就弹出这个画面(这种情况经常出现在远程之后,本机待机之后) 点击确定后,就弹出 当点击取消,无反应,而且再次点击VM2又弹出以上窗口,点击获取所有权,则弹出以下窗口 上网 ...

  9. dubbo之结果缓存

    结果缓存,用于加速热门数据的访问速度,Dubbo提供声明式缓存,以减少用户加缓存的工作量. lru 基于最近最少使用原则删除多余缓存,保持最热的数据被缓存. threadlocal 当前线程缓存,比如 ...

  10. Caffe: gflag编译出现问题汇总

    1. 使用Unicode字符集: 出现问题 E:\CodeBase\ML\Caffe\ThirdPartySrc\gflags-master\src\gflags.cc(1340): error C2 ...