Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean code for sequence of map/filter/reduce functions

Tags:

python

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)
like image 555
iliasfl Avatar asked Dec 01 '14 05:12

iliasfl


People also ask

Can you explain the filter () map () and reduce () functions?

Anonymous Functions The functions map(), filter(), and reduce() all do the same thing: They each take a function and a list of elements, and then return the result of applying the function to each element in the list. As previously stated, Python has built-in functions like map(), filter(), and reduce().

What is lambda function and explain map () filter () reduce () functions with example programs?

The map() function in Python takes in a function and a list as an argument. The function is called with a lambda function and a list and a new list is returned which contains all the lambda modified items returned by that function for each item. Example: Example 1: Python.

What is the difference between map () filter () and reduce ()?

}, initialValue[, thisArg]); map creates a new array by transforming every element in an array individually. filter creates a new array by removing elements that don't belong. reduce , on the other hand, takes all of the elements in an array and reduces them into a single value.

What is map function and reduce function?

MapReduce facilitates concurrent processing by splitting petabytes of data into smaller chunks, and processing them in parallel on Hadoop commodity servers. In the end, it aggregates all the data from multiple servers to return a consolidated output back to the application.


3 Answers

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

like image 194
EntilZha Avatar answered Oct 12 '22 06:10

EntilZha


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.

like image 39
ymonad Avatar answered Oct 12 '22 04:10

ymonad


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]
like image 40
ssm Avatar answered Oct 12 '22 05:10

ssm