js 无点链接_无点组合如何使您成为更好的函数式程序员

js 无点链接

Discover Functional JavaScript was named one of the best new Functional Programming books by BookAuthority!

“发现功能JavaScript”BookAuthority评为最佳新功能编程书籍之一

"Point-free style — aims to reduce some of the visual clutter by removing unnecessary parameter-argument mapping." - Kyle Simpson in Functional-Light JavaScript

“无点样式-旨在通过消除不必要的参数-参数映射来减少一些视觉混乱。” -Functional -Light JavaScript中的 Kyle Simpson

Consider the flowing code:

考虑下面的代码:

let newBooks = books.filter(point => isTechnology(point))

Now look at the same code after eliminating points (parameters/arguments):

现在,在消除点(参数/参数)之后看一下相同的代码:

let newBooks = books.filter(isTechnology)

列表操作中的无积分 (Point-free in List Operations)

Let’s do list operations in a point-free style.

让我们以无点样式列出操作。

Say we need to find the technology titles in a list of books, prepare the book object with all information for the view, and sort the books by the author’s name.

假设我们需要在书籍列表中找到技术标题,为书籍对象准备视图的所有信息,然后按作者姓名对书籍进行排序。

Here is how the code would look:

这里是代码是什么样子:

function getBooks(){
  return books.filter(isTechnology)
              .map(toBookView)
              .sort(ascByAuthor);
}

//Small functions with points
function isTechnology(book){
   return book.type === "T";
}

function toBookView(book){
  return Object.freeze({
    title : book.title,
    author : authors[book.authorID].name
  });
}
  
function ascByAuthor(book1, book2){
  if(book1.author < book2.author) return -1;
  if(book1.author > book2.author) return 1;
  return 0;
}

The callbacks isTechnology(), toBookView(), ascByAuthor() are small functions with intention-revealing names. They are not built in a point-free style.

回调isTechnology()toBookView()ascByAuthor()是具有意图公开名称的小函数。 它们不是以无意义的样式构建的。

The code assembling all these functions in getBooks() is point-free.

getBooks()组装所有这些功能的代码是没有意义的。

分解和组成 (Decomposition and composition)

Our natural way of dealing with a problem is to break it into smaller pieces and then put everything back together.

我们处理问题的自然方法是将其分解成小块,然后将所有内容放回原处。

We break the bigger task up into several functions doing smaller tasks. Then we re-combine these smaller functions to solve the initial problem.

我们将较大的任务分解为几个执行较小任务的功能。 然后,我们重新组合这些较小的功能以解决最初的问题。

Let’s read the requirements again:

让我们再次阅读要求:

We need to find the technology titles in a list of books, prepare the book object with all information for the view, and sort the books by the author’s name.
我们需要在书籍列表中找到技术标题,准备包含视图所有信息的书籍对象,并按作者姓名对书籍进行排序。

We created:

我们创建了:

  • isTechnology() predicate to check if it’s a technology book

    isTechnology()谓词检查是否为技术书

  • toViewBook() to build an object with all the information for the view

    toViewBook()来构建一个包含视图所有信息的对象

  • ascByAuthorname() to sort two books ascending by the author’s name

    ascByAuthorname()对按作者姓名升序排列的两本书进行排序

  • getBooks() to combine all these small functions together in a point-free style

    getBooks()以无点样式将所有这些小功能组合在一起

function getBooks(){
  return books.filter(isTechnology)
              .map(toBookView)
              .sort(ascByAuthor);
}

迈向无点合成的步骤 (Steps towards point-free composition)

There is no additional anonymous callback when doing point-free composition. No function keyword, no arrow syntax =&gt; . All we see are function names.

执行无点合成时,没有其他匿名回调。 没有function关键字,没有箭头语法=& > 。 我们所看到的只是函数名称。

  • In most cases, extract out the callbacks in named functions.

    在大多数情况下,请提取命名函数中的回调。
  • In simple cases, just use an utility function from the toolbox to create the callback on the fly. Look at the prop() function, for example.

    在简单的情况下,只需使用工具箱中的实用程序功能即可即时创建回调。 例如,看一下 prop()函数。

  • Write the coordinator function in a point-free style.

    以无点样式编写协调器函数。
小功能 (Small functions)

The consequence of writing code this way is a lot of small functions with intention revealing names. Naming these small functions requires time, but if it’s done well, it will make the code easier to read.

以这种方式编写代码的结果是许多带有意图揭示名称的小功能。 给这些小的函数命名需要花费时间,但是如果做得好,它将使代码更易于阅读。

There will be two kinds of functions:

将有两种功能:

  • Functions doing one task: they are pure or closure functions. Usually they are not built in a point-free style, but instead have good names.

    执行一项任务的函数:它们是纯函数或闭包函数。 通常,它们不是以无意义的样式构建的,而是具有良好的名称。
  • Functions coordinating a lot of tasks: joining these small tasks in a point-free style makes it easier to read.

    协调许多任务的功能:以无点样式加入这些小任务,使其更易于阅读。
并非所有内容都没有意义 (Not everything is point-free)

I’m not aiming at having everything point-free. I’m aiming for point-free in specific places, especially when composing functions.

我的目标并不是让一切都变得毫无意义。 我的目标是在特定位置实现无点操作,尤其是在编写函数时。

Discover Functional JavaScript was named one of the best new Functional Programming books by BookAuthority!

发现功能JavaScript被称为 BookAuthority最好的新功能编程书籍

For more on applying functional programming techniques in React take a look at Functional React.

有关在React中应用函数式编程技术的更多信息,请查看 Functional React

Learn functional React, in a project-based way, with Functional Architecture with React and Redux.

通过带有React和Redux的功能架构 ,以基于项目的方式学习功能性React

Follow on Twitter

在Twitter上关注

翻译自: https://www.freecodecamp.org/news/how-point-free-composition-will-make-you-a-better-functional-programmer-33dcb910303a/

js 无点链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值