2013-05-17 23 views
0

我正在写一个Python脚本,应该采取函数列表,写成lambda表达式,并返回所有函数的撰写,但是,我在脚本中有一个箭头,也许因为我使用lambda表达式的方式。这似乎即使我给返回的函数一个数字值,我回来了一个函数,而不是一个数字。这是我写的:Python的lambda表达式组成迭代器脚本

def compose1(lst): 
    if lst == []: 
     return lambda x: x 
    else: 
     temp = (lst[len(lst)-1]) 
     for i in range(len(lst)-2,-1,-1): 
      temp = lambda x: lst[i](temp) 
     return lambda x: temp 

这是我写的函数,它说我有一个错误tast。

f = compose1([lambda x: x+1, lambda x: x*2, lambda x: x-1]) 
for x in range(10): 
    assert (f(x) == 1 + (x - 1) * 2) 
f = compose1([lambda x: x-1, lambda x: x*2, lambda x: x+1]) 
for x in range(10): 
    assert (f(x) == (x + 1) * 2) - 1 

我会apreciate对这个问题的一些HALP ..这里 谢谢:)

+0

你的第一个' assert'在表达式周围有一些虚假的括号。如果你删除了它们,那么你可能会注意到第二个'assert'中的圆括号在错误的地方。 – Duncan

回答

1

它看起来像你的循环只是重新实现reduce做什么。下面是你的功能组成问题的功能看看:

def compose1(fnlist): 
    if not fnlist: 
     return lambda x: x 

    # compose 1 function of x from two others 
    def compose2fns(fn1, fn2): 
     return lambda x : fn1(fn2(x)) 
    # or if you really love lambdas 
    # compose2fns = lambda fn1,fn2: lambda x: fn1(fn2(x)) 

    # use reduce to cumulatively apply compose2fns to the functions 
    # in the given list 
    return reduce(compose2fns, fnlist) 

这通过你的测试就好了。

CODE GOLF: 我忍不住了,这里是一个班轮,甚至包括你的检查空输入列表:

compose1 = lambda fnlist: reduce(lambda fn1,fn2: lambda x : fn1(fn2(x)), 
            fnlist or [lambda x:x]) 
1

你的问题是你的逻辑。

for i in range(len(lst)-2,-1,-1): 
    temp = lambda x: lst[i](temp) 
return lambda x: temp 

这将设置temp为函数。 lst是一个函数列表,lst[i]是一个函数。你称之为,给出一个值,然后用lambda创建一个新函数。然后你返回一个给出该函数的函数。

你的返回值是一个函数,它给出了一个函数,它给出一个值,因此是你的问题。

作为说明,此代码还有其他问题。例如,if lst == []:应该是if not lst:。您也不应该按照索引进行迭代,而应该按照Python的设计进行迭代。我实际上无法弄清楚你想用你的代码实现什么,这显示了通过索引来读取迭代是多么困难。

你的代码目前做到这一点:

  • 如果没有值,返回返回第一个参数的函数。
  • 如果有一个值,则返回列表中的函数。
  • 如果有许多值,则返回一个函数,该函数返回一个函数,该函数返回从列表中运行第一个函数而检索到的第一个值。

我不确定你在做什么,但我敢肯定,不是这样。

+0

坦克的帮助。欣赏它。肯定会使用你的服务! –

3
def compose(*funcs): 
    """ 
    compose(func[,...[, func]]) -> function 

    Return the composition of functions. 
    For example, compose(foo, bar)(5) == foo(bar(5)) 
    """ 
    if not all(callable(func) for func in funcs): 
     raise TypeError('argument must be callable') 
    funcs = funcs[::-1] 

    def composition(*args, **kwargs): 
     args = funcs[0](*args, **kwargs) 
     for func in funcs[1:]: 
      args = func(args) 
     return args 
    return composition 

f = compose(*[lambda x: x+1, lambda x: x*2, lambda x: x-1]) 
for x in range(10): 
    assert f(x) == (1 + (x - 1) * 2) 
f = compose(*[lambda x: x-1, lambda x: x*2, lambda x: x+1]) 
for x in range(10): 
    assert f(x) == ((x + 1) * 2) - 1 
+0

对,这就是目标所在。我真的无法解决这个问题,+1。 –

+0

坦克!肯定有很多要学习的脚本! –

0

我喜欢这种语法:

f = do (lambda x: x-1) (lambda x: x*2) (lambda x: x+1) 

for x in range(10): 
    assert f(x) == 1 + (x - 1) * 2 

实现是出奇的简单:

class do(object): 
    def __init__(self, x): 
     self.fns = [x] 
    def __call__(self, x): 
     if callable(x): 
      self.fns.append(x) 
      return self 
     for f in self.fns: 
      x = f(x) 
     return x 
+0

不错的一个!谢谢! –