Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
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

A practical example – a book publishing system

Let’s examine an example that demonstrates functional programming concepts using a book publishing system scenario:

public record Book(string Title, string Author, int Year, string Content);
// Pure function to validate a book
private static bool IsValid(Book book) =>
     !string.IsNullOrEmpty(book.Title) &&
     !string.IsNullOrEmpty(book.Author) &&
     book.Year > 1900 &&
     !string.IsNullOrEmpty(book.Content);
// Pure function to format a book
private static string FormatBook(Book book) =>
     $"{book.Title} by {book.Author} ({book.Year})";
// Higher-order function for processing books
private static IEnumerable<T> ProcessBooks<R>(
     IEnumerable<Book> books,
     Func<Book, bool> validator,
     Func<Book, T> formatter) =>
     books.Where(validator).Select(formatter);
public static void Main()
{
    var books = new List<Book>
         {
              new Book("The Great Gatsby", "F. Scott Fitzgerald", 1925, "In my younger and more vulnerable years..."),
              new Book("To Kill a Mockingbird", "Harper Lee", 1960, "When he was nearly thirteen, my brother Jem..."),
              new Book("Invalid Book", "", 1800, ""),
              new Book("1984", "George Orwell", 1949, "It was a bright cold day in April, and the clocks were striking thirteen.")
         };
    // Using our higher-order function to process books
    var formattedBooks = ProcessBooks(books, IsValid, FormatBook);
    Console.WriteLine("Processed books:");
    foreach (var book in formattedBooks)
    {
         Console.WriteLine(book);
    }
}

This example demonstrates several key functional programming concepts:

  • Immutability: We use record for the Book type, which is immutable by default.
  • Pure functions: IsValid and FormatBook are pure functions. They always return the same output for the same input and have no side effects.
  • Higher-order function: ProcessBooks is a higher-order function that takes two functions as parameters (a validator and a formatter).
  • Composability: We compose the validation and formatting operations in the ProcessBooks function.
  • Declarative style: We describe what we want (valid, formatted books) rather than how to do it step by step.
  • LINQ: We use LINQ methods (Where and Select) that align well with functional programming principles.

This example shows how functional programming can be applied to a real-world scenario such as a book publishing system.

You have been reading a chapter from
Functional Programming with C#
Published in: Jul 2024
Publisher: Packt
ISBN-13: 9781805122685
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