Not sure if the correct term is "distributive property" but I remember learning this in school so here's an example of what I'm trying to do:
Given:
type MyHList = (A :+: B :+: C :+: CNil) :: (Foo :+: Bar :+: CNil) :: HNil
is there any built-in type class in Shapeless that will out this:
type Out = (A, Foo) :+: (A, Bar) :+: (B, Foo) :+: (B, Bar) :+: (C, Foo) :+: (C, Bar) :+: CNil
?
Thanks
I would call such transformation cartesian, tensor or direct product (i.e. a product of each term by each term, on contrary to inner product / scalar product / zipping). Although indeed it relates to distributive law.
I guess there is no such standard type class literally but it can be expressed via standard ones
The type class
Cartesianis now acting on type level only. It's possible that on value level its definition would be a little trickier (withpoly.Case1.Aux[P, ...forP <: MapperPoly[C],poly.Case1.Aux[P, ...forP <: TuplePoly[A]rather thanpoly.Case1.Aux[MapperPoly[C], ...,poly.Case1.Aux[TuplePoly[A], ...and usingUnpack1, see Filter a HList using a supertype ). Update: Or maybe not :)Also there is always an option to define a custom type class recursively rather than try to deduce everything to standard type classes.
Here is recursive type-level implementation for multiple
HLists ofCoproducts (not necessary two)Adding value level:
I replaced mapping a coproduct with
PrependPoly[H]by a custom type classPrepend[H, C <: Coproduct]because genericPolyare tricky and not everything can be done with them on value level.issue #198: Injecting values to a Poly defined outside of calling method is awkward
issue #154: Improve support for partial application of Polys
Passing an extra argument into a polymorphic function?
Pick out the Nth element of a HList of Lists and return that value as a HList of values
Dynamically parametrize Poly1 function in shapeless
shapeless-dev: How to "parameterize" poly function?
HList folding function that requires the HList
Parameterise filtering of element in of shapeless Hlist of Lists
See also:
Taking HList of Seq[_] and generating Seq[HList] with cartesian product of values
Cartesian product of heterogeneous lists (Haskell)