[Javascript Crocks] Compose Functions for Reusability with the Maybe Type

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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, compose, isEmpty, isString, Maybe, not, prop, safe } = crocks
const { join, split, toLower } = require(ramda)


const article = {
    id: 1,
    name: Learning crocksjs functional programming library
};

const createUrlSlug = compose(join(-), split( ), toLower);
const createUrl = slug => `https://egghead.io/articles/${slug}`;
const createUrlFromName = compose(createUrl, createUrlSlug);
const isNonEmptyString = and(not(isEmpty), isString);


const getArticleName = obj => prop(name, obj)
    .chain(safe(isNonEmptyString)) // If return Nothing then use alt value
    .alt(Maybe.of(page not found));

const getArticleUrl = obj => getArticleName(obj)
    .map(createUrlFromName)
    .option(default);

const url = getArticleUrl(article);

console.log(url)

 

We can import those instance methods directly:

const { and, compose, isEmpty, isString, Maybe, not, prop, safe, chain, map, alt, option } = crocks
const crocks = require(crocks)
const { and, compose, isEmpty, isString, option, Maybe, not, prop, safe, chain, map, alt } = crocks
const { join, split, toLower } = require(ramda)


const article = {
    id: 1,
    _name: Learning crocksjs functional programming library
};

const createUrlSlug = compose(join(-), split( ), toLower);
const createUrl = slug => `https://egghead.io/articles/${slug}`;
const createUrlFromName = compose(createUrl, createUrlSlug);
const isNonEmptyString = and(not(isEmpty), isString);


const getArticleUrl = compose(
    option(default),
    map(createUrlFromName),
    alt(Maybe.of(page not found)),
    chain(safe(isNonEmptyString)),
    prop(name)
);
/*
const getArticleName = obj => prop(‘name‘, obj)
    .chain(safe(isNonEmptyString)) // If return Nothing then use alt value
    .alt(Maybe.of(‘page not found‘));

const getArticleUrl = obj => getArticleName(obj)
    .map(createUrlFromName)
    .option(‘default‘);*/

const url = getArticleUrl(article);

console.log(url)

 

以上是关于[Javascript Crocks] Compose Functions for Reusability with the Maybe Type的主要内容,如果未能解决你的问题,请参考以下文章

[Javascript Crocks] Recover from a Nothing with the `coalesce` Method

[Javascript Crocks] Compose Functions for Reusability with the Maybe Type

[Javascript Crocks] Create a Maybe with a `safe` Utility Function

Vaadin 12将对象传递给JavaScript的函数:不能编码类

Javascript列表包含一个字符串[重复]

如何使用位在javascript项目中创建打字稿组件?