Suppose I have a function like this:
from toolz.curried import *
@curry
def foo(x, y):
print(x, y)
然后我可以调用:
foo(1,2)
foo(1)(2)
两者都按预期返回相同的结果。
然而,我想做类似于这样的事情:
@curry.inverse # hypothetical
def bar(*args, last):
print(*args, last)
bar(1,2,3)(last)
这个想法是我想预先配置一个函数,然后像这样将函数放入管道中:
pipe(data,
f1, # another function
bar(1,2,3) # unknown number of arguments
)
然后,
bar(1,2,3)(data)
将作为管道的一部分被调用。然而,我不知道该怎么做。有什么想法吗?非常感谢!编辑:需要更具说明性的示例,因此在这里它来了:import pandas as pd
from toolz.curried import *
df = pd.DataFrame(data)
def filter_columns(*args, df):
return df[[*args]]
pipe(df,
transformation_1,
transformation_2,
filter_columns("date", "temperature")
)
正如你所看到的,DataFrame通过函数进行管道处理,其中之一是
filter_columns
。然而,该函数是预配置的,并返回一个仅接受DataFrame的函数,类似于装饰器。可以使用以下方式实现相同的行为:def filter_columns(*args):
def f(df):
return df[[*args]]
return f
然而,我总是必须运行两个函数调用,例如:filter_columns()(df)
,我希望避免这种情况。
foo(1,2)
和foo(1)(2)
,您希望它打印出2 1
吗? - Copperfield