Python: LINQ capable class supporting simultaneously laziness and fluent design
<p>How to create an Python class which would be a iterable wrapper with LINQ-like methods (select, where, orderby, etc.) without using extension methods or monkey patching.
?</p>

<p>That is this LinqCapable class would be capable to return its own type when it is relevant (i.e. fluent design) and support lazy evaluation.</p>

<p>I'm just looking here for a snippet as a starting point.</p>

<p>You should return a "Linq capable class" to achieve chaining and furthermore your implementation  is not lazy: look at asq where method: it is based on the I filter and it appears correct...
Anyway, here it is a very basic implementation based on my understanding of your question and comments</p>

<pre><code>class LinqCapable(object):
    def __init__(self, iterable=None):
        self._iterable = iterable
        self._predicates = []
    def where(self, predicate):
        chain = LinqCapable(self._iterable)
        chain._predicates = self._predicates
        chain._predicates.append(predicate)
        return chain
    def toArray(self):
        for item in self._iterable:
          isOk = True
          for predicate in self._predicates:
            if (not predicate(item)):
                isOk = False
                break

          if (isOk):
              yield item
</code></pre>

<h2>Usage</h2>

<pre><code>test = LinqCapable([1,2,3])
def pred1(l: int) -&gt; bool:
   return l&gt;1
chain1 = test.where(pred1)
def pred2(l: int) -&gt; bool:
   return l&lt;3
chain2 = chain1.where(pred2)
list(chain2.toArray())
</code></pre>

<h2>Edit (adding also a select method)</h2>

<p>I've added also a simple select method.
The objective here is to aggregate predicates and selectors when possible, to avoid inefficient nested loops.</p>

<pre><code>class LinqCapable(object):
    def __init__(self, iterable=None, predicates = [],  selectors = [],  tree=None):
        self._iterable =  iterable
        self._predicates = list(predicates)
        self._selectors = list(selectors)
        self._tree = tree
    def select(self,  selector):
        if (len(self._predicates) == 0):
            chain = LinqCapable(self._iterable,  [], self._selectors,  self._tree)
            chain._selectors.append(selector) 
        else:
            chain = LinqCapable(None,  [],  [])
            if (len(self._selectors) == 0):
                chain._tree = LinqCapable(self._iterable,  self._predicates,  [],  self._tree)
            else:
                chain._tree = self
            chain._selectors.append(selector) 
        return chain
    def where(self, predicate):
        chain = LinqCapable(self._iterable,  self._predicates,  self._selectors,  self._tree)
        chain._predicates.append(predicate)
        return chain
    def enumerate(self):
        if  (self._tree != None):
            self._iterable = list(self._tree.enumerate())
        return self._cycle()
    def _cycle(self):
        for item in self._iterable:
          for selector in self._selectors:
            item = selector(item)
          isOk = True
          for predicate in self._predicates:
            if (not predicate(item)):
                isOk = False
                break
          if (isOk):
              yield item
</code></pre>

<p>so an example would be</p>

<pre><code>test = LinqCapable([1,2, 20,200, 300])
def pred1(l: int) -&gt; bool:
   return l&gt;1
chain1 = test.where(pred1)
def pred2(l: int) -&gt; bool:
   return l&lt;300
def sel1(l: int) -&gt; str:
   return str(l)
def sel2(l: str) -&gt; str:
   return '&lt;' + l + '&gt;'
def pred3(l: str) -&gt; bool:
   return len(l) &gt; 3
def sel3(l: str) -&gt; str:
   return l[1:-1]
def sel4(l: str) -&gt; int:
   return int(l)
chain2 = chain1.where(pred2).select(sel1).select(sel2).where(pred3).select(sel3).select(sel4)
print(list(chain2.enumerate()))
</code></pre>

python linq iterator generator