How to render a tree in Twig

I played around with domi27’s idea and came up with this. I made a nested array as my tree, [‘link’][‘sublinks’] is null or another array of more of the same. Templates The sub-template file to recurse with: <!–includes/menu-links.html–> {% for link in links %} <li> <a href=”https://stackoverflow.com/questions/8326482/{{ link.href }}”>{{ link.name }}</a> {% if link.sublinks %} … Read more

Are functions in JavaScript tail-call optimized?

Update: As of January 1, 2020 Safari is the only browser that supports tail call optimization. The chromium team explicitly states that Tail Call Optimization is not under active development and can be tracked here. The implementation for Firefox can be tracked here Original Post Yes, ES2015 offers tail call optimization in strict mode. Dr. … Read more

Python: using a recursive algorithm as a generator

def getPermutations(string, prefix=””): if len(string) == 1: yield prefix + string else: for i in xrange(len(string)): for perm in getPermutations(string[:i] + string[i+1:], prefix+string[i]): yield perm Or without an accumulator: def getPermutations(string): if len(string) == 1: yield string else: for i in xrange(len(string)): for perm in getPermutations(string[:i] + string[i+1:]): yield string[i] + perm

Recursion using yield

Yes, you can do this: def infinity(start): yield start for x in infinity(start + 1): yield x This will error out once the maximum recursion depth is reached, though. Starting from Python 3.3, you’ll be able to use def infinity(start): yield start yield from infinity(start + 1) If you just call your generator function recursively … Read more