Skip to content

Commit

Permalink
Added select and where aliases resolving #40 and #41
Browse files Browse the repository at this point in the history
  • Loading branch information
EntilZha committed Oct 27, 2015
1 parent 6aeb867 commit ec9c95c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
24 changes: 24 additions & 0 deletions functional/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,18 @@ def map(self, func):
"""
return self._transform(transformations.map_t(func))

def select(self, func):
"""
Selects f from the elements of the sequence.
>>> seq([1, 2, 3, 4]).select(lambda x: x * -1)
[-1, -2, -3, -4]
:param func: function to select with
:return: sequence with func mapped onto it
"""
return self._transform(transformations.select_t(func))

def for_each(self, func):
"""
Executes func on each element of the sequence.
Expand Down Expand Up @@ -489,6 +501,18 @@ def filter_not(self, func):
"""
return self._transform(transformations.filter_not_t(func))

def where(self, func):
"""
Selects elements where func evaluates to True.
>>> seq([-1, 1, -2, 2]).where(lambda x: x > 0)
[1, 2]
:param func: function to filter on
:return: filtered sequence
"""
return self._transform(transformations.where_t(func))

def count(self, func):
"""
Counts the number of elements in the sequence which satisfy the predicate func.
Expand Down
17 changes: 17 additions & 0 deletions functional/test/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ def test_map(self):
self.assertIteratorEqual(expect, result)
self.assert_type(result)

def test_select(self):
f = lambda x: x * 2
l = [1, 2, 0, 5]
expect = [2, 4, 0, 10]
result = seq(l).select(f)
self.assertIteratorEqual(expect, result)
self.assert_type(result)

def test_filter(self):
f = lambda x: x > 0
l = [0, -1, 5, 10]
Expand All @@ -262,6 +270,15 @@ def test_filter(self):
self.assertIteratorEqual(expect, result)
self.assert_type(result)

def test_where(self):
f = lambda x: x > 0
l = [0, -1, 5, 10]
expect = [5, 10]
s = seq(l)
result = s.where(f)
self.assertIteratorEqual(expect, result)
self.assert_type(result)

def test_filter_not(self):
f = lambda x: x > 0
l = [0, -1, 5, 10]
Expand Down
8 changes: 8 additions & 0 deletions functional/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,18 @@ def map_t(func):
return Transformation('map({0})'.format(name(func)), partial(map, func), None)


def select_t(func):
return Transformation('select({0})'.format(name(func)), partial(map, func), None)


def filter_t(func):
return Transformation('filter({0})'.format(name(func)), partial(filter, func), None)


def where_t(func):
return Transformation('where({0})'.format(name(func)), partial(filter, func), None)


def filter_not_t(func):
return Transformation('filter_not({0})'.format(name(func)), partial(filterfalse, func), None)

Expand Down

0 comments on commit ec9c95c

Please sign in to comment.