项目作者: magniff

项目描述 :
Stack overflow freedom
高级语言: Python
项目地址: git://github.com/magniff/endless.git
创建时间: 2015-10-31T13:58:29Z
项目社区:https://github.com/magniff/endless

开源协议:

下载


ENDLESS

Build Status

WTF

This tiny library helps you to write recursive functions without any ‘’stack overflow’’ related pain.

Say you have a function like this:

  1. def factorial(value):
  2. return 1 if value == 1 else value * factorial(value-1)

It works fine until the value thing is less then a sys.getrecursionlimit().

ENDLESS provides a solution

  1. @endless.make
  2. def factorial(value):
  3. return 1 if value == 1 else value * (yield {'value': value-1})

and then you can use it as a normal function:

  1. >>> result = factorial(10000)

This decorated function is completely stack overflow free, because it doesn’t use process’s stack section at all.

Also stuff like this one is also possible (called maccarthy91 function):

  1. @endless.make
  2. def maccarthy(value):
  3. if value > 100:
  4. return value - 10
  5. else:
  6. return (yield {'value': (yield {'value': value+11})})

So the rule is that: whenever you want to use a recursive call, just yield the dictionary, representing the function kwargs, that is it.

INSTALLATION

  1. pip install endless