From the course: Advanced C#: Functional Programming Patterns

Unlock this course with a free trial

Join today to access over 24,500 courses taught by industry experts.

Calculate the total in a pure function

Calculate the total in a pure function - C# Tutorial

From the course: Advanced C#: Functional Programming Patterns

Calculate the total in a pure function

At this point in the chapter, we have the GetRobots method that is working with the XML file and returning an ImmutableList< Robot >. The next step is to create a pure function that calculates the total weight of all the robots that are in the competition. So that's going to be this method here. I'm going to write this as a pure function. It's called GetTotalWeight. And I'm calling it here on line 24 in the DoWork() method where I pass in this ImmutableList< Robots >. A pure function, remember, always returns the same output value given the same input parameters. So if I pass in an immutable list of 12 robots, which is what I have in this demo, and they all have the same weights as the 12 robots here, I should get back the same int value every time. So let's verify that the code in here is pure. It doesn't have any side effects, it's not using I/O, and it's not working with any data that's not passed in explicitly as a parameter. In line 32, looks good. I'm declaring a variable there.…

Contents