热函数发生器
fnn的Python项目详细描述
带有一些帮助函数的库,可以为您生成更多函数。
查看
用拨片做拨片机:
from fnny import pluck as p from collections import namedtuple G = namedtuple("G", ["a"]) guys = [G(i) for i in range(10)] expected = sum(map(lambda x: x.a, guys)) actual = sum(map(p.a, guys)) assert expected == actual
使用方法调用方法:
from fnny import method as m from test.test_it import _Argless() no_args = [_Argless() for a in range(10)] expected = list(map(lambda x: x.wow(), no_args)) actual = list(map(m.wow, no_args)) assert expected == actual
使用部分应用的方法,部分:
from fnny import partial as pa from test.test_it import _OneArg() has_args = [_OneArg() for a in range(10)] expected = list(map(lambda x: x.add(1), has_args)) actual = list(map(pa.add(1), has_args)) assert expected == actual
部分偶数支持千瓦数:
from test.test_it import _OneKwarg() has_args = [_OneKwarg() for a in range(10)] expected = list(map(lambda x: x.derp(something="heyy"), has_args)) actual = list(map(pa.derp(something="heyy"), has_args)) assert expected == actual
但实际上,python已经有了一种更习惯的方法来完成以上所有工作:
guys = [G(i) for i in range(10)] assert sum(map(lambda x: x.a, guys)) == sum(g.a for g in guys) no_args = [_Argless() for a in range(10)] expected = list(map(lambda x: x.wow(), no_args)) actual = [argless.wow() for argless in no_args] assert expected == actual has_args = [_OneArg() for a in range(10)] expected = list(map(lambda x: x.add(1), has_args)) actual = [one.add(1) for one in has_args] assert expected == actual has_kwargs = [_OneKwarg() for a in range(10)] expected = list(map(lambda x: x.derp(something="heyy"), has_kwargs)) actual = [kw.derp(something="heyy") for kw in has_kwargs] assert expected == actual
但也许还有一个地方可以用来生成基于描述器的函数库。
我想一想到这些我就会加上例子。