Array
: All you want for a List type is here¶
-
class
carriage.
Array
(items=None)¶ -
accumulate
(func=None)¶ Create a new Array of calling
itertools.accumulate
-
append
(item)¶ Append element to the Array
-
appended
(item)¶ Create a new Array that extends source Array with another element.
-
butlast
()¶ Create a new Array that last element dropped
-
distincted
()¶ Create a new Array with non-repeating elements. And elements are with the same order of first occurence in the source Array.
-
drop
(n)¶ Create a new Array of first n element dropped
-
drop_right
(n)¶ Create a new Array that last n elements dropped
-
drop_while
(pred)¶ Create a new Array without elements as long as predicate evaluates to true.
-
dropright
(n)¶ Create a new Array that last n elements dropped
-
dropwhile
(pred)¶ Create a new Array without elements as long as predicate evaluates to true.
-
extend
(iterable)¶ Extend the Array from iterable
-
extended
(iterable)¶ Create a new Array that extends source Array with another iterable
-
filter
(pred)¶ Create a new Array contains only elements passing predicate
-
filter_false
(pred)¶ Create a new Array contains only elements not passing predicate
-
filterfalse
(pred)¶ Create a new Array contains only elements not passing predicate
-
find
(pred)¶ Get first element satifying predicate
-
find_opt
(pred)¶ Optionally get first element satifying predicate. Return Some(element) if exist Otherwise return Nothing
-
first
()¶ Get first element
-
first_opt
()¶ Get first element as Some(element), or Nothing if not exists
-
flat_map
(to_iterable_action)¶ Apply function to each element, then flatten the result.
>>> Array([1, 2, 3]).flat_map(range) Array([0, 0, 1, 0, 1, 2])
Returns: Return type: Array
-
flatten
()¶ flatten each element
>>> Array([(1, 2), (3, 4)]).flatten() Array([1, 2, 3, 4])
Returns: Return type: Array
-
get
(index, default=None)¶ Get item of the index. Return default value if not exists.
-
get_opt
(index)¶ Optionally get item of the index. Return Some(value) if exists. Otherwise return Nothing.
-
group_by
(key=None)¶ Create a new Array using the builtin itertools.groupby, which sequentially groups elements as long as the key function evaluates to the same value.
Comparing to
group_by_as_map
, there’re some pros and cons.Cons:
- Elements should be sorted by the key function first, or elements with the same key may be broken into different groups.
Pros:
- Key function doesn’t have to be evaluated to a hashable value.
It can be any type which supports
__eq__
.
-
group_by_as_map
(key=None)¶ Group values in to a Map by the value of key function evaluation result.
Comparing to
group_by
, there’re some pros and cons.Pros:
- Elements don’t need to be sorted by the key function first.
You can call
map_group_by
anytime and correct grouping result.
Cons:
- Key function has to be evaluated to a hashable value.
- Elements don’t need to be sorted by the key function first.
You can call
-
interpose
(sep)¶ Create a new Array by interposing separater between elemens.
-
last
()¶ Get last element
-
last_opt
()¶ Get last element as Some(element), or Nothing if not exists
-
len
()¶ Get the length
-
make_string
(elem_format='{elem!r}', start='[', elem_sep=', ', end=']')¶ Make string from elements
>>> Array.range(5, 8).make_string() '[5, 6, 7]' >>> print(Array.range(5, 8).make_string(elem_sep='\n', start='', end='', elem_format='{index}: {elem}')) 0: 5 1: 6 2: 7
-
map
(action)¶ Create a new Array by applying function to each element
>>> Array.range(5, 8).map(lambda x: x * 2) Array([10, 12, 14])
Returns: Return type: Array
-
mean
()¶ Get the average of elements.
-
pluck
(key)¶ Create a new Array of values by evaluating
elem[key]
for each element.
-
pluck_attr
(attr)¶ Create a new Array of Optional values by evaluating
elem.attr
of each element. GetSome(value)
if attr exists for that element, otherwise get Nothing singleton.
-
pluck_opt
(key)¶ Create a new Array of Optional values by evaluating
elem[key]
for each element. GetSome(value)
if the key exists for that element, otherwise get Nothing singleton.
-
classmethod
range
(start, end=None, step=1)¶ Create a Array from range.
>>> Array.range(2, 10, 2).to_list() [2, 4, 6, 8] >>> Array.range(3).to_list() [0, 1, 2]
-
reverse
()¶ In place reverse this Array.
-
reversed
()¶ Create a new reversed Array.
-
second
()¶ Get second element
-
second_opt
()¶ Get second element as Some(element), or Nothing if not exists
-
sliding_window
(n)¶ Create a new Array instance that all elements are sliding windows of source elements.
-
sort
(key=None, reverse=False)¶ In place sort this Array.
-
sorted
(key=None, reverse=False)¶ Create a new sorted Array.
-
split_after
(pred)¶ Create a new Array of Arrays by splitting after each element passing predicate.
-
split_before
(pred)¶ Create a new Array of Arrays by splitting before each element passing predicate.
-
starmap
(func)¶ Create a new Array by evaluating function using argument tulpe from each element. i.e.
func(*elem)
. It’s convenient that if all elements in Array are iterable and you want to treat each element in elemnts as separate argument while calling the function.>>> Array([(1, 2), (3, 4)]).starmap(lambda a, b: a+b) Array([3, 7])
The map way. Not easy to read and write
>>> Array([(1, 2), (3, 4)]).map(lambda a_b: a_b[0]+a_b[1]) Array([3, 7])
-
sum
()¶ Get sum of elements
-
tail
()¶ Create a new Array first element dropped
-
take
(n)¶ Create a new Array of only first n element
-
take_right
(n)¶ Create a new Array with last n elements
-
take_while
(pred)¶ Create a new Array with successive elements as long as predicate evaluates to true.
-
takeright
(n)¶ Create a new Array with last n elements
-
takewhile
(pred)¶ Create a new Array with successive elements as long as predicate evaluates to true.
-
tap
(tag='', n=5, msg_format='{tag}:{index}: {elem}')¶ A debugging tool. This method create a new Array with the same elements. While creating it, it print first n elements.
>>> (Array.range(3).tap('orig') ... .map(lambda x: x * 2).tap('x2') ... .accumulate(lambda a, b: a + b) ... .tap_with(func=lambda i, e: f'{i} -> {e}') ... ) orig:0: 0 orig:1: 1 orig:2: 2 x2:0: 0 x2:1: 2 x2:2: 4 0 -> 0 1 -> 2 2 -> 6 Array([0, 2, 6])
-
tap_with
(func, n=5)¶ A debugging tool. This method create a new Array with the same elements. While creating Array, it call the function using index and element then prints the return value for first n elements.
>>> (Array.range(3).tap('orig') ... .map(lambda x: x * 2).tap('x2') ... .accumulate(lambda a, b: a + b) ... .tap_with(func=lambda i, e: f'{i} -> {e}') ... ) orig:0: 0 orig:1: 1 orig:2: 2 x2:0: 0 x2:1: 2 x2:2: 4 0 -> 0 1 -> 2 2 -> 6 Array([0, 2, 6])
Parameters: - func (
func(index, elem) -> Any
) – Function for building the printing object. - n (int) – First n element will be print.
- func (
-
to_dict
()¶ Convert to a dict
>>> Array.range(5, 10, 2).zip_index().to_dict() {5: 0, 7: 1, 9: 2}
Returns: Return type: dict
-
to_list
(copy=False)¶ Convert to a list.
>>> Array.range(3).to_list() [0, 1, 2]
-
to_map
()¶ Convert to a Map
>>> Array.range(5, 10, 2).zip_index().to_map() Map({5: 0, 7: 1, 9: 2})
Returns: Return type: Map
-
to_series
()¶ Convert to a pandas Series
>>> Array.range(5, 10, 2).to_series() 0 5 1 7 2 9 dtype: int64
Returns: Return type: pandas.Series
-
to_set
()¶ Convert to a set
>>> Array([3, 2, 3, 6, 2]).to_set() {2, 3, 6}
Returns: Return type: set
-
to_stream
()¶ Convert to a Stream
>>> strm = Array.range(5, 8, 2).zip_index().to_stream() >>> type(strm) <class 'carriage.stream.Stream'> >>> strm.to_array() Array([Row(value=5, index=0), Row(value=7, index=1)])
Returns: Return type: Stream
-
value_counts
()¶ Get a Counter instance of elements counts
-
where
(**conds)¶ Create a new Array contains only mapping pass all conditions.
-
without
(*items)¶ Create a new Array without specified elements.
-
zip
(*iterable)¶ Create a new Array by zipping elements with other iterables.
-
zip_index
(start=0)¶ Create a new Array by zipping elements with index.
-
zip_longest
(*iterables, fillvalue=None)¶ Create a new Array by zipping elements with other iterables as long as possible.
-
zip_next
(fillvalue=None)¶ Create a new Array by zipping elements with next one.
-
zip_prev
(fillvalue=None)¶ Create a new Array by zipping elements with previous one.
-