This chapter introduces practical ways to structure an application around functions rather than objects, showing how a complex system can be broken into small, reusable pieces that are easier to compose. It begins with the idea that general functions can be made more specific by supplying arguments in stages, which helps separate concerns across different parts of the application lifecycle. The discussion uses simple examples first, then builds toward real-world scenarios where configuration is supplied at startup and runtime data arrives later.
A central theme is partial application: taking a multi-argument function and fixing some of its inputs so the result is a new, more specialized function. The chapter contrasts a manual approach with a general helper that can partially apply binary and ternary functions, and then explains currying as the related transformation of an n-ary function into a chain of unary functions. It also notes that argument order matters, since placing general, early-bound parameters first makes partial application more natural, though C# method type inference can make multi-argument methods awkward to use in higher-order functions.
The later sections show how these ideas support real application design, especially data access, validation, and web endpoints. By wrapping configuration values in expressive types and defining adapter functions that accept dependencies as arguments, the chapter demonstrates how to create APIs that are friendly to partial application and easier to test. It compares object-oriented modularity based on interfaces and dependency injection with a functional style that injects functions directly, then closes by showing how lists of validators can be reduced into a single validator using aggregation, either by failing fast or by collecting all errors.
Fred needs several pieces of information before he can provide the product. This information can be given at different points by Ada and Bruno.
Comparing normal function application with partial application. Partial application allows you to supply arguments piecemeal to obtain functions that have those arguments built-inand are awaiting the following arguments. Partial application can be enabled manually or by using the general Apply function.
In OO design, higher-level components, like Controller, consume lower-level components, like Repository, via interfaces.
Reducing a list to a single value with Aggregate
Summary
- Partial application means giving a function its arguments piecemeal, effectively creating a more specialized function with each argument given.
- Currying means changing the signature of a function so that it will take its arguments one at a time.
- Partial application enables you to write highly general functions by parameterizing their behavior and then supplying arguments to obtain increasingly specialized functions.
- The order of arguments matters: you give the leftmost argument first so that a function should declare its arguments from general to specific.
- When working with multi-argument functions in C#, method resolution can be problematic and lead to syntactic overhead. This can be overcome by relying on Funcs rather than on methods.
- You can inject the dependencies required by your functions by declaring them as arguments. This allows you to compose your application entirely of functions without compromising on the separation of concerns, decoupling, and testability.
FAQ
```htmlWhat is partial application in chapter 9?
Partial application is the technique of supplying a function’s arguments piecemeal. You provide some arguments now and get back a more specialized function that remembers those values and waits for the remaining arguments.How does partial application help with separation of concerns?
It lets different parts of an application supply different arguments at different times. For example, configuration values can be applied at startup, while request-specific values can be supplied later, keeping responsibilities separate.What is the difference between greet and greetWith?
greet takes both arguments at once: (Greeting, Name) → PersonalizedGreeting. greetWith is curried: it takes a greeting first and returns a function that accepts a name later, with signature Greeting → Name → PersonalizedGreeting.What does it mean for a function to be in curried form?
A curried function takes one argument at a time, returning a new function after each argument until the final result is produced. In arrow notation, this is written asT1 → T2 → ... → R.What does the generic Apply function do?
Apply partially applies a binary function by fixing its first argument and returning a unary function for the second argument. It lets you turn a general function into a specialized one without rewriting it.Why does order of arguments matter for partial application?
The most general or reusable arguments should usually come first, and the most specific data should come last. That way, the early arguments can be fixed during setup, while the later ones can vary at runtime.Why does partial application work better with delegates than with methods in C#?
C# method resolution and type inference can become awkward with multi-argument methods, especially when using higher-order functions. Delegates such asFunc are easier for the compiler to infer and are more convenient to partially apply.How does the chapter use partial application for database access?
The chapter shows how to build a function likeRetrieve<T> that takes a connection string and SQL template up front, then returns a function waiting for query parameters. This makes it easy to create specialized functions such as querying employees by ID or last name.How does the functional approach replace interfaces in application composition?
Instead of defining interfaces and injecting objects, the functional approach injects functions directly. Dependencies like validation or saving are just function values, which reduces boilerplate and makes testing simpler.What is the role of Aggregate in this chapter?
Aggregate is used to reduce a list to a single value. In the chapter, it is used to combine multiple validators into one validator, either by failing fast or by collecting all validation errors.
Functional Programming in C#, Second Edition ebook for free