Skip to content

Commit

Permalink
added union, difference, intersection, and symmetric difference
Browse files Browse the repository at this point in the history
  • Loading branch information
EntilZha committed Mar 23, 2015
1 parent 22367ff commit 498a37e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
53 changes: 53 additions & 0 deletions functional/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,59 @@ def take_while(self, f):
break
return self.take(i)

def union(self, other):
"""
New sequence with unique elements from self.sequence and other.
>>> seq([1, 1, 2, 3, 3]).union([1, 4, 5])
[1, 2, 3, 4, 5]
:param other: sequence to union with
:return: union of sequence and other
"""
result = set(self.sequence).union(set(other))
return FunctionalSequence(list(result))

def intersection(self, other):
"""
New sequence with unique elements present in sequence and other.
>>> seq([1, 1, 2, 3]).intersection([2, 3, 4])
[2, 3]
:param other: sequence to perform intersection with
:return: intersection of sequence and other
"""
result = set(self.sequence).intersection(set(other))
return FunctionalSequence(list(result))

def difference(self, other):
"""
New sequence with unique elements present in sequence but not in other.
>>> seq([1, 2, 3]).difference([2, 3, 4])
[1]
:param other: sequence to perform difference with
:return: difference of sequence and other
"""
result = set(self.sequence).difference(set(other))
return FunctionalSequence(list(result))

def symmetric_difference(self, other):
"""
New sequence with elements in either sequence or other, but not both.
>>> seq([1, 2, 3, 3]).symmetric_difference([2, 4, 5])
[1, 3, 4, 5]
:param other: sequence to perform symmetric difference with
:return: symmetric difference of sequence and other
"""

result = set(self.sequence).symmetric_difference(set(other))
return FunctionalSequence(list(result))

def map(self, f):
"""
Maps f onto the elements of the sequence.
Expand Down
24 changes: 24 additions & 0 deletions test/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,30 @@ def test_take_while(self):
self.assertEqual(expect, result)
self.assertType(result)

def test_union(self):
result = seq([1, 1, 2, 3, 3]).union([1, 4, 5])
expect = [1, 2, 3, 4, 5]
self.assertType(result)
self.assertEqual(result, expect)

def test_intersection(self):
result = seq([1, 2, 2, 3]).intersection([2, 3, 4, 5])
expect = [2, 3]
self.assertType(result)
self.assertEqual(result, expect)

def test_difference(self):
result = seq([1, 2, 3]).difference([2, 3, 4])
expect = [1]
self.assertType(result)
self.assertEqual(result, expect)

def test_symmetric_difference(self):
result = seq([1, 2, 3, 3]).symmetric_difference([2, 4, 5])
expect = [1, 3, 4, 5]
self.assertType(result)
self.assertEqual(result, expect)

def test_map(self):
f = lambda x: x * 2
l = [1, 2, 0, 5]
Expand Down

0 comments on commit 498a37e

Please sign in to comment.