Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Functional Programming with C#

You're reading from   Functional Programming with C# Unlock coding brilliance with the power of functional magic

Arrow left icon
Product type Paperback
Published in Jul 2024
Publisher Packt
ISBN-13 9781805122685
Length 258 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Alex Yagur Alex Yagur
Author Profile Icon Alex Yagur
Alex Yagur
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. Part 1:Foundations of Functional Programming in C#
2. Chapter 1: Getting Started with Functional Programming FREE CHAPTER 3. Chapter 2: Expressions and Statements 4. Chapter 3: Pure Functions and Side Effects 5. Chapter 4: Honest Functions, Null, and Option 6. Part 2:Advanced Functional Techniques
7. Chapter 5: Error Handling 8. Chapter 6: Higher-Order Functions and Delegates 9. Chapter 7: Functors and Monads 10. Part 3:Practical Functional Programming
11. Chapter 8: Recursion and Tail Calls 12. Chapter 9: Currying and Partial Application 13. Chapter 10: Pipelines and Composition 14. Part 4:Conclusion and Future Directions
15. Chapter 11: Reflecting and Looking Ahead 16. Index 17. Other Books You May Enjoy

Building pipelines

Before constructing pipelines, let’s briefly recap two key concepts from the previous chapter: currying and partial application. These techniques are fundamental to creating flexible, reusable function components that serve as excellent pipeline building blocks.

Currying, as we learned, transforms a function that takes multiple arguments into a sequence of functions, each accepting a single argument. Here’s an example:

Func<int, int, int> add = (a, b) => a + b;
Func<int, Func<int, int>> curriedAdd = a => b => a + b;

Partial application, on the other hand, involves fixing a number of arguments to a function, producing another function with fewer parameters:

Func<int, int, int> multiply = (a, b) => a * b;
Func<int, int> triple = x => multiply(3, x);

These concepts naturally lead on to pipeline construction. By currying functions or partially applying them, we create specialized, single-purpose functions that can be easily composed into pipelines. This approach allows us to do the following:

  • Break down complex operations into simpler, more manageable pieces
  • Reuse these pieces across different pipelines or contexts
  • Create more expressive and readable code by chaining these specialized functions

For instance, consider a pipeline for processing game data:

var processGameData =
     LoadData()
     .Then(ValidateData)
     .Then(TransformData)
     .Then(SaveData);

Each step in this pipeline could be a curried or partially applied function, allowing for easy customization and reuse. As we explore pipeline construction further, remember how currying and partial application can be leveraged to create more flexible and powerful pipelines.

Now, let’s move on to building pipelines.

Pipelines process data through a sequence of processing steps, each represented by a function. This approach is particularly useful for tasks that require multiple transformations, validations, or computations. You most probably have already encountered pipelines while using LINQ to manipulate collections.

Let’s consider a real-world scenario: an Extract, Transform, Load (ETL) process for publishing manuscripts. This process involves several steps:

  1. Extracting (querying) the manuscript from a database
  2. Validating its content
  3. Transforming it into the required format
  4. Loading (submitting) it for publication

Each step can be represented as a function, and we can use a pipeline to streamline this process. To do this, let’s create a method that applies a sequence of functions to an initial value, passing the result of each function to the next, and name it Pipe:

public static T Pipe<T>(this T source, params Func<T, T>[] funcs)
{
     return funcs.Aggregate(source, (current, func) => func(current));
}

Let’s consider book manuscript processing: querying the manuscript from a database, validating its content, transforming it into the required format, and finally submitting it for publication:

public class Manuscript
{
     public string Content { get; set; }
     public bool IsValid { get; set; }
     public string FormattedContent { get; set; }
}
public Manuscript Query(Manuscript manuscript)
{
     // Simulate querying the manuscript from a database
     manuscript.Content = "Original manuscript content.";
     return manuscript;
}
public Manuscript Validate(Manuscript manuscript)
{
     // Simulate validating the manuscript
     manuscript.IsValid = !string.IsNullOrWhiteSpace(manuscript.Content);
     return manuscript;
}
public Manuscript Transform(Manuscript manuscript)
{
     // Simulate transforming the manuscript content
     if (manuscript.IsValid)
     {
         manuscript.FormattedContent = manuscript.Content.ToUpper();
     }
     return manuscript;
}
public Manuscript Submit(Manuscript manuscript)
{
     // Simulate submitting the manuscript for publication
     if (manuscript.IsValid)
     {
         Console.WriteLine($"Manuscript submitted: {manuscript.FormattedContent}");
     }
     else
     {
         Console.WriteLine("Manuscript validation failed. Submission aborted.");
     }
     return manuscript;
}

Here’s how we might execute this flow without using the Pipe method:

public void ExecutePublishingFlow(Manuscript manuscript)
{
     manuscript = Submit(
         Transform(
             Validate(
                 Query(
                     manuscript))));
}

Now, using the Pipe method, our code becomes 10 times better:

public void ExecutePublishingFlow(Manuscript manuscript)
{
     manuscript
         .Pipe(Query)
         .Pipe(Validate)
         .Pipe(Transform)
         .Pipe(Submit);
}

Of course, it adds a bit of overhead, and the program might work noticeably more slowly, but it’s so much easier and faster to read!

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image