[Javascript Crocks] Compose Functions for Reusability with the Maybe Type
We can dot-chain our way to great success with an instance of Maybe, but there are probably cases where you need to apply the same series of transformations against different Maybes. In this lesson, we’ll look at some point-free versions of some common Maybe methods and see how we can compose them together to get a reusable function that can be applied to any Maybe instance.
We are going to rewrite the following code by using function composion:
const crocks = require('crocks');
const {and, isString, Maybe, prop, safe, option, map, alt, chain} = crocks;
const {not, isEmpty, compose, converge, join, split, toLower} = require('ramda');
///////////////UTILS/////////////////
const joinKey = compose(join('_'), split(' '), toLower);
const isNotEmpty = compose(
not,
isEmpty
)
const isNonEmptyString = and(isNotEmpty, isString);
/*const isNonEmptyString = R.converge(
R.and,
[
isNotEmpty,
isString
]
);*/
const createUrl = key =>`https://egghead.io/articles/${joinKey(key)}`;
////////////////MAIN////////////////
const article = {
id: ,
name: 'Learn FP with this one weird trick'
};
/*
const getUrl = obj =>
prop('name', obj) // Maybe(string)
.chain(safe(isNonEmptyString)) // Maybe(string) --safe(isNonEmptyString)--> Maybe(Maybe(String)) --chain--> Maybe(String)
.alt(Maybe.of('Nope')) // Nothing -> Just('Nope')
.map(createUrl)
.option('default');
*/
const getSafeName = compose(
chain(safe(isNonEmptyString)),
prop('name')
);
const getUrlOrDefault = compose(
option('Not valid URL'),
map(createUrl)
);
const getUrl = compose(
getUrlOrDefault,
getSafeName
);
const getUrlOrNope = compose(
getUrlOrDefault,
alt(Maybe.of('Nope')),
getSafeName
)
const res = getUrl(article);
console.log(res);
[Javascript Crocks] Compose Functions for Reusability with the Maybe Type的更多相关文章
- [Javascript Crocks] Make your own functions safer by lifting them into a Maybe context
You probably have functions that aren’t equipped to accept a Maybe as an argument. And in most cases ...
- [Javascript Crocks] Create a Maybe with a `safe` Utility Function
In this lesson, we’ll create a safe function that gives us a flexible way to create Maybes based on ...
- [Javascript Crocks] Apply a function in a Maybe context to Maybe inputs (curry & ap & liftA2)
Functions are first class in JavaScript. This means we can treat a function like any other data. Thi ...
- [Javascript Crocks] Recover from a Nothing with the `coalesce` Method
The alt method allows us to recover from a nothing with a default Maybe, but sometimes our recovery ...
- [Javascript Crocks] Safely Access Object Properties with `prop`
In this lesson, we’ll use a Maybe to safely operate on properties of an object that could be undefin ...
- JavaScript ES6 Arrow Functions(箭头函数)
1. 介绍 第一眼看到ES6新增加的 arrow function 时,感觉非常像 lambda 表达式. 那么arrow function是干什么的呢?可以看作为匿名函数的简写方式. 如: var ...
- [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 ...
- [Javascript Crocks] Understand the Maybe Data Type
In this lesson, we’ll get started with the Maybe type. We’ll look at the underlying Just and Nothing ...
- [Javascript] Refactoring: Polymorphic Functions
if-statements can add serious complexity and beg for refactoring. You can use polymorphic functions ...
随机推荐
- 前端使用express mock数据
项目中使用的是RESTFUL接口规范,项目框架用的是vue,项目开始时,调研了几个比较有名的mock数据的插件:比如webpack的中间件api-mock,json-server,mockjs,还有e ...
- 深入理解javascript作用域系列第二篇
前面的话 大多数时候,我们对作用域产生混乱的主要原因是分不清楚应该按照函数位置的嵌套顺序,还是按照函数的调用顺序进行变量查找.再加上this机制的干扰,使得变量查找极易出错.这实际上是由两种作用域工作 ...
- git实现github仓库和本地仓库同步
配置git 安装git以后,打开git bash,首先要对git进行配置,输入 git config --global username "你的名字" git config --g ...
- 【51Nod 1756】【算法马拉松 23】谷歌的恐龙
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1765 设答案为\(X\). 则\[X=\frac{m}{n}\times ...
- 51nod1380 夹克老爷的逢三抽一
问题等价于选出$n / 3$个不相邻元素是权值和最大 这是一个经典贪心问题,同种树,拿堆维护即可,复杂度$O(n \log n)$ #include <queue> #include &l ...
- zoj 3329 概率dp
题意:有三个骰子,分别有k1,k2,k3个面.每个面值为1--kn每次掷骰子,如果三个面分别为a,b,c则分数置0,否则加上三个骰子的分数之和.当分数大于n时结束.求游戏的期望步数.初始分数为0 链接 ...
- iframe里面的页面调用父窗口,左右窗口js函数的方法
iframe里面的页面调用父窗口,左右窗口js函数的方法 实现iframe内部页面直接调用该iframe所属父窗口自定义函数的方法. 比如有A窗口,A内有个IFRAME B,B里面的装载的是C页面 ...
- Windows蓝屏dump文件查看器(转)
Windbg-分析Windows蓝屏原因利器[转]下载地址先声明下,虽然用windbg诊断蓝屏之前网络上已经有人发过教程了,但就我而言, 学会使用windbg来诊断蓝屏也算是自己的原创吧.以前看一个微 ...
- Java常量字符串String理解 String理解
以前关于String的理解仅限于三点:1.String 是final类,不可继承2.String 类比较字符串相等时时不能用“ == ”,只能用 "equals" 3.Strin ...
- Delphi CompilerVersion Constant / Compiler Conditional Defines
http://delphi.wikia.com/wiki/CompilerVersion_Constant The CompilerVersion constant identifies the in ...