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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. JavaScript ES6 Arrow Functions(箭头函数)

    1. 介绍 第一眼看到ES6新增加的 arrow function 时,感觉非常像 lambda 表达式. 那么arrow function是干什么的呢?可以看作为匿名函数的简写方式. 如: var ...

  7. [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 ...

  8. [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 ...

  9. [Javascript] Refactoring: Polymorphic Functions

    if-statements can add serious complexity and beg for refactoring. You can use polymorphic functions ...

随机推荐

  1. ubuntu下mysql的安装

    1.在终端输入 sudo apt-get install mysql-server mysql-client 2.在此安装过程中会让你输入root用户(管理MySQL数据库用户,非Linux系统用户) ...

  2. CUDA学习笔记3:CUFFT(CUDA提供了封装好的CUFFT库)的使用例子

    一.FFT介绍 傅里叶变换是数字信号处理领域一个很重要的数学变换,它用来实现将信号从时域到频域的变换,在物理学.数论.组合数学.信号处理.概率.统计.密码学.声学.光学等领域有广泛的应用.离散傅里叶变 ...

  3. HDU 6138 Fleet of the Eternal Throne(AC自动机)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6138 [题目大意] 给出一些串,询问第x个串和第y个串的公共子串, 同时要求该公共子串为某个串的前 ...

  4. AGC026D Histogram Coloring

    link 题意: 给定n列的方块,第i列高度$h_i$.现在要把它染成红蓝两色,要求满足:对于任意一个$2\times 2$的区域,恰有2个蓝色,2个红色.问方案数. $n\leq 100,h_i\l ...

  5. 04-RocketMQ入门及其使用(一)

    视频开始主要介绍数据库逻辑以及分表相关的设计. 关键的数据库读写分析操作---

  6. Codeforces Round #299 (Div. 2) B. Tavas and SaDDas 水题

    B. Tavas and SaDDas Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/535/p ...

  7. Linux知识(6)----VIM

    vi的第一版是由Bill Joy在1978年写成的,当时他是UC Berkeley的学生.后来他共同创建了神奇的Sun公司.vi来源于visual一词,目标是在终端上可视化地模拟文本的编辑,是的更人性 ...

  8. Marvell w8782 sdio wifi AP模式设置

    http://blog.csdn.net/junllee/article/details/8895908 w8782工作在station模式一切正常,于是想试试AP模式(master mode): A ...

  9. oracle定时任务(dbms_job)

    author:skate time:2007-09-12 http://publish.it168.com/2006/0311/20060311017002.shtml 今天总结下Oracle的任务队 ...

  10. Spring MapFactoryBean例子

    MapFactoryBean类为开发者提供了一种在Spring的bean配置文件中创建一个具体的Map集合类(HashMap和TreeMap). 这里有一个MapFactoryBean.例如,在运行时 ...