- implemented half of the underscore.js funtionality (collection, array, object)
- all the built-in methods are still available on _ object.
check if all items can pass the func
(return True
)
func
default is bool. every
is an alias of all
.
@param : function(a) -> bool
@return : bool
_([1,2,3]).all(lambda x: x>0)
=> True
check if any item in self._ can pass the func
(return True
)
func default is bool. any
is an alias of some
@param : function(a) -> bool
@return : bool
_([1,2,3]).some(lambda x: x>2)
=> True
return all but last n items. n default is 1.
take
and initial
are its aliases.
@param : int
@return : _
_([1,2,3,4]).but_last()._
=> [1,2,3]
math.ceil
@param : none
@return : _(float)
_(1.2).ceil()._
=> 2.0
builtin chr
@param : none
@return : _(str)
_(65).chr()._
'A'
divide the self._ into n-size list
@param : int
@return : _([[a]])
_([1,2,3,4]).chunks(3)._
=> [[1,2,3], [4]]
shallow copy
@param : none
@return : _
_a = _([1,2,[3]])
b = _a.copy()
b == _a._
=> True
it's equivalent to reject(lambda x: bool(x))
@param : none
@return : _
_(['', None, False, 0]).compact()._
=> []
check if item is part of self._
@param : a
@return : bool
_([1,2,3]).contains(1)
=> True
_({'a': 1,'b': 2, 'c': 3}).contains({'a':1, 'c':3})
=> True
(deep or shallow) copy self._
. deep
default is False
@param : bool
@return : _
_a = _([1,2,3])
b = _a.copy()
b == _a._
=> True
b.append('hello')
b == _a._
=> False
count a certain item
@param : item
@return : _(int)
_([1,2,3,2,1]).count(1)._
=> 2
count each element in each group. func
default is bool
@param : function(a) -> b
@return : _(dict)
_([1,2,3,4,5]).count_by(lambda x: x%2)._
=> {1: 3, 0: 2}
deep copy self._
@param : none
@return : _
_a = _([1,2,[3]])
b = _a.copy()
b == _a._
=> True
b[-1].append('4')
print b
=> [1,2,[3,4]]
print _a._
=> [1,2,[3]]
add key and value to self._ only if key is not in self._ .
@param : *kwargs
@return : _(dict)
_({"a":1}).default({"a":2, "b": 3})._
=> {"a": 1, "b": 3}
make a dict
with keys as key and self._ as value
@param : a
@return : _(dict)
_([1,2,3]).dict_keys(['a', 'b', 'c'])._
=> {'a': 1, 'b': 2, 'c': 3}
make a dict
with values
as value and self._
as key
@param : a
@return : _(dict)
_(['a', 'k']).dict_values((1,2))._
=> {'a': 1, 'k': 2}
items in self._ but not present in lists
if self._ is a set, then the return _ object holds a set.
diff
is its alias.
@param : *lists
@return : _
_([1,2,2,3]).differece([2,3,4], [2,5])._
=> [1]
# set
_({1,2,3,4}).difference([1,2,6])._
=> set([3,4])
items in self._ but not present in lists
if self._ is a set, then the return _ object holds a set.
diff
is its alias.
@param : *lists
@return : _
_([1,2,2,3]).differece([2,3,4], [2,5])._
=> [1]
# set
_({1,2,3,4}).difference([1,2,6])._
=> set([3,4])
iterate from self._
to n
pass the number to function fn
.
@param : int
@param : function(int)
@return : _(int)
result = []
_(5).down_to(1, lambda x: result.append(x))
print result
=> [5,4,3,2]
apply the func
to every item
type: function -> _
@param : function(a)
@return : _
def printit(it): print(it)
_([1,2,3]).each(printit)
=> 1
=> 2
=> 3
check if the number is even
@param : none
@return : bool
_(2).even()
=> True
check if all items can pass the func
(return True
)
func
default is bool. every
is an alias of all
.
@param : function(a) -> bool
@return : bool
_([1,2,3]).all(lambda x: x>0)
=> True
looks through the self._ and collect the items that passed
func
(return true). the default func
is bool
.
@param : function(a) -> bool
@return : _
_([1,2,3,4]).filter(lambda x: x % 2 == 0)._
=> [2,4]
find the first item when func(item)
reutrn True
.
it return as soon as it finds such an item.
@param : function(a) -> bool
@return : _(a) || None
_([1,2,2,4]).find_item(lambda x: x == 2)._
=> 2
find the first item whoses (key, value) pair matches cond
@param : dict
@return : _(dict) || None
_([{"a": 1, "b":2}, {"c":1, "d":2}]).find_where({"a":1})._
=> {"a": 1, "b": 2}
get the first value . the same as self._[0]
@param : none
@return : _(a)
_([1,2,3]).first._
=> 1
flatten a list, when deep
is set to True
,
this will recursively flatten the whole list.
deep
default is False
@param : bool
@return : _
_([1,2,[3], [[4]]]).flatten()._
=> [1,2,3,[4]]
_([1,2,[3], [[4]]]).flatten(True)._
=> [1,2,3,4]
math.floor
@param : none
@return : _(float)
_(1.2).floor()._
=> 1.0
group items by function, return a dict which key is generated by
func
and value is a list.
@param : function(a) -> b
@return : _(dict)
_([1,2,3,4]).group_by(lambda x: x%2)._
=> {1: [1,3], 0: [2,4]}
return all but last n items. n default is 1.
take
and initial
are its aliases.
@param : int
@return : _
_([1,2,3,4]).but_last()._
=> [1,2,3]
convert to integer
@param : none
@return : _(int)
_(2.3).int()._
2
self._ and lists intersection. if self._ is a set, then the return _ object holds a set.
@param : *lists
@return : _
_([1,2,2,3]).intersection([2,3,4], [2,5])._
=> [2]
# set
_({1,2,3,4}).intersection([1,2,6])._
=> set([1,2])
invert dict's key and value
@param : none
@return : _(dict)
_({"k1": "v1", "k2": "v2"}).invert()._
{"v1": "k1", "v2": "k2"}
invoke method with args on every item
@param : function
@param : *args
@param : **kwargs
@return : _
_([1,2,3], [3,4,5]).invoke("append", 'hello')._
=> [[1,2,3,"hello"], [1,2,3,"hello"]]
test if self._ is a t
.
@param : type
@return : bool
_([1,2,3,4]).is_a(list)
=> True
concat a list of strings by sep which is string, and if sep is a list of string then concat the sep by self._
@param : [str] || str
@return : _(str)
_(['a', 'b']).join('/')._
=> 'a/b'
_('/').join(['a', 'b'])._
=> 'a/b'
get the last item . the same as self._[-1]
@param : none
@return : _(a)
_([1,2,3]).last._
=> 3
return the last index of item in self._
.
since it use the builtin index
method, if no such item found
it will raise ValueError.
last_index_of
is its alias.
_([1,3,2,3]).last_index(3)._
=> 3
return the last index of item in self._
.
since it use the builtin index
method, if no such item found
it will raise ValueError.
last_index_of
is its alias.
_([1,3,2,3]).last_index(3)._
=> 3
apply func
to every item, and collect the result.
@param : function(a) -> b
@return : _
_([1,2,3]).map(lambda x: x+1)._
=> [2,3,4]
get the max item using a key function fn
fn
default is lambda x: x
@param : function(a) -> b
@return : _(a)
_([1,2,3,4]).max(lambda x: -x)._
=> -1
get the min item using a key function fn
fn
default is lambda x: x
@param : function(a) -> b
@return : _(a)
_([1,2,3,4]).min(lambda x: -x)._
=> 4
check if the number is odd.
@param : none
@return : bool
_(2).odd()
=> False
get a new dict by filtering out keys from self._
@param : *keys
@return : _(dict)
_({"a": 1, "b": 2, "c": 2}).omit("b", "c")._
=> {"a": 1}
list of lists containing two items. if self._ is dict then it will be key-value tuple.
@param : none
@return : _([(a,b)])
_([1,2,3,4,5]).pairs()._
=> [[1,2], [3,4], [5]]
_({"a": 1, "b": 2}).pairs()._
=> [("a", 1), ("b", 2)]
get a new dict by picking from self._
by keys
@param : *keys
@return : _(dict)
_({"a": 1, "b": 2}).pick("a")._
=> {"a": 1}
extracting a list of property values from self._ which is a list of dict
@param : a
@return : _([])
_([{"a": 1, "b": 2}, {"a": 3, "c": 4}]).pluck("a")._
=> [1,3]
@param : none
@return : _(int)
_(2).pred()._
=> 1
left-fold the self._ by func
with initial value set to init
@param : function(t, s) -> t
@param : init=t
@return : _
_([1,2,3]).reduce(lambda t, x: t-x)._
=> -4
right-fold the self._ by func
with initial value set to init
@param : function(t, s)
@param : init=t
@return : _
_([1,2,3]).reduce_right(lambda t, x: t-x)._
=> 0
collect items which dosen't pass the func
(return False
)
func
default is bool
@param : function(a) -> bool
@return : _
_([1,2,3,4]).reject(lambda x: x%2 == 0)._
=> [1,3]
return all but first n items. n deafult is 1.
@param : int
@return : _
_([1,2,3,4]).rest()._
=> [2,3,4]
@param : none
@return : _
_([1,2,3]).shuffle()._
=> ...
return the size of the collection
@param : none
@return : _(int)
_([1,2,3]).size()._
=> 3
check if any item in self._ can pass the func
(return True
)
func default is bool. any
is an alias of some
@param : function(a) -> bool
@return : bool
_([1,2,3]).some(lambda x: x>2)
=> True
sort the self._
@param : function(a) -> k
@return : _
_([3,2,1]).sorted(lambda x: x)._
=> [1,2,3]
Uses a binary search to determine the index at which the value should be inserted into the list in order to maintain the list's sorted order the return index will be as large as possible. see the examples.
@param : a
@return : _(int)
_([1,2,3,4,5,6]).sorted_index(4)._
=> 4
_([1,2,3,4,4,4,5,6]).sorted_index(4)._
=> 6
@param : none
@return : _(int)
_(2).succ()._
=> 3
return all but last n items. n default is 1.
take
and initial
are its aliases.
@param : int
@return : _
_([1,2,3,4]).but_last()._
=> [1,2,3]
call fn self._ times
@param : function()
@return : _(number)
def p(): print('s')
_(3).times(p)
=> s
=> s
=> s
merge *lists
if there is any. and delete duplicated items.
if self._ is a set, then the return _ object holds a set.
@param : *lists
@return : _
_([1,2,2,3]).union()._
=> [1,2,3]
_([1,2,2,3]).union([2,3,4], [4,5])._
=> [1,5]
# set
_({1,2,3,4}).union([1,2,6])._
=> set([1,2,3,4,6])
no duplicated items.
@param : none
@return : _
_([1,2,3,2]).uniq()._
=> [1,2,3]
iterate from self._
to n
pass the number to function fn
.
@param : int
@param : function(int)
@return : _(int)
result = []
_(5).down_to(1, lambda x: result.append(x))
print result
=> [5,4,3,2]
get the value out of the _ object
@param : none
@return : self._
_([1,2,3]).value()
=> [1,2,3]
# which is the same as _([1,2,3])._
find all items whose (key, value) pairs matches cond
@param : dict
@return : _([dict])
_([{"a": 1, "b":2}, {"a": 1, "b":1}, {"c":1, "d":2}]).find_where({"a":1})._
=> [{"a": 1, "b":2}, {"a": 1, "b":1}]
remove any item in args
from self._
@param : *args
@return : _
_([1,2,2,3,4,1]).without(2,1)._
=> [3,4]
merge self._ and lists in a way that each time pick one item from each of the lists and self._ , then make a tuple out of it.
@param : *lists
@return : _
_([1,2,3,4]).zip([1,2,3])._
=> [(1,1), (2,2), (3,3)]