[Javascript Crocks] Flatten Nested Maybes with `chain`
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 chain
can 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`的更多相关文章
- [LintCode] Flatten Nested List Iterator 压平嵌套链表迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- [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 ...
- [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 ...
- [LeetCode] Flatten Nested List Iterator 压平嵌套链表迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- Leetcode: Flatten Nested List Iterator
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- JavaScript的语法要点 2 - Scope Chain
前文所述,JavaScript是基于词法作用域(lexically scoped)的,所以标识符被固定在它们被定义的作用域而不是语法上或是其被调用时的作用域.即全局变量的作用域是整个程序,局部变量的作 ...
- [Swift]LeetCode341. 压平嵌套链表迭代器 | Flatten Nested List Iterator
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- [leetcode]341. Flatten Nested List Iterator展开嵌套列表的迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- Flatten Nested List Iterator
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
随机推荐
- E20170829-mk
Parse vt. 从语法上描述或分析(词句等); serial adj. 连续的; 连载的; 顺序排列的; 分期偿付的; MultiThread n. 多线程; 多流; concurren ...
- 用xftp从win7系统传输一些必要的文件到Linux
新建会话,主机名为Linux系统的ip地址,选用SFTP协议,选用UTF-8编码格式 1.安装JDK 切换到java路径下 卸载openJDK: 用rpm -qa |grep java指令查看 用rp ...
- weui&flexible布局
1.weui 一开始以为只能用于小程序中,原来分两种:weui-wxss-master和weui-master.真的是强大的不得了,把设计好的样式和功能封装.然后分类,有明确的层级和逻辑,感动!!值得 ...
- POJ 1985 求树的直径 两边搜OR DP
Cow Marathon Description After hearing about the epidemic of obesity in the USA, Farmer John wants h ...
- js-var变量作用域
看代码: var a=10; function fn1(){ alert(a); var a=20; alert(a); } 运行结果:undefined 和 20 注意: 在函数内,变量如没用var ...
- PL/SQL之高级篇
原文地址:http://www.cnblogs.com/sin90lzc/archive/2012/08/30/2661117.html 参考文献:<Oracle完全学习手册> 1.概述 ...
- Embedded之Stack之一
1 Intro When a program starts executing, a certain contiguous section of memory is set aside for the ...
- 转:Fiddler抓包工具总结
http://www.cnblogs.com/yyhh/p/5140852.html#l02
- Higher-Order Functions and Lambdas
https://kotlinlang.org/docs/reference/lambdas.html
- python排序sorted与sort比较 (转)
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. sorted(iterable,key=None,revers ...