ToolzPython 函数式编程工具箱

联合创作 · 2023-09-28 13:21

Toolz 是 Python 的函数式编程工具箱。Toolz 支持 Python 2.6+ 和 Python 3.3+。

Toolz 的实现包括三部分:

  • itertoolz,for operations on iterables. 例如:groupby,unique, interpose,

  • functoolz, for higher-order functions. 例如:memoize,curry, compose

  • dicttoolz, for operations on dictionaries. 例如:assoc,update-in, merge.

示例:

>>> def stem(word):
...     """ Stem word to primitive form """
...     return word.lower().rstrip(",.!:;'-\"").lstrip("'\"")
>>> from toolz import compose, frequencies, partial
>>> wordcount = compose(frequencies, partial(map, stem), str.split)
>>> sentence = "This cat jumped over this other cat!"
>>> wordcount(sentence)
{'this': 2, 'cat': 2, 'jumped': 1, 'over': 1, 'other': 1}
浏览 7
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报