[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 Maybe
s. 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 ...
随机推荐
- jquery DOM操作(一)
上一篇文章是记录了jquery中选择器的作用,这里只要记录下jquery中的DOM操作,还是按照上篇的格式来. 下面是测试用的html代码,接下来DOM的操作会在下面的代码下进行. <body& ...
- Django+Nginx+uwsgi搭建自己的博客(三)
(本来打算在这篇博文中介绍Users App的前端部分的,但写着写着就发现还需要铺垫很多东西才能把整个项目串的比较流畅些,因此这篇就继续介绍了后端的一些东西,前端的部分只好跳票到下一篇了-) 在上一篇 ...
- Mat矩阵(图像容器)创建及CV_8UC1、CV_8UC2等参数详解
CV_<bit_depth>(S|U|F)C<number_of_channels> 1--bit_depth---比特数---代表8bite,16bites,32bites, ...
- JAVA基础关键字小结一
基础部分总是看起来简单,若要明白原理和使用场景,需要慢慢的体会. 本文对常用的关键字如final,transient,volatile,static以及foreach循环的原理重新梳理了一遍. 一.f ...
- TP5视频教程课程内容
<TP5 视频教程课程内容> 一.ThinkPHP5TP5 官网基础教程, 官网手册作为参考,讲解TP5的使用方法.理解TP的用途 二.TP5大型项目实战及底层源码分析用TP5 做大型电商 ...
- HDU 6205[计算几何,JAVA]
题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=6206] 题意: 给出不共线的三个点,和一个点(x,y),然后判断(x,y)在不在这三个点组成的圆外. ...
- POJ3710 Christmas Game 博弈论 sg函数 树的删边游戏
http://poj.org/problem?id=3710 叶子节点的 SG 值为0:中间节点的SG值为它的所有子节点的SG值加1后的异或和. 偶环可以视作一个点,奇环视为一条边(连了两个点). 这 ...
- Ubuntu 12.04下spark1.0.0 集群搭建(原创)
spark1.0.0新版本的于2014-05-30正式发布啦,新的spark版本带来了很多新的特性,提供了更好的API支持,spark1.0.0增加了Spark SQL组件,增强了标准库(ML.str ...
- bzoj 2038 小Z的袜子 莫队算法
题意 给你一个长度序列,有多组询问,每次询问(l,r)任选两个数相同的概率.n <= 50000,数小于等于n. 莫队算法裸题. 莫队算法:将序列分为根号n段,将询问排序,以L所在的块为第一关键 ...
- Markdown 简明语法手册 - 作业
目录 Cmd Markdown 简明语法手册 1. 内容目录 2. 标签分类 3. 删除线 水平线--- 1. 斜体和粗体 2. 分级标题 标题1 标题2 标题3 3. 外链接 4. 无序列表 5. ...