本文介绍了在中途停止Reduce()操作。部分运行总和的功能方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做一些函数式编程,并有一个问题。也许我可能会错过一些东西,但有什么方法可以在中途停止reduce()函数吗?让我说当我达到一定的条件?这个想法似乎反功能。我还没有在python或F#中看到任何这样的选项,例如,假设我有一个列表,例如[1,2,3,4,5 ]。我想总结这个列表中的元素,直到总和不大于某个数字(让我们说8),然后返回/标记/存储/标识不知何故,我实际添加的元素数量。

如果我们看一下python,例如我可能会尝试类似于

  reduce(lambda a, b:a如果a + b> 8否则a + b,输入)

这给了我正确的答案6,但我怎么发现我已经添加了3个元素来到这里。没有这样的柜台。我不能在lambdas里面做任务。我知道我可以使用for循环或者使用一个可以存储状态的函数等。但是,什么是功能方式呢? /想想这个。 Reduce()想运行直到结束,但是在这个处理线的某处,我们要么停止它(因为我们不关心处理其余的元素),或者至少记下我们的地方停止关心。

解决方案

Reduce经常与地图结合使用。例如Google开发了一个map-reduce框架来查询他们的数据库,而这个map-reduce模式现在已经用于其他几个项目(例如CouchDB,Hadoop等)。

首先,您需要将输入变量 [2,1,3,4,5] 映射到类似的东西:(1,2),(1,1),(1,3),(1,4),(b), (1,5)]

在这种情况下, x [0] 将表示获得总和 x [1] 的元素数量。当然,每个元素的开始元素的数量是 1

接下来的事情是,

  reduce(
lambda a,b:a if a [1] + b [1]> 8 else(a [0] + b [0],a [1] + b [1]),
map(lambda x:(1,x),input))

这将返回(3,6),表示部分金额为 6 使用 3 元素。



I希望你有了map-reduce-algorithms背后的想法。



问候,

Christoph


I have been doing some functional programming and had a question. Perhaps I might be missing something but is there any way to stop a "reduce()" function midway? Lets say when I reach a certain condition? The idea somehow seems anti functional. I haven't seen any such option in python or F#,

As an example, lets say I have a list such as [1,2,3,4,5]. I want to sum the elements in this list until the sum is not greater than some number (lets say 8), and return/mark/store/identify somehow, the number of elements I have actually added.

If we looked at python for example for I might try something like

reduce(lambda a,b : a if a + b > 8 else a + b, input)

This gives me the right answer 6, but how do I find that I had added 3 elements to get here. There is no counter as such. I can't do assignments inside lambdas. I think F# has the same situation.

I know I can use a for loop or use a function that can store state etc. But what would be the functional way of doing/thinking about this. Reduce() wants to run until the end, but somewhere along this line of processing, we either want to stop it (because we don't care about processing the rest of the elements) or at least make a note of the place where we stopped caring.

解决方案

Reduce is often used in combination with map. Google for example has developed a map-reduce framework for querying their databases and this map-reduce pattern is now used in several other projects (e.g. CouchDB, Hadoop, etc).

First, you need to map the input variables [2, 1, 3, 4, 5] to something like:

[(1, 2), (1, 1), (1, 3), (1, 4), (1, 5)]

In that case, x[0] will represent the number of the elements to get the sum x[1]. Of course, the number of elements is 1 at the beginning for each single element.

The next thing then, is to operate on those tuples:

reduce(
    lambda a, b: a if a[1] + b[1] > 8 else (a[0] + b[0], a[1] + b[1]),
    map(lambda x: (1, x), input))

This will return (3, 6), meaning the partial sum is 6 using 3 elements.

I hope you got the idea behind map-reduce-algorithms.

Regards,
Christoph

这篇关于在中途停止Reduce()操作。部分运行总和的功能方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 07:50