MCPcopy Create free account
hub / github.com/EntilZha/PyFunctional / aggregate

Method aggregate

functional/pipeline.py:1043–1080  ·  view source on GitHub ↗

Aggregates the sequence by specified arguments. Its behavior varies depending on if one, two, or three arguments are passed. Assuming the type of the sequence is A: One Argument: argument specifies a function of the type f(current: B, next: A => result: B. current r

(self, *args)

Source from the content-addressed store, hash-verified

1041 return sum(self) / length
1042
1043 def aggregate(self, *args):
1044 """
1045 Aggregates the sequence by specified arguments. Its behavior varies depending on if one,
1046 two, or three arguments are passed. Assuming the type of the sequence is A:
1047
1048 One Argument: argument specifies a function of the type f(current: B, next: A => result: B.
1049 current represents results computed so far, and next is the next element to aggregate into
1050 current in order to return result.
1051
1052 Two Argument: the first argument is the seed value for the aggregation. The second argument
1053 is the same as for the one argument case.
1054
1055 Three Argument: the first two arguments are the same as for one and two argument calls. The
1056 additional third parameter is a function applied to the result of the aggregation before
1057 returning the value.
1058
1059 :param args: options for how to execute the aggregation
1060 :return: aggregated value
1061 """
1062 seed = None
1063 result_lambda = identity
1064 if len(args) == 1:
1065 func = args[0]
1066 elif len(args) == 2:
1067 seed = args[0]
1068 func = args[1]
1069 elif len(args) == 3:
1070 seed = args[0]
1071 func = args[1]
1072 result_lambda = args[2]
1073 else:
1074 raise ValueError(
1075 "aggregate takes 1-3 arguments, {0} were given".format(len(args))
1076 )
1077 if len(args) == 1:
1078 return result_lambda(self.drop(1).fold_left(self.first(), func))
1079 else:
1080 return result_lambda(self.fold_left(seed, func))
1081
1082 def fold_left(self, zero_value, func):
1083 """

Callers 1

test_aggregateMethod · 0.80

Calls 3

fold_leftMethod · 0.95
dropMethod · 0.95
firstMethod · 0.95

Tested by 1

test_aggregateMethod · 0.64