Rumah >pembangunan bahagian belakang >Tutorial Python >为什么这个函数的名字是reduce?

为什么这个函数的名字是reduce?

WBOY
WBOYasal
2016-06-06 16:23:422231semak imbas

我的英语不好,我理解的reduce是减少的意思。但这个函数实现的功能我感觉和减少就没有关系。所以请教该怎样理解reduce?为什么这个函数的名字是reduce?

回复内容:

reduce翻译成规约,意思其他答案里面已经讲了。
实际上函数名不应取reduce,应该叫fold,而且要区分foldl和foldr左右两个版本。并行计算里面的那个reduce有个隐含前提,是reduce over monoid,所以结果才与reduce方向无关,可以做有效的并行化。 归约
这是一个函数式编程里面,高阶函数里面很基础的概念。不过我最早听到这个词是在并行计算里面。
函数式编程里面有时也称作 fold ,也就是折叠,这个更好理解吧。详情可参见wikipedia,里面提到了 In functional programming, fold – also known variously as reduce, accumulate, aggregate, compress, or inject
你找一个你觉得好理解的词就OK了。 这就是MapReduce中的reduce,也叫fold。
wikipedia链接:Fold (higher-order function)

SICP中用scheme实现的叫accumulate。
C++中是std::accumulate。

wiki中Folds in various languages部分讲了各种语言中的这个函数的叫法。 reduce在函数式语言里就是要减少的意思,要合并结果,多项变成一项。至于怎么合并,就看reduce的函数是什么。
range得到一个collection(或sequence),里面有多项。reduce函数是add,结果是一项。
当然reduce后也可以还是一个collection,那是因为把collection整体看做一项结果。 别纠结了,这个函数如此难懂,完全不符合python的风格,以致于在python3中被删除了。

题主还是趁早改邪归正,抛弃python2,投入python3的阵营吧! reduce 在这里应该翻译成“归纳”。 verb (used with object), brought, bringing.
1.
to carry, convey, conduct, or cause (someone or something) to comewith, to, or toward the speaker:
Bring the suitcase to my house. He brought his brother to my office.
2.
to cause to come to or toward oneself; attract:
Her scream brought the police. He brought honor to his family by hisheroism.
3.
to cause to occur or exist:
The medication brought instant relief.
4.
to cause to come into a particular position, state, or effect:
to bring the car to a stop.
5.
to cause to appear or occur in the mind; evoke or recall:
The letter brought her memories of youth.
6.
to persuade, convince, compel, or induce:
She couldn't bring herself to sell the painting.
7.
to sell for; fetch:
These lamps will bring a good price.

Verb phrases
9.
bring about, to accomplish; cause:
Land reform brought about a great change in the lives of the commonpeople.
10.
bring around /round,
  1. to convince of a belief or opinion; persuade:
    I think we can bring him around to agreeing with the plan.
  2. to restore to consciousness, as after a faint.
  3. to bring as a visitor:
    They brought around a new employee this morning.
11.
bring down,
  1. to injure, capture, or kill:
    He brought down several ducks on his last hunting trip.
  2. to lessen; reduce:
    I won't buy that lamp unless they bring down the price.
  3. Slang. to cause to be in low spirits; depress:
    The bad news brought him down.
12.
bring forth,
  1. to give birth to; deliver; bear:
    to bring forth a son.
  2. to give rise to; introduce:
    to bring forth a proposal for reducing costs.
13.
bring forward,
  1. to bring to view; show.
  2. to present for consideration; adduce:
    to bring forward an opinion.
14.
bring in,
  1. to yield, as profits or income:
    My part-time job doesn't bring in much, but I enjoy it.
  2. to present officially; submit:
    The jury brought in its verdict.
  3. to cause to operate or yield:
    They brought in a gusher on his property.
  4. to present for consideration, approval, etc.; introduce:
    She brought in six new members last month.
15.
bring off, to accomplish, carry out, or achieve (something):
He brought off his speech with ease.
16.
bring on,
  1. to cause to happen or exist; bring about:
    This incident will surely bring on a crisis.
  2. to introduce; cause to appear:
    Bring on the clowns.
17.
bring out,
  1. to expose; reveal.
  2. to make noticeable or conspicuous in a contrast.
  3. to publish, as a book or play.
  4. to introduce officially into society:
    to bring out a debutante.
18.
bring to,
  1. to bring back to consciousness; revive.
  2. Nautical. to head (a vessel) close to or into the wind so as to halt.
19.
bring up,
  1. to care for during childhood; rear.
  2. to introduce or mention for attention, discussion, action, orconsideration.
  3. to vomit.
  4. to stop or cause to stop quickly:
    to bring up a car at the curb.
  5. Nautical. (of a vessel) to cause to halt, as by lowering an anchor orrunning aground; fetch up.
....
reduce 很多项成一项啊。这个很难理解么?
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn