中途停止 Reduce() 操作.执行部分运行总和的功能方法 [英] Stopping a Reduce() operation mid way. Functional way of doing partial running sum

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

问题描述

我一直在做一些函数式编程并有一个问题.也许我可能遗漏了一些东西,但是有什么办法可以在中途停止reduce()"函数?让我们说当我达到某个条件时?这个想法在某种程度上似乎是反功能的.我在 python 或 F# 中没有看到任何这样的选项,

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#,

例如,假设我有一个列表,例如 [1,2,3,4,5].我想对这个列表中的元素求和,直到总和不大于某个数字(假设为 8),然后以某种方式返回/标记/存储/识别我实际添加的元素数.

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.

如果我们以 python 为例,我可能会尝试类似的东西

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)

这给了我正确的答案 6,但是我怎么发现我已经添加了 3 个元素来到达这里.没有这样的计数器.我无法在 lambdas 中进行赋值.我认为 F# 也有同样的情况.

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.

我知道我可以使用 for 循环或使用可以存储状态等的函数.但是做/考虑这个的功能方式是什么.Reduce() 想一直运行到最后,但是沿着这条处理线的某个地方,我们要么想停止它(因为我们不关心处理其余元素)要么至少记下我们在哪里不再关心.

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 经常与 map 结合使用.例如,谷歌开发了一个 map-reduce 框架来查询他们的数据库,这个 map-reduce 模式现在被用于其他几个项目(例如 CouchDB、Hadoop 等).

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).

首先,您需要将 input 变量 [2, 1, 3, 4, 5] 映射到类似:

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)]

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

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))

这将返回 (3, 6),这意味着部分和是 6 使用 3 元素.

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

我希望你了解 map-reduce-algorithms 背后的想法.

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

问候,
克里斯托夫

Regards,
Christoph

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆