Calmcode - method chains: args

Allowing for multiple lambda functions with *args.

1 2 3 4 5 6 7 8 9

This is the class that we had before.

class Clumper:
    def __init__(self, blob):
        self.blob = blob

    def keep(self, func):
        return Clumper([d for d in self.blob if func(d)])

This is the class that we have now.

class Clumper:
    def __init__(self, blob):
        self.blob = blob

    def keep(self, *funcs):
        data = self.blob
        for func in funcs:
            data = [d for d in data if func(d)]
        return Clumper(data)

We can still perform commands in a chain.

(Clumper(poke_dict)
  .keep(lambda d: 'Grass' in d['type'])
  .keep(lambda d: d['hp'] < 60)
  .blob)

But we could also put all of our filtering functions into one keep call.

(Clumper(poke_dict)
  .keep(lambda d: 'Grass' in d['type'],
        lambda d: d['hp'] < 60)
  .blob)