Maybe 也能使用 Function Composition

栏目: 后端 · 前端 · 发布时间: 7年前

内容简介:之前都使用VS Code 1.34.0Quokka 1.0.216

之前都使用 Maybe 自帶的 Method,以 OOP 的 Method Chaining 處理 Maybe ,事實上也能完全以 FP 的 Function Composition 處理。

Version

VS Code 1.34.0

Quokka 1.0.216

Ramda 0.26.1

Crocks 0.11.1

OOP

import { isString, and, not, isEmpty, prop, safe, Maybe } from 'crocks';
import { compose, join, split, toLower, concat } from 'ramda';

let { of } = Maybe;

// isNonEmptyString :: a -> Boolean
let isNonEmptyString = and(not(isEmpty), isString);

// createUrlSlug :: String -> String
let createSlug = compose(join('-'), split(' '), toLower);

// appendSlug :: String -> String
let appendSlug = concat('https://oomusou.io/crocks/');

// createUrl :: String -> String
let createUrl = compose(appendSlug, createSlug);

let data = {
  title: '',
  price: 100,
};

// fn :: Object -> Maybe String
let fn = obj => prop('title', obj)
  .chain(safe(isNonEmptyString))
  .alt(of('page-not-found'))
  .map(createUrl);

fn(data).option('https://oomusou.io'); // ?

23 行

// fn :: Object -> Maybe String
let fn = obj => prop('title', obj)
  .chain(safe(isNonEmptyString))
  .alt(of('page-not-found'))
  .map(createUrl);

在 使用 alt() 提早處理 Nothing 一文中,由於 prop() 回傳 Maybe ,我們使用了一系列的 chain() 、 alt() 、 map() 處理 Maybe ,這些都是 Maybe 自帶 method,呈現出 OOP 風格的 method chaining,我們能否使用 FP 的 function composition 處理 Maybe 呢 ?

Maybe 也能使用 Function Composition

FP

import { isString, and, not, isEmpty, prop, safe, Maybe, chain, alt, map, option } from 'crocks';
import { compose, join, split, toLower, concat, pipe } from 'ramda';

let { of } = Maybe;

// isNonEmptyString :: a -> Boolean
let isNonEmptyString = and(not(isEmpty), isString);

// createUrlSlug :: String -> String
let createSlug = compose(join('-'), split(' '), toLower);

// appendSlug :: String -> String
let appendSlug = concat('https://oomusou.io/crocks/');

// createUrl :: String -> String
let createUrl = compose(appendSlug, createSlug);

let data = {
  title: '',
  price: 100,
};

// fn :: Object -> Maybe String
let fn = pipe(
  prop('title'),
  chain(safe(isNonEmptyString)),
  alt(of('page-not-found')),
  map(createUrl)
);

option('https://oomusou.io')(fn(data)); // ?

第 1 行

import { isString, and, not, isEmpty, prop, safe, Maybe, chain, alt, map, option } from 'crocks';

將原本的 chain() 、 alt() 、 map() 與 option() 改以 function 從 Crocks import 進來。

第 2 行

import { compose, join, split, toLower, concat, pipe } from 'ramda';

從 Ramda import 進 pipe() 。

也可以使用 Crocks 的 compose() 與 pipe()

23 行

// fn :: Object -> Maybe String
let fn = pipe(
  prop('title'),
  chain(safe(isNonEmptyString)),
  alt(of('page-not-found')),
  map(createUrl)
);

pipe() 將 obj parameter 給 point-free 了,且完全不使用 method chaining。

31 行

option('https://oomusou.io')(fn(data)); // ?

option() 也可改用 function 方式。

Maybe 也能使用 Function Composition

Function Composition

import { isString, and, not, isEmpty, prop, safe, Maybe, chain, alt, map, option } from 'crocks';
import { compose, join, split, toLower, concat, pipe } from 'ramda';

let { of } = Maybe;

// isNonEmptyString :: a -> Boolean
let isNonEmptyString = and(not(isEmpty), isString);

// createUrlSlug :: String -> String
let createSlug = compose(join('-'), split(' '), toLower);

// appendSlug :: String -> String
let appendSlug = concat('https://oomusou.io/crocks/');

// getSlugIfEmptyString :: Maybe String -> Maybe String
let getSlugIfEmptyString = pipe(
  chain(safe(isNonEmptyString)),
  alt(of('page-not-found')),
);

// createUrl :: String -> String
let createUrl = compose(appendSlug, createSlug);

let data = {
  title: '',
  price: 100,
};

// fn :: Object -> Maybe String
let fn = pipe(
  prop('title'),
  getSlugIfEmptyString,
  map(createUrl)
);

option('https://oomusou.io')(fn(data)); // ?

29 行

// fn :: Object -> Maybe String
let fn = pipe(
  prop('title'),
  getSlugIfEmptyString,
  map(createUrl)
);

Q:從 method chaining 改成 function composition 只是語法差異,還有什麼實際用途嗎 ?

chain() 與 alt() 是為了處理 empty string 特別邏輯,可將這部分單獨抽出成 getSlugIfEmptyString() ,其語義更為清楚。

15 行

// getSlugIfEmptyString :: Maybe String -> Maybe String
let getSlugIfEmptyString = pipe(
  chain(safe(isNonEmptyString)),
  alt(of('page-not-found')),
);

反之若使用 method chaining,則 chain() 與 alt() 很難單獨抽出來,因為都綁在 Maybe 上了,這就是 FP 將 data 與 function 分離的優勢。。

Maybe 也能使用 Function Composition

Conclusion

Reference

Andy Van Slaars , Compose Function for Reusability with the Maybe Type

Crocks , chain()

Crocks , alt()

Crocks , coalesce()

Crocks , option()


以上所述就是小编给大家介绍的《Maybe 也能使用 Function Composition》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

JAVA多线程设计模式

JAVA多线程设计模式

结城 浩、博硕文化 / 博硕文化 / 中国铁道出版社 / 2005-4-1 / 49.00元

《JAVA多线程设计模式》中包含JAVA线程的介绍导读,12个重要的线程设计模式和全书总结以及丰富的附录内容。每一章相关线程设计模式的介绍,都举一反三使读者学习更有效率。最后附上练习问题,让读者可以温故而知新,能快速地吸收书中的精华,书中最后附上练习问题解答,方便读者学习验证。一起来看看 《JAVA多线程设计模式》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具