[BangPypers] Clojure style multimethod functions in python

Rajeev J Sebastian rajeev.sebastian at gmail.com
Mon Aug 23 16:09:32 CEST 2010


On Mon, Aug 23, 2010 at 6:35 PM, Dhananjay Nene
<dhananjay.nene at gmail.com> wrote:
> On Mon, Aug 23, 2010 at 4:26 PM, Rajeev J Sebastian <
> rajeev.sebastian at gmail.com> wrote:
>
>> Probably better to use PEAK Rules....
>>
>> Regards
>> Rajeev J Sebastian
>>
>
> That looks much more fully featured indeed. But I couldn't understand why
> features such as the one I "copy-pasted" below are relevant when a simple
> lambda is all that is required :
>
>
>   - The Basic AST
> Builder<http://peak.telecommunity.com/DevCenter/PEAK-Rules/AST-Builder>and
> advanced Code
>   Generation<http://peak.telecommunity.com/DevCenter/PEAK-Rules/Code-Generation>
>   - Criteria <http://peak.telecommunity.com/DevCenter/PEAK-Rules/Criteria>,
>   Indexing <http://peak.telecommunity.com/DevCenter/PEAK-Rules/Indexing>,
>   and Predicates<http://peak.telecommunity.com/DevCenter/PEAK-Rules/Predicates>
>   - Syntax pattern
> matching<http://peak.telecommunity.com/DevCenter/PEAK-Rules/Syntax-Matching>

When you have a large number of rules, you want to optimize the
dispatcher to select the most appropriate rule in the fastest possible
way. In your case, only the result of the 'ticket' method is ever used
for the dispatch; imagine if different methods had different
conditions altogether. For e.g.:


@when("product.days_since_last_sale > days(365)")
def discount(product, user):
        return "10%"

@when("product.category == 'football' and user.is_football_lover")
def discount(product, user):
      return "50%"

@when("product.category == 'cricket'")
def discount(product, user):
      return "0%"

etc.

Regards
Rajeev J Sebastian


More information about the BangPypers mailing list