[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 ...
随机推荐
- iOS 9音频应用播放音频之ios9音频基本功能
iOS 9音频应用播放音频之ios9音频基本功能 在iOS 9音频应用开发中最为简单和常用的就是AVFoundation框架中的AVAudioPlayer类.虽然AVAudioPlayer类不能播放网 ...
- POJ 3904 JZYZOJ 1202 Sky Code 莫比乌斯反演 组合数
http://poj.org/problem?id=3904 题意:给一些数,求在这些数中找出四个数互质的方案数. 莫比乌斯反演的式子有两种形式http://blog.csdn.net/out ...
- 入侵91网直到拿下服务器#并泄露150w+用户信息
在补天看到一厂商 首先挖到一处注入 http://www.91taoke.com/index.php?m=Dayi&a=answer&aid=26313 此处注入是dba权限 打算使用 ...
- MYSQL学习笔记 (三)JOIN用法
数据库的操作分开增删改查,其中查询操作基本占系统的90%,大家所说的优化SQL语句基本是优化查询语句.接下来将学习JOIN的用法,JOIN包括:INNER JOIN(内连接).LEFT JOIN(左外 ...
- 自定义IPython Qt Console 窗口大小、字体、颜色
windows下将IPython Qt Console的快捷方式修改为: "C:\Python\IPython Qt Console.exe" --ConsoleWidget.fo ...
- maven,阿里云国内镜像,提高jar包下载速度
镜像 maven默认会从中央仓库下载jar包,这个仓库在国外,而且全世界的人都会从这里下载,所以下载速度肯定是非常慢的.镜像就相当于是中央仓库的一个副本,内容和中央仓库完全一样,目前有不少国内镜像,其 ...
- Struts2的ActionError&ActionMessage示例
本教程显示使用Struts2的 ActionError 和 ActionMessage 类. 1. ActionError – 是用来发送错误信息反馈给用户 - 通过 <s:actionerro ...
- 【转载】利用Matlab制作钟表
静态时钟 hObject=figure; set(hObject,'NumberTitle','off'); set(hObject,'MenuBar','none'); set(hObject,'v ...
- fmri资源站点
1. MRI analysis tutorials:http://www.mccauslandcenter.sc.edu/CRNL/wp-content/tools/tutorial/index.h ...
- fatal error: sys/cdefs.h: No such file or directory
sudo apt-get install g++-multilib