general_add

hybrid_learning.datasets.transforms.common.general_add(first, second, *, composition_class, identity_class=None, type_check=<built-in function callable>)[source]

Return a flat, associative composition of first and second. Elements of identity_class and None are treated as neutral element, and all summands must either pass the type_check or be neutral. In case both summands are non-neutral, a composition_class instance of both is returned. If any already is of type composition_class, this one is unpacked to ensure flatness of the returned composition. It is worked on copies of the summands if these provide a __copy__ method. The composition_class must be iterable and accept as init argument a sequence of the summands.

Note

This operation is in general not commutative. Associativity is ensured via the flatness.

Parameters
  • first – summand

  • second – summand

  • composition_class – the class to use for generating the flat composition

  • identity_class (Optional[Union[type, List[type]]]) – optionally a class that is treated as neutral element

  • type_check (Callable[[Any], bool]) – a callable that accepts any of first or second and returns a bool whether they have an appropriate type or not

Returns

one of the summands in case the other is neutral, else a composition_class instance; NotImplemented is returned if any non-None summand doesn’t pass the type_check