Takes product of elements in sequence. >>> seq([1, 2, 3, 4]).product() 24 >>> seq([]).product() 1 >>> seq([(1, 2), (1, 3), (1, 4)]).product(lambda x: x[0]) 1 :param projection: function to project on the sequence before taking the
(self, projection=None)
| 973 | return separator.join(str(e) for e in self) |
| 974 | |
| 975 | def product(self, projection=None): |
| 976 | """ |
| 977 | Takes product of elements in sequence. |
| 978 | |
| 979 | >>> seq([1, 2, 3, 4]).product() |
| 980 | 24 |
| 981 | |
| 982 | >>> seq([]).product() |
| 983 | 1 |
| 984 | |
| 985 | >>> seq([(1, 2), (1, 3), (1, 4)]).product(lambda x: x[0]) |
| 986 | 1 |
| 987 | |
| 988 | :param projection: function to project on the sequence before taking the product |
| 989 | :return: product of elements in sequence |
| 990 | """ |
| 991 | if self.empty(): |
| 992 | if projection: |
| 993 | return projection(1) |
| 994 | else: |
| 995 | return 1 |
| 996 | if self.size() == 1: |
| 997 | if projection: |
| 998 | return projection(self.first()) |
| 999 | else: |
| 1000 | return self.first() |
| 1001 | |
| 1002 | if projection: |
| 1003 | return self.map(projection).reduce(mul) |
| 1004 | else: |
| 1005 | return self.reduce(mul) |
| 1006 | |
| 1007 | def sum(self, projection=None): |
| 1008 | """ |