array enumerate

This commit is contained in:
Jesse Bannon 2023-11-21 17:03:55 -08:00
parent 6c32c44680
commit 7e2ecac38c
3 changed files with 15 additions and 6 deletions

View file

@ -50,6 +50,16 @@ class ArrayFunctions:
@staticmethod
def array_apply(array: Array, lambda_function: Lambda) -> Array:
"""
Reverse an Array.
Apply a lambda function on every element in the Array.
"""
return ResolvedArray([ResolvedArray([val]) for val in array.value])
@staticmethod
def array_enumerate(array: Array, lambda_function: Lambda) -> Array:
"""
Apply a lambda function on every element in the Array, where each arg
passed to the lambda function is ``idx, element`` as two separate args.
"""
return ResolvedArray(
[ResolvedArray([Integer(idx), val]) for idx, val in enumerate(array.value)]
)

View file

@ -28,7 +28,8 @@ from ytdl_sub.script.types.variable import FunctionArgument
from ytdl_sub.script.types.variable import Variable
from ytdl_sub.script.types.variable_dependency import VariableDependency
from ytdl_sub.script.utils.exception_formatters import FunctionArgumentsExceptionFormatter
from ytdl_sub.script.utils.exceptions import FunctionDoesNotExist, UNREACHABLE
from ytdl_sub.script.utils.exceptions import UNREACHABLE
from ytdl_sub.script.utils.exceptions import FunctionDoesNotExist
from ytdl_sub.script.utils.exceptions import FunctionRuntimeException
from ytdl_sub.script.utils.exceptions import UserThrownRuntimeError
from ytdl_sub.script.utils.type_checking import FunctionInputSpec

View file

@ -127,8 +127,6 @@ class TestFunction:
{
"%times_three": "{%mul($0, 3)}",
"%times_two": "{%mul($0, 2)}",
"wip": "{%array_apply([1, 2, 3], %if(False, %times_two, %times_three))}"
"wip": "{%array_apply([1, 2, 3], %if(False, %times_two, %times_three))}",
}
).resolve() == {
"wip": ResolvedArray([Integer(3), Integer(6), Integer(9)])
}
).resolve() == {"wip": ResolvedArray([Integer(3), Integer(6), Integer(9)])}