[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 ...
随机推荐
- C++ 四种显示转换
转自:http://www.jellythink.com/archives/205 (果冻想) 前言 这篇文章总结的是C++中的类型转换,这些小的知识点,有的时候,自己不是很注意,但是在实际开发中 ...
- [C#] 实现可设置超时的 Task
前言 如何实现支持超时的 Task ? 关键点: Task.WhenAny 实现 一个扩展方法就可以搞定. // 有返回值 public static async Task<TResult> ...
- centos7-硬盘坏道检测
#检测坏道命令,结果输出到 /home/badblocks.log badblock -s -v -o /home/badblocks.log /dev/sdb1 [注]:硬盘损坏程度不同,block ...
- 初识Linux 基础操作(2)
1.Linux启动流程 1).linux启动过程 ①.进入grub界面选择相应的启动内核 ②.读取kernel内核文件-/boot/vmlinuz-* ...
- Matrix-tree定理 spoj HIGH
Matrix-tree定理,给出一个无向图,问求出的生成树方案有多少种方案,利用Matrix-tree定理,主对角线第i行是i的度数,(i,j) 值为i和j之间边的数量,然后删去第一行第一列,利用初等 ...
- [CF226E]Noble Knight's Path
[CF226E]Noble Knight's Path 题目大意: 一棵\(n(n\le10^5)\)个结点的树,初始时所有结点都是白色.\(m(m\le10^5)\)次操作,操作包含以下两种: 将点 ...
- 【原创】MySQL+MyEclipse+对象映射文件,schema与category的关系
(一) 1.映射文件的类如下写法:class name="com.sanqing.po.SysUser" table="sys_user" catalog=& ...
- HDU 5645 DZY Loves Balls 水题
DZY Loves Balls 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5645 Description DZY loves playing b ...
- CodeM资格赛3
题目描述 美团点评上有很多餐馆优惠券,用户可以在美团点评App上购买.每种优惠券有一个唯一的正整数编号.每个人可以拥有多张优惠券,但每种优惠券只能同时拥有至多一张.每种优惠券可以在使用之后继续购买. ...
- 动软代码生成器连接Oracle 11g
首先要说明的是:如果你连接的是远程的Oracle服务器,你本地机器必须装Oracle客户端,然后 用sqldeveloper 先建立一个连接. 然后你才能用.NET动软代码生成器连接到数据库. 因 ...