Azder
Azder

Reputation: 4728

Mappers, Reducers, FIlters

I know about map/reduce alghoritm and its use. It's using functions that are called Mappers and Reducers, but I also find people use the word Filters.

Are Filters same as Mappers or is there some significant difference?

Upvotes: 1

Views: 320

Answers (4)

Alex
Alex

Reputation: 4362

Generally, map functions take an input set and a function, and returns a set containing the function output for each input element. A filter takes an input set and a boolean function, and returns a set containing the input values for which the function returns true.

Upvotes: 0

Charlie Martin
Charlie Martin

Reputation: 112366

A filter is like a map for which the passed function is always a "characteristic function", that is a function that returns either "yes" or "no" to the question "does this belong here?"

In other words, think of a set defined as {x | x ∈ X and P(x) }. Filter takes the base set, tests to see if P(x) is true, and returns only those members for which it is true.

So { x | x is a natural number and odd(x) } is {1,3,5,7...}.

A map applies an arbitrary function, so you can think of that as a set like { y | x ∈ X and y = f(x) }.

So { y | x is a natural number and y = x² } is {1,4,9,16,...}.

Upvotes: 8

Emil H
Emil H

Reputation: 40240

A filter determines if an item should be kept or removed. A mapper just translates the value in to another. As a consequence: The output set of a map operation is always equal in size with the input set. The output on a filter operation is smaller than the input set.

Upvotes: 1

Patrick
Patrick

Reputation: 92520

Filter takes a "list" and a function, applies the function to every member of the list and returns a new list containing only members where the application of the function returned true. For instance:

l = [1,2,3,4]
l = filter(lambda x: x < 3, l)
print l # [1,2]

Map does the same thing, but returns a list containing the results of the function application:

l = [1,2,3,4]
l = map(lambda x: x < 3, l)
print l # [True,True,False,False]

Upvotes: 1

Related Questions