Ian Bicking: the old part of his blog

Re: Functional Python

What I liked most about OCaml (and I think Haskell has it, too) is pattern matching. I don't think that it will ever make its way into python and I'm not sure if I liked it either.

But it's definately one of the cooler, functional language/syntax features out there.

Somehow it could be done, though.

def insertElementIntoTree(tree, element):
  if element < tree.left:
    if (Pattern(tree) == Pattern(left=None) and element < tree.left:
    # ...

Anyway, it's kind of obscuring the code if Pattern would not become part of the standard library.

Comment on Functional Python
by Justin Bayer


It is extremly easy to implement, by the way: http://home.in.tum.de/~bayerj/patternmatch.py

(not extremly well tested, just a scratch...)

# Justin Bayer

Generic functions, or particularly RuleDispatch, accomplishes just this. Maybe like:

def insert_element(tree, el):
    raise NotImplementedError

@insert_element.when('tree.left is None or el < tree.left')
def insert_element_left(tree, el):
    insert_element(tree.left, el)


At least, I think that does what you want. I wouldn't bank on the performance of that algorithm ;) But the functionality is there, particular in RuleDispatch (which with arbitrary predicate dispatch is more general/powerful than what is being proposed for py3k). Well, I guess I'm not entirely clear on the intention f Pattern(left=None); I guess I'd express that as lambda tree: tree.left is None. I assume the distinction is primarily in a language like OCaml, where you can statically analyze aspects of patterns, which is not generally possible in Python.

# Ian Bicking

Is the reason that you don't bank on the performance ;) that eval()-ing the condition in quotes would be a bottleneck?

# Justin Bayer

It's not eval'd, it's compiled on import; it's just that doing that branching operation for every pass of a rather tight loop (in tree traversal) isn't an efficient way to do that operation.

# Ian Bicking

www.katalog.wesnoth.pl holifld.info/katalog www.elate.pl/cat/ marek.holewinski.pl/katalog www.strony.pinia.pl www.dobredomy.pl/katalog/ www.artefakt.pl/katalog/ katalog.programisci.com.pl strony.shaco.pl katalog.twojapraca.info www.katalog.gigante.pl www.kat.plusk.info k1024.toreuse.pl b2b.wroclaw.pl/katalog/ news.stylokna.pl ale.biurka.pl k1024.telewizorek.pl stop.perfectfresh.com kat.gigahurt.pl www.strony.prbox.pl katalog.gcity.pl ups.markpol.biz www.coolmarket.net/katalog

# Adim