Skip to content

Instantly share code, notes, and snippets.

@dunossauro
Created December 18, 2017 03:35
Show Gist options
  • Save dunossauro/9f68663b01c22fc3a3a328b26ca2da01 to your computer and use it in GitHub Desktop.
Save dunossauro/9f68663b01c22fc3a3a328b26ca2da01 to your computer and use it in GitHub Desktop.
Rapidinha #3 - programação declarativa com python
from collections.abc import MutableSequence
from itertools import islice
from functools import reduce
class List(MutableSequence):
def __init__(self, seq):
self._d = list(seq)
def __delitem__(self, pos):
del self._d[pos]
def __getitem__(self, pos):
return self._d[pos]
def __setitem__(self, pos, val):
self._d[pos] = val
def __len__(self):
return len(self._d)
def insert(self, pos, val):
self._d.insert(pos, val)
def __repr__(self):
return 'List({})'.format(self._d)
def map(self, function):
return List(map(function, self._d))
def filter(self, function):
return List(filter(function, self._d))
def reduce(self, function):
return reduce(function, self._d)
def take(self, n):
return List(islice(self._d, n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment