Repository: ivy-llc/ivy

Issue:
Extract `Duplicate Code` into Helper Function in `ivy\func_wrapper.py`
I identified duplicate sections of code in the following functions:
https://github.com/unifyai/ivy/blob/2bfb19a2ca3b277e95cba987353aa349ef28015e/ivy/func_wrapper.py#L94
https://github.com/unifyai/ivy/blob/2bfb19a2ca3b277e95cba987353aa349ef28015e/ivy/func_wrapper.py#L137

I think it will be good if we can extract these into their own method/function to increase the readability and decrease the amount of duplicate/repeating code in the file. This can simplify the code and reduce redundancy.


Discussion:

Resolving patch:
diff --git a/ivy/func_wrapper.py b/ivy/func_wrapper.py
--- a/ivy/func_wrapper.py
+++ b/ivy/func_wrapper.py
@@ -91,91 +91,41 @@ def caster(dtype, intersect):
                 return ret_dtype
 
 
+def cast_helper(arg, dtype, intersect, is_upcast=True):
+    step = 1 if is_upcast else -1
+    index = casting_modes_dict[arg]().index(dtype) + step
+    result = ""
+    while 0 <= index < len(casting_modes_dict[arg]()):
+        if casting_modes_dict[arg]()[index] not in intersect:
+            result = casting_modes_dict[arg]()[index]
+            break
+        index += step
+
+    return result
+
+
 def upcaster(dtype, intersect):
     # upcasting is enabled, we upcast to the highest
     if "uint" in str(dtype):
-        index = casting_modes_dict["uint"]().index(dtype) + 1
-        result = ""
-        while index < len(casting_modes_dict["uint"]()):
-            if casting_modes_dict["uint"]()[index] not in intersect:
-                result = casting_modes_dict["uint"]()[index]
-                break
-            index += 1
-        return result
-
+        return cast_helper("uint", dtype, intersect, is_upcast=True)
     if "int" in dtype:
-        index = casting_modes_dict["int"]().index(dtype) + 1
-        result = ""
-        while index < len(casting_modes_dict["int"]()):
-            if casting_modes_dict["int"]()[index] not in intersect:
-                result = casting_modes_dict["int"]()[index]
-                break
-            index += 1
-        return result
-
+        return cast_helper("int", dtype, intersect, is_upcast=True)
     if "float" in dtype:
-        index = casting_modes_dict["float"]().index(dtype) + 1
-        result = ""
-        while index < len(casting_modes_dict["float"]()):
-            if casting_modes_dict["float"]()[index] not in intersect:
-                result = casting_modes_dict["float"]()[index]
-                break
-            index += 1
-        return result
-
+        return cast_helper("float", dtype, intersect, is_upcast=True)
     if "complex" in dtype:
-        index = casting_modes_dict["complex"]().index(dtype) + 1
-        result = ""
-        while index < len(casting_modes_dict["complex"]()):
-            if casting_modes_dict["complex"]()[index] not in intersect:
-                result = casting_modes_dict["complex"]()[index]
-                break
-            index += 1
-        return result
+        return cast_helper("complex", dtype, intersect, is_upcast=True)
 
 
 def downcaster(dtype, intersect):
     # downcasting is enabled, we upcast to the highest
     if "uint" in str(dtype):
-        index = casting_modes_dict["uint"]().index(dtype) - 1
-        result = ""
-        while index >= 0:
-            if casting_modes_dict["int"]()[index] not in intersect:
-                result = casting_modes_dict["uint"]()[index]
-                break
-            index -= 1
-        return result
-
+        return cast_helper("uint", dtype, intersect, is_upcast=False)
     if "int" in dtype:
-        index = casting_modes_dict["int"]().index(dtype) - 1
-        result = ""
-        while index >= 0:
-            if casting_modes_dict["int"]()[index] not in intersect:
-                result = casting_modes_dict["int"]()[index]
-                break
-            index -= 1
-        return result
-
+        return cast_helper("int", dtype, intersect, is_upcast=False)
     if "float" in dtype:
-        index = casting_modes_dict["float"]().index(dtype) - 1
-
-        result = ""
-        while index >= 0:
-            if casting_modes_dict["float"]()[index] not in intersect:
-                result = casting_modes_dict["float"]()[index]
-                break
-            index -= 1
-        return result
-
+        return cast_helper("float", dtype, intersect, is_upcast=False)
     if "complex" in dtype:
-        index = casting_modes_dict["complex"]().index(dtype) - 1
-        result = ""
-        while index >= 0:
-            if casting_modes_dict["complex"]()[index] not in intersect:
-                result = casting_modes_dict["complex"]()[index]
-                break
-            index -= 1
-        return result
+        return cast_helper("complex", dtype, intersect, is_upcast=False)
 
 
 def cross_caster(intersect):


Test patch: