V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
abersheeran
V2EX  ›  Python

更好用的 Python 管道!

  •  
  •   abersheeran · Apr 19, 2021 · 2473 views
    This topic created in 1847 days ago, the information mentioned may be changed or developed.

    之前的主题 https://v2ex.com/t/743574

    吸取了大家的建议,做了一个更好用的管道出来。根据测试,性能损耗在纳秒级,我个人觉得应该可以忽略。

    还是老样子,不多说,直接看代码。仓库在 https://github.com/abersheeran/cool

    from functools import reduce
    from cool import F, FF
    
    range(10) | F(filter, lambda x: x % 2) | F(sum) == 25
    
    (1, 2) | FF(lambda x, y: x + y) == 3
    
    range(10) | F(reduce, lambda x, y: x + y) == 45
    range(10) | F(reduce, lambda x, y: x + y, ..., 10) == 55
    
    square = F(pow, ..., 2)
    range(10) | F(map, square) | F(sum) == 285
    
    Supplement 1  ·  Apr 19, 2021

    http://coconut-lang.org/ 写一个可能更清晰一些的对比样例。

    range(10) |> map$(pow$(?, 2)) |> list
    

    上面那个代码等价于下面这个

    range(10) | F(map, F(pow, ..., 2)) | F(list)
    

    另外吐槽一下 coconut 的编译结果,惨不忍睹。我都想写个 Python 超集出来编译一波了(在学了、在学了)。

    5 replies    2021-04-20 18:20:57 +08:00
    SjwNo1
        1
    SjwNo1  
       Apr 19, 2021
    cool
    ... 没太懂,表示占位吗
    abersheeran
        2
    abersheeran  
    OP
       Apr 19, 2021   ❤️ 1
    @SjwNo1 是的。在仓库 README 里有解释的。
    hanyceZ
        3
    hanyceZ  
       Apr 20, 2021
    给大佬点个赞
    listenerri
        4
    listenerri  
       Apr 20, 2021
    不明觉厉,点个赞
    abersheeran
        5
    abersheeran  
    OP
       Apr 20, 2021
    @listenerri 用就完事!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5721 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 07:02 · PVG 15:02 · LAX 00:02 · JFK 03:02
    ♥ Do have faith in what you're doing.