Skip to content

Commit

Permalink
Added compact()
Browse files Browse the repository at this point in the history
  • Loading branch information
Suor committed Feb 27, 2013
1 parent 8bd304d commit b45410a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,6 @@
0.3.3
- added compact()

0.3.2 0.3.2
- added ilen() - added ilen()
- added some object helpers: namespace base class and @cached_property - added some object helpers: namespace base class and @cached_property
Expand Down
1 change: 1 addition & 0 deletions README.rst
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Select a part of collection::
select(even, {1,2,3,10,20}) # {2,10,20} select(even, {1,2,3,10,20}) # {2,10,20}
select(re_tester('^a'), ('a','b','ab','ba')) # ('a','ab') select(re_tester('^a'), ('a','b','ab','ba')) # ('a','ab')
select_keys(callable, {str: '', None: None}) # {str: ''} select_keys(callable, {str: '', None: None}) # {str: ''}
compact({2, None, '', 0}) # {2,'',0}




Test collection contents:: Test collection contents::
Expand Down
9 changes: 9 additions & 0 deletions docs/colls.rst
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ Transform and select
select_values(bool, some_dict) select_values(bool, some_dict)




.. function:: compact(pred, coll)

Removes ``None`` values from given collection. When compacting a dict all keys with ``None`` values are trashed.

Extract integer data from request::

compact(walk_values(silent(int), request_dict))


Dict utils Dict utils
---------- ----------


Expand Down
2 changes: 1 addition & 1 deletion from_underscore.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ countby
foldl?=reduce foldr? foldl?=reduce foldr?


# Array # Array
compact=keep/1? without(relates to conj) without(relates to conj)
flatten? flatten?


# Functions # Functions
Expand Down
11 changes: 10 additions & 1 deletion funcy/colls.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@




__all__ = ['empty', 'iteritems', 'join', 'merge', __all__ = ['empty', 'iteritems', 'join', 'merge',
'walk', 'walk_keys', 'walk_values', 'select', 'select_keys', 'select_values', 'walk', 'walk_keys', 'walk_values', 'select', 'select_keys', 'select_values', 'compact',
'is_distinct', 'all', 'any', 'none', 'one', 'some', 'is_distinct', 'all', 'any', 'none', 'one', 'some',
'zipdict', 'flip', 'project', 'zipdict', 'flip', 'project',
'where', 'pluck', 'invoke'] 'where', 'pluck', 'invoke']
Expand Down Expand Up @@ -90,6 +90,15 @@ def select_keys(pred, coll):
def select_values(pred, coll): def select_values(pred, coll):
return select(lambda (k, v): pred(v), coll) return select(lambda (k, v): pred(v), coll)



def compact(coll):
pred = lambda x: x is not None
if isinstance(coll, Mapping):
return select_values(pred, coll)
else:
return select(pred, coll)


### Content tests ### Content tests


def is_distinct(coll): def is_distinct(coll):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_colls.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ def test_select_keys():
def test_select_values(): def test_select_values():
assert select_values(_ % 2, {'a': 1, 'b': 2}) == {'a': 1} assert select_values(_ % 2, {'a': 1, 'b': 2}) == {'a': 1}


def test_compact():
assert eq(compact([0, 1, None, 3]), [0, 1, 3])
assert eq(compact((0, 1, None, 3)), (0, 1, 3))
assert eq(compact({'a': None, 'b': 0, 'c': 1}), {'b': 0, 'c': 1})


def test_is_distinct(): def test_is_distinct():
assert is_distinct('abc') assert is_distinct('abc')
Expand Down

0 comments on commit b45410a

Please sign in to comment.