条件计算 [英] conditional computation

查看:93
本文介绍了条件计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想经常使用计算缓存方案,比如

o = CACHECOMPUTE complex-key-expr expensive-calc-expr

而不用编写复杂的密钥-expr或者昂贵的calc-expr两次。

所以它的丑陋:


_ = complex-key-expr; o = cache.get(_)或cache.setdefault(_,expensive-calc-expr)


有什么想法吗?


-robert

I want to use a computation cache scheme like
o = CACHECOMPUTE complex-key-expr expensive-calc-expr
frequently and elegantly without writing complex-key-expr or expensive-calc-expr twice.
So its ugly:

_=complex-key-expr; o=cache.get(_) or cache.setdefault(_,expensive-calc-expr)

Any ideas?

-robert

推荐答案

在星期四26/10/2006 16:30,罗伯特写道:
At Thursday 26/10/2006 16:30, robert wrote:

>我想使用计算缓存方案,例如


o = CACHECOMPUTE complex-key-expr expensive-calc-expr

经常和优雅地没有写复杂的key-expr或
昂贵的calc-expr两次。
所以它的丑陋:


_ = complex-key-expr; o = cache.get(_)或

cache.setdefault(_,expensive-calc-expr)

任何想法?
>I want to use a computation cache scheme like
o = CACHECOMPUTE complex-key-expr expensive-calc-expr
frequently and elegantly without writing complex-key-expr or
expensive-calc-expr twice.
So its ugly:

_=complex-key-expr; o=cache.get(_) or
cache.setdefault(_,expensive-calc-expr)

Any ideas?



memoize模式可以提供帮助;在
http://wiki.python.org/moin/PythonDecoratorLibrary你可以看到使用装饰器的

实现。

-

Gabriel Genellina

Softlab SRL


__________________________________________________

Correo Yahoo!

Espacio para todos tus mensajes,antivirus y antispam?gratis!

?Abrítucuenta ya! - http://correo.yahoo.com.ar

The memoize pattern can help; in
http://wiki.python.org/moin/PythonDecoratorLibrary you can see an
implementation using decorators.
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ?gratis!
?Abrí tu cuenta ya! - http://correo.yahoo.com.ar




robert写道:

robert wrote:

我想使用像
$这样的计算缓存方案b
$ bo = CACHECOMPUTE complex-key-expr expensive-calc-expr


经常且优雅地没有编写complex-key-expr或expensive-calc-expr两次。 br />
所以它的丑陋:


_ = complex-key-expr; o = cache.get(_)或cache.setdefault(_,expensive-calc-expr)


有什么想法吗?


-robert
I want to use a computation cache scheme like
o = CACHECOMPUTE complex-key-expr expensive-calc-expr
frequently and elegantly without writing complex-key-expr or expensive-calc-expr twice.
So its ugly:

_=complex-key-expr; o=cache.get(_) or cache.setdefault(_,expensive-calc-expr)

Any ideas?

-robert



你的问题有点简洁,所以我的答案可能不适合它,

但听起来你想要的是什么通常称为memoization,

,其中一个函数缓存其昂贵的计算返回值,

其中缓存由函数参数键入。


请参阅:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/52201

以及Cookbook中的各种其他实施细节。

Your question is a bit terse, so my answer might not be spot on for it,
but it sounds like you want what is typically called ''memoization'',
whereby a function caches its expensive-to-calculate return values,
where the cache is keyed by the function arguments.

See:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/52201
and various other implimentations in the Cookbook for details.


Gabriel Genellina写道:
Gabriel Genellina wrote:

在星期四26/10/2006 16:30,罗伯特写道:
At Thursday 26/10/2006 16:30, robert wrote:

>我想使用计算缓存方案,例如

o = CACHECOMPU TE complex-key-expr expensive-calc-expr

频繁而优雅,无需编写复杂的key-expr或
昂贵的calc-expr两次。
所以它的丑陋:

_ = complex-key-expr; o = cache.get(_)或
cache.setdefault(_,expensive-calc-expr)

任何想法?
>I want to use a computation cache scheme like
o = CACHECOMPUTE complex-key-expr expensive-calc-expr
frequently and elegantly without writing complex-key-expr or
expensive-calc-expr twice.
So its ugly:

_=complex-key-expr; o=cache.get(_) or
cache.setdefault(_,expensive-calc-expr)

Any ideas?



memoize模式可以提供帮助;在
http://wiki.python.org/moin/PythonDecoratorLibrary你可以看到使用装饰器的

实现。


The memoize pattern can help; in
http://wiki.python.org/moin/PythonDecoratorLibrary you can see an
implementation using decorators.



谢谢,因此它不会帮助挂钩单行表达式,但它有帮助我记得lambda! :耻辱::

类MemoCache(字典):#cache在会话期间昂贵的对象(仅限内存)

def memo(self,k,f):

尝试:返回自己[k]

除了IndexError:

返回self.setdefault(k,f())

cache = MemoCache()

....

o = cache.memo(complex-key-expr,lambda:expensive-calc-expr)


现在是pythonic。


-robert


thanks, as such it will not help hooking single line expressions, but it helped me remember the lambda ! :shame: :
class MemoCache(dict): # cache expensive Objects during a session (memory only)
def memo(self, k, f):
try: return self[k]
except IndexError:
return self.setdefault(k, f())
cache=MemoCache()
....

o = cache.memo( complex-key-expr, lambda: expensive-calc-expr )

Thats pythonic now.

-robert


这篇关于条件计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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