How should I think about Scala's Product classes?

前端 未结 5 1524
情深已故
情深已故 2020-12-07 22:19

The package \"scala\" has a number of classes named Product, Product1, Product2, and so on, up to Product22.

The descriptions of these classes are surely precise. Fo

5条回答
  •  天涯浪人
    2020-12-07 23:14

    Everyone else has gone for the maths so I'll go for the silly answer just in case! You have a simple car which has a gearbox, a steering wheel, an accelerator and a number of passengers. These can each vary: which gear are you in, which way are you steering, is your foot "on the floor" etc. The gearbox, steering, accelerator etc are therefore variables and each has its own set of possible values.

    The cartesian product of each of these sets is basically all possible states that your car can be in. So a few possible values are:

    (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. hence if you car has 5 gears (+ reverse + neutral), steering is left/straight/right, accelerator is on/off and up to 4 passengers, then there are 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 by the way) has the multiplication symbol x

提交回复
热议问题