From the course: Advanced C#: Functional Programming Patterns

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Calculate the total in pure function

Calculate the total in pure function - C# Tutorial

From the course: Advanced C#: Functional Programming Patterns

Calculate the total in pure function

- At this point in the chapter, we have the GetRobots method that is working with the XML file and returning it an immutable list 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 its called get total weight and I'm calling it here on line 24 and the D work method where I pass in this immutable list of robots. A pure function remember, is supposed to return the same output if it takes the exact same input values. 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 IO and it's not working with any data that's not passed in…

Contents