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. 洛谷P3950 部落冲突 [LCT]

    题目传送门 部落冲突 格式难调,体面就不放了. 分析: julao们应该都看得出来就是个$LCT$板子,战争就$cut$,结束就$link$,询问就$find$.没了... 太久没打$LCT$,然后发 ...

  2. java 读入文件 FileInputStream

    package com.mkyong.io; import java.io.File; import java.io.FileInputStream; import java.io.IOExcepti ...

  3. python的进阶--爬虫小试

    代理之说 [ python实现代理服务功能实例 ]  --  https://www.jb51.net/article/43266.htm [检测代理是否有效] -- https://blog.csd ...

  4. RxSwift 系列(七)

    前言 本篇文章将要学习RxSwift中连接操作符.Connectable Observable在订阅时不发射事件消息,而是仅当调用它们的connect()方法时才发射消息,这样就可以等待所有我们想要的 ...

  5. Boolean Expressions POJ - 2106 (表达式求值)

    The objective of the program you are going to produce is to evaluate boolean expressions as the one ...

  6. 【BZOJ 3133】 3133: [Baltic2013]ballmachine (线段树+倍增)

    3133: [Baltic2013]ballmachine Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 148  Solved: 66 Descri ...

  7. 批量 修改 android eclipse 项目名

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 最外层的 文件夹名字.

  8. 2018ECfinal J. Philosophical Balance

    2018ECfinal J. Philosophical Balance 题目大意: 给出一个字符串 \(s\) ,你需要给每一个 \(i\) 一个 \([0,1]\) 之间的权值 \(k_i\) , ...

  9. Codeforces Round #479 (Div. 3)

    手速场2333,这群人贼牛逼!手速贼快!   A. Wrong Subtraction time limit per test 1 second memory limit per test 256 m ...

  10. bzoj 3757 树上莫队

    感谢以下文章作者: http://blog.csdn.net/kuribohg/article/details/41458639 http://vfleaking.blog.163.com/blog/ ...