简体   繁体   English

如何使用Toolz制作总是返回列表的咖喱版地图

[英]How to make a curry version of map that always returns list using toolz

If I import toolz using 如果我使用导入工具

from toolz.curried import *

then map will automatically becomes curried form, so 然后map将自动变为咖喱形式,因此

map(func,[[1,2],[3,4]])

can be written as 可以写成

map(func)([[1,2],[3,4]])

but curried map always return an iterable. 但是咖喱map总是返回可迭代的。 I way to define an curried lmap which always return list. 我定义了一个总是返回列表的咖喱lmap But simple try 但是简单的尝试

lmap=compose(list,map)

will not work, for example 例如,将不起作用

lmap(len)([[1,2],[3,4]])

will give 会给

--------------------------------------------------------------------------- -------------------------------------------------- -------------------------
TypeError Traceback (most recent call last) in () ----> 1 lmap(len)([[1,2],[3,4]]) 在()----> 1 lmap(len)([[1,2],[3,4]])中的TypeError Traceback(最近一次调用最后一次)

C:\\ProgramData\\Anaconda3\\lib\\site-packages\\toolz\\functoolz.py in C:\\ ProgramData \\ Anaconda3 \\ lib \\ site-packages \\ toolz \\ functoolz.py在
call (self, *args, **kwargs) 致电 (自我,* args,** kwargs)
466 ret = self.first(*args, **kwargs) 466 ret = self.first(* args,** kwargs)
467 for f in self.funcs: self.funcs中的f为467:
--> 468 ret = f(ret) -> 468 ret = f(ret)
469 return ret 469返回ret
470 470

TypeError: 'curry' object is not iterable TypeError:“ curry”对象不可迭代

So how to define a curried lmap ? 那么如何定义一个咖喱lmap

You're calling it wrong way. 您称其为错误方式。 map being passed to compose is curried, but not a whole expression. 传递给comp的map被管理,但不是整个表达式。 When you call it like 当你这样称呼它时

lmap(len)([[1,2],[3,4]])

it passes len to lmap it returns toolz.functoolz.curry equivalent to 它将len传递给lmap ,返回等于的toolz.functoolz.curry

map(len)

and then tries to call list on it: 然后尝试在其上调用list

list(map(len))

which obviously cannot work. 这显然是行不通的。 If it didn't fail complete expression would be equivalent to: 如果没有失败,则完整表达式将等效于:

list(map(len))([[1,2],[3,4]])

while the call you're looking for is: 而您正在寻找的电话是:

list(map(len), [[1,2],[3,4]])

So in fact currying doesn't make much sense here. 因此,实际上,在这里进行咖喱没有多大意义。

You probably want something around these lines: 您可能需要围绕以下内容:

def lmap(g): return compose(list, map(g)) 

which would be callable as you want: 您可以根据需要调用它:

>>> lmap(len)([[1,2],[3,4]])
[2, 2]

but to honest the problem looks a bit artificial - the biggest advantage of toolz in uniform laziness. 但说实话,这个问题看起来有点人为toolz在懒惰方面的最大优势。 Converting to list throws away most of that. 转换为list将丢弃大部分内容。

This question is currently top in google results for the toolz error 'curry' object is not iterable . 这个问题目前在Google搜索结果中排在首位,因为toolz错误'curry' object is not iterable One additional way to trigger this error message not yet mentioned here is to attempt to use toolz.curried.pipe with curried iterators as if pipe itself were curried: 触发此错误消息的另一种方法是尝试使用toolz.curried.pipe迭代器使用toolz.curried.pipe ,就像pipe本身是toolz.curried.pipe一样:

>>> transform = pipe(
    map(lambda x: x + 1),
    map(lambda x: x + x)
    )
>>> list(transform([3,4,5]))
TypeError: 'curry' object is not iterable

Here the first curried map is being passed as the final argument to the second; 这里,第一个咖喱图作为最终参数传递给第二个; despite being reexported from the curried namespace, pipe is not curried. 尽管管道是从curried命名空间中重新导出的,但管道却不是curried的。 The solution is to upgrade to toolz version 0.10.0 if not already using it and use toolz.curried.compose_left instead which is effectively a curried pipe : 解决方案是,如果尚未使用Toolz版本0.10.0,请升级到该版本,并使用toolz.curried.compose_left代替,它实际上是一个管理pipe

>>> transform = compose_left(
    map(lambda x: x + 1),
    map(lambda x: x + x)
    )
>>> list(transform([3,4,5]))
[8, 10, 12]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM