V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
thautwarm
V2EX  ›  Python

安利框架,原生 Python 的 functional 库,无伤尾递归加模式匹配。

  •  
  •   thautwarm ·
    thautwarm · 2018-01-08 03:57:20 +08:00 · 2156 次点击
    这是一个创建于 2271 天前的主题,其中的信息可能已经有所发展或是发生改变。

    模式匹配用到的类型系统类似 ts。

    pattern-matching

    • demo1
    from pattern_matching.core.match import when, overwrite
    from pattern_matching import var, Using
    from numpy.random import randint
    
    with Using(scope=locals(), use_tco=True):
        @overwrite((var, *var))
        def qsort(head, tail):
            lowers = [i for i in tail if i < head]
            highers = [i for i in tail if i >= head]
            return qsort(lowers) + [head] + qsort(highers)
    
    @when(var)
    def qsort(lst):
        return lst
    print(qsort(randint(0, 2000, size=(1200, ))))
    
    • demo2
    from pattern_matching import Match, when, var, T, t, match_err, _, overwrite
    
    @overwrite(_ == 1, var[int])
    def u_func(res):
        return res
    
    @when(var < 0, _)
    def u_func():
      raise varueError('input should be positive.')
    
    @when(var[int] > 1, var) 
    def u_func(now, res):
      return u_func(now-1, res*now)
    
    @when(var[int])
    def u_func(now):
      return u_func(now, 1)
    
    u_func(10, 1)  # => 3628800
    

    大哥哥们有什么看法吗?

    7 条回复    2018-01-08 16:04:34 +08:00
    guyskk0x0
        1
    guyskk0x0  
       2018-01-08 09:32:38 +08:00 via Android
    尾递归自动展开吗?和手写展开相比性能怎么样?
    WoodenRobot
        2
    WoodenRobot  
       2018-01-08 10:05:33 +08:00
    资瓷一下~
    thautwarm
        3
    thautwarm  
    OP
       2018-01-08 11:33:41 +08:00
    @guyskk0x0 还很慢,只能和普通递归比。主要动态模式匹配太耗时间。我这边 tco 是和 pattern matching 绑定的,现阶段只能说让函数栈深度最大不超过 3,但是优化还不完全。。
    xpresslink
        4
    xpresslink  
       2018-01-08 11:40:28 +08:00
    小玩 fn 可以移情
    https://github.com/kachayev/fn.py

    疯玩 fn 可能失神
    http://docs.hylang.org/en/stable/
    thautwarm
        5
    thautwarm  
    OP
       2018-01-08 12:58:50 +08:00
    @xpresslink fn.py 太丑陋了,实现也太裸。我以前和那个作者在 mailing list 有点不愉快。有些东西(特指 fp)没有语法糖支持,根本就没那个感觉。
    xpresslink
        6
    xpresslink  
       2018-01-08 15:57:32 +08:00
    @thautwarm
    要搞 fp 还是用正统一点用 lisp 吧,学术一点 haskell, 前位一点 clojure, 混搭 scala
    thautwarm
        7
    thautwarm  
    OP
       2018-01-08 16:04:34 +08:00
    @xpresslink 不一定呀,也有很多人只是想日常写代码能够写得舒服。。

    说到 lisp... P.S: 我一直觉得 lisp 只是宏比较强能容易实现 fp 的组件罢了,haskell-like 才是真 fp...
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1013 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:40 · PVG 03:40 · LAX 12:40 · JFK 15:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.