How can I think of Scala product classes?

There are several classes in the "scala" package with the name Product, Product1, Product2, etc., up to Product22.

Descriptions of these classes are certainly accurate. For example:

Product4 is a cartesian product of 4 components 

Exact, yes. Communicative? Not so much. I expect this to be the perfect formulation for those who already understand the meaning of the "Cartesian product" used here. For someone who doesn't, this sounds a little circular. "Oh yes, well, of course, Product4 is a mumble of 4 mumbles."

Please help me understand the correct point of view in a functional language. What is the point of using a "Cartesian product"? What do projection members mean "projection classes"?

+52
scala functional-programming
Aug 19 '09 at 18:48
source share
4 answers

The set of all possible pairs of elements whose components are members of two sets.

"In particular, the Cartesian product of two sets X (for example, points on the x-axis) and Y (for example, points on the y-axis), denoted by X Γ— Y, is the set of all possible ordered pairs, the first component of which is a member of X, and the second the component is a member of Y (for example, the entire xy plane) "

Perhaps a better understanding can be achieved by knowing who arises from it:

Direct Known Subclasses: Tuple4

Or, knowing that this "extends the product", be aware that other classes can use it, by virtue of the Product extension. I will not bring it here because it is quite long.

In any case, if you have types A, B, C, and D, then Product4 [A, B, C, D] is a class whose instances are all possible elements of the Cartesian product A, B, C, and D. Literally .

Also, of course, that Product4 is a sign, not a class. It simply provides some useful methods for classes that are Cartesian products from four different sets.

+25
Aug 19 '09 at 19:03
source share

Everyone else went to math, so I'll go for a stupid answer just in case! You have a simple car with a gearbox, steering wheel, accelerator and several passengers. Each of them can vary: what mechanism you use, how you control, your foot β€œon the floor”, etc. Therefore gearbox, steering, accelerator, etc. They are variables, and each of them has its own set of possible values.

The Cartesian product of each of these sets is basically all the possible conditions your car might be in. Thus, several possible values:

 (gear, steer, accel, pssngers) --------|---------|----------|--------- (1st, left, foot down, none) (neutral, straight, off, the kids) 

the size of the Cartesian product is, of course, the product (multiplication) of the possibilities of each set. therefore, if you have 5 gears (+ reverse + neutral), steering left / right / right, the accelerator is on / off and up to 4 passengers, that is, 7 x 3 x 2 x 4 or 168 possible states.

This last fact is the reason that the Cartesian product (named after Rene Descartes ) has a multiplication symbol x

+35
Aug 19 '09 at 22:13
source share

From this thread :

From mathematics, the Cartesian product of two sets A, B is denoted as AxB , and its elements are (a, b) , where a in and b from B.

For three sets of elements (Cartesian) products (a, b, c) , etc ...

So, you have tuples of elements, and indeed, you can see in the Scala library that all tuples (for example, Tuple1 ) inherit the corresponding product attribute (for example, Product1 ).

Think of the product as an abstraction, and the corresponding set is a concrete representation .

Projection allows you to get an instance of the class 'n' that the Product refers to.

+7
Aug 19 '09 at 19:02
source share

A Cartesian product is a product of sets. For given sets A and BA, x B ("A cross B") is the set of all tuples (x, y) such that x is in A and y is in B. The Cartesian product can be similarly defined on types: types A are given and B, A x B is the type of tuples (x, y), where x is of type A and y is of type B.

So Product4 is the type of tuples (w, x, y, z), where w, x, y, z are the components.

+4
Aug 19 '09 at 19:04
source share



All Articles