Clean code for sequence of map/filter/reduce functions
Question:
Is there a simple way to code in one line a sequence of map/filter/reduce functions?
For example instead of:
reduce(lambda x, y: x*y, filter(lambda x: x>0, map(lambda x: x - 1, some_list)))
I am looking for something like:
some_list.map(lambda x: x -1, a).filter(lambda x: x>0).reduce(lambda x, y: x*y)
Answers:
If you can use foreign library, look for
https://github.com/serkanyersen/underscore.py
It is python port of underscore.js. Therefore you can use chain()
method to begin filter
, map
, reduce
, and get the result by value()
method.
Using this library, you can write something like
from underscore import _
my_array = [10, 48, -1, 30, 20, 0]
result = _(my_array).chain().map(lambda x,*a: x - 1).filter(lambda x: x > 0).reduce(lambda x,y,*a: x*y, 1).value()
I tested in python 3.4.2 and seems to work fine.
You can use your own list:
class Mylist(list):
def __init__(self, l):
list.__init__(self,l)
def map(self, f):
return Mylist(map(f, self[:]))
In this case, we just subclass the list to a new list method. Then, you can add the map
, filter
and reduce
methods to the class. I have shown how to add the map
method. The other functions will be very similar.
Note that in this case, you can chain the map
and filter
functions as much as you want, but the reduce
method will generally not result in a list it wont be possible to chain functions anymore.
Here is an example output:
In [16]: xx = Mylist([1,2,3])
In [17]: xx.map(lambda m: m*2).map(lambda m: m**2)
Out[17]: [4, 16, 36]
PyFunctional
lets you do exactly that after installing via pip install PyFunctional
from functional import seq
seq(some_list).map(lambda x: x -1, a).filter(lambda x: x>0).reduce(lambda x, y: x*y)
The package can do much more than that, which you can checkout at
github.com/EntilZha/PyFunctional
Pyterator can do this too!
from pyterator import iterate
(
iterate(some_list)
.map(lambda x: x-1)
.filter(lambda x: x>0)
.reduce(lambda x, y: x*y)
)
You can install it with
pip install git+https://github.com/remykarem/pyterator#egg=pyterator
Is there a simple way to code in one line a sequence of map/filter/reduce functions?
For example instead of:
reduce(lambda x, y: x*y, filter(lambda x: x>0, map(lambda x: x - 1, some_list)))
I am looking for something like:
some_list.map(lambda x: x -1, a).filter(lambda x: x>0).reduce(lambda x, y: x*y)
If you can use foreign library, look for
https://github.com/serkanyersen/underscore.py
It is python port of underscore.js. Therefore you can use chain()
method to begin filter
, map
, reduce
, and get the result by value()
method.
Using this library, you can write something like
from underscore import _
my_array = [10, 48, -1, 30, 20, 0]
result = _(my_array).chain().map(lambda x,*a: x - 1).filter(lambda x: x > 0).reduce(lambda x,y,*a: x*y, 1).value()
I tested in python 3.4.2 and seems to work fine.
You can use your own list:
class Mylist(list):
def __init__(self, l):
list.__init__(self,l)
def map(self, f):
return Mylist(map(f, self[:]))
In this case, we just subclass the list to a new list method. Then, you can add the map
, filter
and reduce
methods to the class. I have shown how to add the map
method. The other functions will be very similar.
Note that in this case, you can chain the map
and filter
functions as much as you want, but the reduce
method will generally not result in a list it wont be possible to chain functions anymore.
Here is an example output:
In [16]: xx = Mylist([1,2,3])
In [17]: xx.map(lambda m: m*2).map(lambda m: m**2)
Out[17]: [4, 16, 36]
PyFunctional
lets you do exactly that after installing via pip install PyFunctional
from functional import seq
seq(some_list).map(lambda x: x -1, a).filter(lambda x: x>0).reduce(lambda x, y: x*y)
The package can do much more than that, which you can checkout at
github.com/EntilZha/PyFunctional
Pyterator can do this too!
from pyterator import iterate
(
iterate(some_list)
.map(lambda x: x-1)
.filter(lambda x: x>0)
.reduce(lambda x, y: x*y)
)
You can install it with
pip install git+https://github.com/remykarem/pyterator#egg=pyterator