为什么这个函数的名字是reduce?
程序员文章站
2022-03-25 15:37:14
...
我的英语不好,我理解的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,
bring down,
bring forth,
bring forward,
bring in,
bring off, to accomplish, carry out, or achieve (something):
He brought off his speech with ease.
16.
bring on,
bring out,
bring to,
bring up,
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,
- to convince of a belief or opinion; persuade:
I think we can bring him around to agreeing with the plan.
- to restore to consciousness, as after a faint.
- to bring as a visitor:
They brought around a new employee this morning.
bring down,
- to injure, capture, or kill:
He brought down several ducks on his last hunting trip.
- to lessen; reduce:
I won't buy that lamp unless they bring down the price.
- Slang. to cause to be in low spirits; depress:
The bad news brought him down.
bring forth,
- to give birth to; deliver; bear:
to bring forth a son.
- to give rise to; introduce:
to bring forth a proposal for reducing costs.
bring forward,
- to bring to view; show.
- to present for consideration; adduce:
to bring forward an opinion.
bring in,
- to yield, as profits or income:
My part-time job doesn't bring in much, but I enjoy it.
- to present officially; submit:
The jury brought in its verdict.
- to cause to operate or yield:
They brought in a gusher on his property.
- to present for consideration, approval, etc.; introduce:
She brought in six new members last month.
bring off, to accomplish, carry out, or achieve (something):
He brought off his speech with ease.
16.
bring on,
- to cause to happen or exist; bring about:
This incident will surely bring on a crisis.
- to introduce; cause to appear:
Bring on the clowns.
bring out,
- to expose; reveal.
- to make noticeable or conspicuous in a contrast.
- to publish, as a book or play.
- to introduce officially into society:
to bring out a debutante.
bring to,
- to bring back to consciousness; revive.
- Nautical. to head (a vessel) close to or into the wind so as to halt.
bring up,
- to care for during childhood; rear.
- to introduce or mention for attention, discussion, action, orconsideration.
- to vomit.
- to stop or cause to stop quickly:
to bring up a car at the curb.
- Nautical. (of a vessel) to cause to halt, as by lowering an anchor orrunning aground; fetch up.
reduce 很多项成一项啊。这个很难理解么?
上一篇: python使用多线程不断刷新网页的方法
下一篇: 详解php如何对百万级数据进行快速排重
推荐阅读