[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 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`的更多相关文章
- [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 ...
随机推荐
- awk 去重的同时并保持原来的顺序
#-----------awk.awk------------ { if(data[$0]++ == 0) lines[++count] = $0} END { for (i ...
- CSS--浏览器CSS Hack 收集
所谓的Hack就是只有特定浏览器才能识别这段hack代码.Hack 不是什么好东西,除非没有办法,我们尽量还是不要用着玩意. 下面是各个浏览器的CSS Hack 列表. Firefox 浏览器 @-m ...
- EditPlus 1:更改默认编码方式
打开软件点击上面的菜单栏Tools(工具),再找到Configure User Tools(用户配置工具)点击,再找到左边栏File点击,这个时候可以看到右边栏的Default encoding点击可 ...
- SnackDown Online Qualifier 2017
好久没做题了,然后就想着随便做一个.无奈cf都是晚上,然后就看见这个,随便做做. 资格赛,只要做出来1题就行了,4天的时间. 1. 水题 #include <iostream> #incl ...
- vs2008 启动IE浏览器 出现DW20.exe占用大量cpu 服务器iis 异常调试
DW20.exe占用大量cpu 服务器iis运行出现异常想查一下故障原因,发现有好几个DW20.exe进程,每个占用20%左右的cpu,在任务管理器中将其终止后,它又自动运行起来了 查了一下DW20. ...
- wps 2016 个人版 重新开始编号
wps文档重新开始编号,继续编号,自定义编号 首先选中这一行 鼠标右键选中项目符号和编号 单击项目符号和编号,你可以重新开始编号为1,继续前一列表,还可自定义,单击确定按钮就可以实现你想要的结果 效果 ...
- Django模板常用语法规则
Django 模板标签 if/else 标签 for 标签 ifequal/ifnotequal 标签 注释标签 过滤器 include 标签 URL标签 模板继承 if/else 标签 1. 基 ...
- Css打造一个简单的静态七巧板
偶然在微博上看到用css写一个七巧板,正好也有一些源代码,于是就试着敲了敲. 主要是利用了css3的transform,实现平移,旋转,变形,直接用看到的代码敲出来之后有些问题,因为宽度上下面绿色的三 ...
- telerik:RadAsyncUpload 使用 时不执行上传事件的解决办法AsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
一般是因为web.config没有配置的原因! 只要在<handlers>下加上 <add name="Telerik.Web.UI.WebResource" v ...
- Morse理论:拓扑不变性特征匹配原理
设计精美的宽基线双目相机镇文 Mo'ersi lilun莫尔斯理论(卷名:数学) Morse theory 微分拓扑的一个重要分支.通常是指两部分内容:一部分是微分流形上可微函数的莫尔斯理论,即临界点 ...