美文网首页
toolz, python函数式库事实标准

toolz, python函数式库事实标准

作者: 深圳都这么冷 | 来源:发表于2022-05-11 09:42 被阅读0次

    toolz 是什么

    操作迭代器,函数和字典的函数集合
    简而言之,toolz是python事实上的函数式标准库

    主要特性:

    • 1.可组合性
    • 2.纯函数式
    • 3.延迟计算
    • 4.流程控制,抽象控制复杂度
    • 5.偏函数柯里化
    • 6.流解析
    • 7.并行计算友好

    安装

    pip install toolz

    API组成

    itertoolz操作可迭代对象,如: groupby, unique, interpose
    functoolz操作高阶函数,如: memoize, curry, compose
    dicttoolz操作字典结构,如: assoc, update-in, merge

    例子

    >>> def stem(word):
    ...     """ Stem word to primitive form """
    ...     return word.lower().rstrip(",.!:;'-\"").lstrip("'\"")
    
    >>> from toolz import compose, frequencies
    >>> from toolz.curried import map
    >>> wordcount = compose(frequencies, map(stem), str.split)
    
    >>> sentence = "This cat jumped over this other cat!"
    >>> wordcount(sentence)
    {'this': 2, 'cat': 2, 'jumped': 1, 'over': 1, 'other': 1}
    

    总结

    toolz是一个非常强大的函数库,可以帮助你写出短小易读,优雅强大的代码。
    更多功能参见官方文档PyToolz API Documentation

    相关文章

      网友评论

          本文标题:toolz, python函数式库事实标准

          本文链接:https://www.haomeiwen.com/subject/jqglurtx.html