array product
This commit is contained in:
parent
c4362afda5
commit
229af2351a
2 changed files with 16 additions and 0 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
import itertools
|
||||||
from typing import List
|
from typing import List
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
|
@ -85,6 +86,17 @@ class ArrayFunctions:
|
||||||
"""
|
"""
|
||||||
return Array(list(reversed(array.value)))
|
return Array(list(reversed(array.value)))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def array_product(*arrays: Array) -> Array:
|
||||||
|
"""
|
||||||
|
Returns the Cartesian product of elements from different arrays
|
||||||
|
"""
|
||||||
|
out: List[Resolvable] = []
|
||||||
|
for combo in itertools.product(*[arr.value for arr in arrays]):
|
||||||
|
out.append(Array(combo))
|
||||||
|
|
||||||
|
return Array(out)
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,10 @@ class TestArrayFunctions:
|
||||||
output = single_variable_output("{%array_reverse(['a', 'b', 'c'])}")
|
output = single_variable_output("{%array_reverse(['a', 'b', 'c'])}")
|
||||||
assert output == ["c", "b", "a"]
|
assert output == ["c", "b", "a"]
|
||||||
|
|
||||||
|
def test_array_product(self):
|
||||||
|
output = single_variable_output("{%array_product(['a', 'b', 'c'], ['arg'])}")
|
||||||
|
assert output == [["a", "arg"], ["b", "arg"], ["c", "arg"]]
|
||||||
|
|
||||||
def test_array_apply(self):
|
def test_array_apply(self):
|
||||||
output = single_variable_output("{%array_apply(['a', 'b', 'c'], %capitalize)}")
|
output = single_variable_output("{%array_apply(['a', 'b', 'c'], %capitalize)}")
|
||||||
assert output == ["A", "B", "C"]
|
assert output == ["A", "B", "C"]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue