MethodChain

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bearophileHUGS@lycos.com

    MethodChain

    Found from Reddit, it's for e ECMA(Java)Scrip t, but something similar
    may be useful for Python too:




    Bye,
    bearophile
  • Marc 'BlackJack' Rintsch

    #2
    Re: MethodChain

    On Sat, 19 Jul 2008 08:55:23 -0700, bearophileHUGS wrote:
    Found from Reddit, it's for e ECMA(Java)Scrip t, but something similar
    may be useful for Python too:
    >

    http://blog.jcoglan.com/2008/07/16/w...rself-clearly/
    What's called `MethodChain` there seems to be function composition in
    functional languages. Maybe `functools` could grow a `compose()` function.

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • bearophileHUGS@lycos.com

      #3
      Re: MethodChain

      Marc 'BlackJack' Rintsch:
      What's called `MethodChain` there seems to be function composition in
      functional languages. Maybe `functools` could grow a `compose()` function.
      To me it looks like a quite more "refined" thing, it's an object, it
      has some special methods, etc. I think it's not too much difficult to
      implement it with Python.

      Bye,
      bearophile

      Comment

      • Marc 'BlackJack' Rintsch

        #4
        Re: MethodChain

        On Sat, 19 Jul 2008 13:57:33 -0700, bearophileHUGS wrote:
        Marc 'BlackJack' Rintsch:
        >What's called `MethodChain` there seems to be function composition in
        >functional languages. Maybe `functools` could grow a `compose()` function.
        >
        To me it looks like a quite more "refined" thing, it's an object, it
        has some special methods, etc. I think it's not too much difficult to
        implement it with Python.
        The methods are a problem IMHO. You can't add an own method/function with
        the name `fire()` or `toFunction()`. `MethodChain` has to know all
        functions/methods in advance. You can add the methods of whole classes at
        once and there are over 300 pre-added, this begs for name clashes.

        Ciao,
        Marc 'BlackJack' Rintsch

        Comment

        • Paul McGuire

          #5
          Re: MethodChain

          On Jul 20, 12:01 am, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
          The methods are a problem IMHO.  You can't add an own method/function with
          the name `fire()` or `toFunction()`.  `MethodChain` has to know all
          functions/methods in advance.  You can add the methods of whole classesat
          once and there are over 300 pre-added, this begs for name clashes.
          >
          Ciao,
                  Marc 'BlackJack' Rintsch
          If you shift the syntax just a bit, instead of writing a.b.c, pass a,
          b, and c as the args to a MethodChain object. Here's a rough stab at
          the problem:

          class MethodChain(obj ect):
          def __init__(self, *fns):
          self.fns = fns[:]
          def __call__(self,* args):
          if self.fns:
          for f in self.fns:
          args = (f(*args),)
          return args[0]

          def dncase(s):
          return s.lower()

          def upcase(s):
          return s.upper()

          def stripVowels(s):
          return "".join( c for c in s if c not in "aeiou" )

          def selectItems(ite ms,s):
          return "".join(c for i,c in enumerate(s) if i in items)

          from functools import partial

          chn = MethodChain(
          dncase,
          stripVowels,
          upcase,
          partial(selectI tems,(0,2))
          )

          print chn("FoO Bar")


          -- Paul

          Comment

          • James Coglan

            #6
            Re: MethodChain

            Name clashes aren't an issue, since MethodChain doesn't apply any
            special meaning to the method names it knows; the limitation is
            because JavaScript doesn't allow you to modify property lookup
            behavior.  And since we can make the chain object callable, we don't
            need "fire" or "toFunction " methods.
            I'm the author of MethodChain, so just thought I'd confirm the above
            statement. All MethodChain does is store method calls so they can
            later be replayed on any object. All methods in MethodChain simply add
            their name and arguments to an array inside the MethodChain instance,
            they don't implement any concrete functionality. All that's important
            is the names of the methods -- the object the chain is fired on will
            decide how to handle those calls itself, so naming clashes aren't a
            problem. For example:

            var chain = it().toLowerCas e().split('-').map(function () {...});
            chain.fire('my-String');

            is the same as

            'my-String'.toLower Case().split('-').map(function () {...});

            So split() gets called on 'my-string', map() gets called on ['my',
            'string']. The methods 'fire' and 'toFunction' are a problem but I
            can't see any way around having them in JavaScript -- you need some
            way of getting the method list out of the chain object. if JavaScript
            had method_missing, we wouldn't need to tell MethodChain about names
            in advance either.

            Comment

            Working...