From the course: Advanced C#: Functional Programming Patterns

Unlock this course with a free trial

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

Higher-order: transform example

Higher-order: transform example

Let's see how to create our own Transform() method that has a delegate parameter. This is the equivalent of mapping in functional programming, where you take a list, in my case, it's the numbers from 1 to 120, and I'm going to perform some transformation on each of the items in the list. So again, I'm using a lambda expression here. This is the transformation. I'm going to take a value of x, and I'm going to bring back x * x * x or x cubed. Here is the code. It is in this section. It's very similar to what we saw in the last video. It's an extension method that extends IEnumerable< T >. It returns an IEnumerable< T >. It has a func here that takes in a value and returns a value. I call this transformer. And then I'm going to run the code here to modify each item as it's looped over in this foreach. So I'm saying foreach every item that's passed in here and then transform, that's my function I created, this item and return that value. And there's the results: 1, 8, 27, 64, and so on.

Contents