diff --git a/docs/for-each.md b/docs/for-each.md new file mode 100644 index 0000000..c6de0df --- /dev/null +++ b/docs/for-each.md @@ -0,0 +1,34 @@ +## for_each + +`for_each(n)` + +Executes a given function per each element in an `Enumerable` collection. Syntactic sugar for a for loop. This is an executing function. + +**Parameters** + +__func__ : the function, lambda or otherwise, to execute per each element + +**Returns** + +None + +**Example** + +

+from py_linq import Enumerable
+
+Enumerable([1 ,2 ,3]).for_each(print)
+# 1
+# 2
+# 3
+
+Enumerable([
+    {'value': 1},
+    {'value': 2},
+    {'value': 3}
+]).for_each(lambda x: print(x['value']))
+# 1
+# 2
+# 3
+
+
\ No newline at end of file diff --git a/docs/index-of.md b/docs/index-of.md new file mode 100644 index 0000000..b816a2a --- /dev/null +++ b/docs/index-of.md @@ -0,0 +1,33 @@ +## index_of + +`index_of(n)` + +Returns the index of a given element in an `Enumerable` collection. If no matching element is found, None is returned. This is an executing function. + +**Parameters** + +__element__ : the element to search for and return the index of within the collection + +**Returns** + +The index of the given element, or None. + +**Example** + +

+from py_linq import Enumerable
+
+Enumerable([
+    {'value': 1},
+    {'value': 2},
+    {'value': 3}
+]).index_of({'value': 2})
+# 1
+
+Enumerable([
+    {'value': 1},
+    {'value': 2},
+    {'value': 3}
+]).index_of({'value': 4})
+# None
+
\ No newline at end of file diff --git a/docs/index.md b/docs/index.md index 384f1e0..5056d4d 100644 --- a/docs/index.md +++ b/docs/index.md @@ -42,38 +42,41 @@ Once you have created an `Enumerable` instance, the LINQ methods will become ava 11. [intersect](/py-enumerable/intersect) 12. [except_](/py-enumerable/except) 13. [to_list](/py-enumerable/to-list) -14. [count](/py-enumerable/count) -15. [sum](/py-enumerable/sum) -16. [min](/py-enumerable/min) -17. [max](/py-enumerable/max) -18. [avg](/py-enumerable/avg) -19. [median](/py-enumerable/median) -20. [any](/py-enumerable/any) -21. [element_at](/py-enumerable/element-at) -22. [element_at_or_default](/py-enumerable/element-at-or-default) -23. [first](/py-enumerable/first) -24. [first_or_default](/py-enumerable/first-or-default) -25. [last](/py-enumerable/last) -26. [last_or_default](/py-enumerable/last-or-default) -27. [contains](/py-enumerable/contains) -28. [group_by](/py-enumerable/group-by) -29. [distinct](/py-enumerable/distinct) -30. [group_join](/py-enumerable/group-join) -31. [union](/py-enumerable/union) -32. [all](/py-enumerable/all) -33. [aggregate](/py-enumerable/aggregate) -34. [append](/py-enumerable/append) -35. [prepend](/py-enumerable/prepend) -36. [empty](/py-enumerable/empty) -37. [range](/py-enumerable/range) -38. [repeat](/py-enumerable/repeat) -39. [reverse](/py-enumerable/reverse) -40. [skip_last](/py-enumerable/skip_last) -41. [skip_while](/py-enumerable/skip_while) -42. [take_last](/py-enumerable/take_last) -43. [take_while](/py-enumerable/take_while) -44. [zip](/py-enumerable/zip) -45. [default_if_empty](/py-enumerable/default_if_empty) -46. [single](/py-enumerable/single) -47. [single_or_default](/py-enumerable/single-or-default) -48. [to_dictionary](/py-enumerable/to-dictionary) \ No newline at end of file +14. [for_each](/py-enumerable/for-each) +15. [count](/py-enumerable/count) +16. [sum](/py-enumerable/sum) +17. [min](/py-enumerable/min) +18. [max](/py-enumerable/max) +19. [avg](/py-enumerable/avg) +20. [median](/py-enumerable/median) +21. [any](/py-enumerable/any) +22. [index_of](/py-enumerable/index-of) +23. [last_index_of](/py-enumerable/last-index-of) +24. [element_at](/py-enumerable/element-at) +25. [element_at_or_default](/py-enumerable/element-at-or-default) +26. [first](/py-enumerable/first) +27. [first_or_default](/py-enumerable/first-or-default) +28. [last](/py-enumerable/last) +29. [last_or_default](/py-enumerable/last-or-default) +30. [contains](/py-enumerable/contains) +31. [group_by](/py-enumerable/group-by) +32. [distinct](/py-enumerable/distinct) +33. [group_join](/py-enumerable/group-join) +34. [union](/py-enumerable/union) +35. [all](/py-enumerable/all) +36. [aggregate](/py-enumerable/aggregate) +37. [append](/py-enumerable/append) +38. [prepend](/py-enumerable/prepend) +39. [empty](/py-enumerable/empty) +40. [range](/py-enumerable/range) +40. [repeat](/py-enumerable/repeat) +41. [reverse](/py-enumerable/reverse) +42. [skip_last](/py-enumerable/skip_last) +43. [skip_while](/py-enumerable/skip_while) +44. [take_last](/py-enumerable/take_last) +45. [take_while](/py-enumerable/take_while) +46. [zip](/py-enumerable/zip) +47. [default_if_empty](/py-enumerable/default_if_empty) +48. [single](/py-enumerable/single) +49. [single_or_default](/py-enumerable/single-or-default) +50. [to_dictionary](/py-enumerable/to-dictionary) \ No newline at end of file diff --git a/docs/last-index-of.md b/docs/last-index-of.md new file mode 100644 index 0000000..bb0edb2 --- /dev/null +++ b/docs/last-index-of.md @@ -0,0 +1,36 @@ +## last_index_of + +`last_index_of(n)` + +Returns the index of the last occurrence of a given element in an `Enumerable` collection. If no matching element is found, None is returned. This is an executing function. + +**Parameters** + +__element__ : the element to search for and return the last index of within the collection + +**Returns** + +The last index of the given element, or None. + +**Example** + +

+from py_linq import Enumerable
+
+Enumerable([
+    {'value': 1},
+    {'value': 2},
+    {'value': 3},
+    {'value': 1},
+    {'value': 2},
+    {'value': 3}
+]).last_index_of({'value': 2})
+# 4
+
+Enumerable([
+    {'value': 1},
+    {'value': 2},
+    {'value': 3}
+]).last_index_of({'value': 4})
+# None
+
\ No newline at end of file diff --git a/py_linq/py_linq.py b/py_linq/py_linq.py index d7b5e1e..a92d9e3 100644 --- a/py_linq/py_linq.py +++ b/py_linq/py_linq.py @@ -106,6 +106,15 @@ def select(self, func=lambda x: x) -> TEnumerable: """ return Enumerable(map(func, self)) + def for_each(self, func=lambda x: x): + """ + Execute a given function per each element + :param func: the function to execute + :return: None + """ + for x in self: + func(x) + def sum(self, func=lambda x: x) -> Number: """ Returns the sum of af data elements @@ -161,6 +170,33 @@ def median(self, func=lambda x: x) -> Number: else (float(result[i - 1]) + float(result[i])) / float(2) ) + def index_of(self, element): + """ + Returns the index of the first occurrence of a given element. + + :param element: the element for which to retrieve the index + :return: Index of given element + """ + for i, e in enumerate(self): + if e == element: + return i + + return None + + def last_index_of(self, element): + """ + Returns the index of the last occurrence of a given element. + + :param element: the element for which to retrieve the last index + :return: Index of last occurence of given element + """ + last_index = self.count() - 1 + for i, e in enumerate(self.reverse()): + if e == element: + return last_index - i + + return None + def element_at(self, n) -> Any: """ Returns element at given index.