Skip to Content
Functional Programming in C#: Classic Programming Techniques for Modern Projects
book

Functional Programming in C#: Classic Programming Techniques for Modern Projects

by Oliver Sturm
June 2011
Intermediate to advanced
288 pages
7h 54m
English
Wiley
Content preview from Functional Programming in C#: Classic Programming Techniques for Modern Projects

APPLYING OPERATIONS: MAP

The Map function is quite simple — but then that’s the nature of the standard higher order functions; that’s what makes them the great building blocks they are in functional programming. Map takes a list of elements and a function to call with each element in turn. Then it constructs a new list from the results of the function calls and returns that new list.

Using iterators in C#, Map can be implemented lazily. Here is the implementation from FCSlib:

static IEnumerable<R> Map<T, R>(Converter<T, R> function, IEnumerable<T> list)

{

  foreach (T sourceVal in list)

    yield return function(sourceVal);

}

The Converter<T,R> delegate type is a function that receives a parameter of type T and returns an element of type R. The name Converter can be a bit misleading, since the function doesn’t necessarily convert anything. It might just as well extract something, which is a major use case of the Map function. For example, given a list of objects, Map can be used to extract a particular property from each of the objects:

var people = new List<Person> {

  new Person {Name = "Harry", Age = 32},

  new Person {Name = "Anna", Age = 45},

  new Person {Name = "Willy", Age = 43},

  new Person {Name = "Rose", Age = 37}

};

var names = Functional.Map(p => p.Name, people);

Of course, actual calculations can be performed just as easily:

var squares = Functional.Map(i => i * i, Enumerable.Range(1, 10));

Using Criteria: Filter

Filter is a function that applies criteria to ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Functional Programming in C#

Functional Programming in C#

Enrico Buonanno

Publisher Resources

ISBN: 9780470971109Purchase bookDownloads