Shoftim Perek 16
At the end of perek 16 Shimshon asks Hashem to give him the merit of one of his eyes, which had been knocked out by the Plishtim, in order to give him the strength to bring down the roof of the party. How could he ask for reward for his eyes having been poked out - that was a punishment for being swayed by the physical beauty of Delilah? Why is he asking reward for a punishment?
The answer is a fundamental way to view the goal of a punishment. Punishments are not all there to 'smack us’ vengefully for doing bad - they are means for us to grow, correct our shortfalls, and become better than we were before the sin. Thus, the punishment matches the sin perfectly. This is how Shimshon could receive reward for punishment; because he was receiving reward for having grown spiritually from the punishment. He was asking for reward for having used the punishment appropriately to learn and grow spiritually. This concept is also seen in Rashi (Vayikra 14:34). Rashi cites a Midrash that says that Hashem guaranteed Bnei Yisrael that they would have to break down their houses (because of tzara'as- leprosy of the house form) and would find treasure underneath it. But why are we getting a reward for the tzara'as which was caused by sin in the first place? Again, since punishment is ultimately to make us better people, we can now understand that we are not getting reward for having sinned to cause tzara’as, but rather for having used the punishment of tzara’as to grow and learn.
The same is true of all tests in life - they take on a new perspective when one internalises that they are planned exercises to better ourselves and develop into fuller, more mature and loftier people.