0
$\begingroup$

Let's say I have a struct Product with an unknown and variable amount of attributes-values. The attributes-values come from a user at runtime. One product with a unique combination of attributes and values form a product_variant.

For intance, user may create 4 attributes:

  • size (5 values)
  • material (3 values)
  • design (2 values)
  • colour (9 values)

and thus there'll be 5 * 3 * 2 * 9 = 270 unique product variants.

Example:

product_variant with:

%{ 
  size => m,
  material => paper,
  design => ds_abc,
  colour => red,
}

How do I traverse all the attributes with their respective values and thus create 270 unique product variants? What'll be an algorithm?


The number of attributes may be any and isn't known at compile time. There're 4 here, but there can be any number and they, amount and names, aren't known up front - they get resolved at runtime.

Not with a library one-line function.

$\endgroup$
3
  • 1
    $\begingroup$ Requests for code are off-topic here. $\endgroup$
    – D.W.
    Jun 14, 2021 at 5:48
  • $\begingroup$ And your number of product variants is off if not all attributes need to be present. $\endgroup$
    – gnasher729
    Jun 17, 2021 at 11:17
  • $\begingroup$ @gnasher729 I don't undestand your indian english $\endgroup$
    – turinaki
    Jun 20, 2021 at 6:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.