Overview

10 Working effectively with multi-argument functions

The chapter explains how to work with functions that take multiple arguments when those functions live inside effectful, elevated types such as Option, IEnumerable, and related abstractions. It begins by showing that currying lets a multi-argument function be treated as a unary function, which makes it possible to map it into an elevated context and obtain an elevated function. From there, the chapter introduces the applicative approach, where Apply is used to supply arguments one at a time while staying inside the elevated world, avoiding awkward manual unwrapping and rewrapping.

It then compares applicatives and monads in terms of power and behavior. Applicatives are defined by Return and Apply, and they are especially useful when multiple elevated values are computed independently and then combined. Monads are defined by Return and Bind, and they are more suited to sequencing computations where each step may depend on the previous one. The chapter uses the relationship between these patterns to show that functors, applicatives, and monads form a hierarchy, and it also introduces property-based testing as a way to check algebraic laws such as the applicative law and the monad laws.

The later part of the chapter focuses on readability and practical use. It shows how LINQ syntax can express both simple mappings and more complex workflows with multiple inputs, and how the C# compiler translates query syntax into calls to Select, SelectMany, and related methods. This makes multi-argument workflows much easier to read than deeply nested Bind calls. Finally, the chapter applies these ideas to validation, demonstrating a case where applicative flow is preferable because it can accumulate multiple errors, while monadic flow is better when you want to fail fast. The overall message is that the right tool depends on whether your computations are independent or sequential, and on whether you want error accumulation or short-circuiting.

Mapping a binary function onto an Option yields a unary function wrapped in an Option.
Apply performs function application in the elevated world.
Relationship of functors, applicatives, and monads

Summary

  • You can use the Apply function to perform function application in an elevated world, such as the world of Option.
  • Multi-argument functions can be lifted into an elevated world with Return; then you can supply arguments with Apply.
  • Types for which Apply can be defined are called applicatives. Applicatives are more powerful than functors but less powerful than monads.
  • Because monads are more powerful, you can also use nested calls to Bind; to perform function application in an elevated world.
  • LINQ provides a lightweight syntax for working with monads that reads better than nesting calls to Bind.
  • To use LINQ with a custom type, you must implement the LINQ query pattern, particularly providing implementations of Select and SelectMany with appropriate signatures.
  • For several monads, Bind has short-circuiting behavior (the given function won’t be applied in some cases), but Apply doesn’t (it’s not given a function but rather an elevated value). For this reason, you can sometimes embed desirable behavior into applicatives, such as collecting validation errors in the case of Validation.
  • FsCheck is a framework for property-based testing. It allows you to run a test with a large number of randomly generated inputs, giving high confidence that the test’s assertions hold for any input.

FAQ

What is the main goal of chapter 10, “Working effectively with multi-argument functions”?The chapter teaches how to use multi-argument functions in the world of effectful or elevated types such as Option, Either, Exceptional, IEnumerable, and others. It focuses on two main approaches: applicative programming with Apply and monadic programming with Bind, plus how LINQ can make these workflows more readable.
Why can’t Map and Bind handle multi-argument functions directly?Map and Bind are built around unary functions, so a multi-argument function must first be curried or partially applied. The chapter shows that currying lets a multi-argument function be treated as a chain of unary functions, making it compatible with Map and later with Apply.
What does it mean to “lift” a function into an elevated type?Lifting means wrapping a function itself inside an elevated container, such as Option. For example, Some(multiply) creates an Option containing the function, after which Apply can feed it wrapped arguments.
What is Apply and why is it important?Apply performs function application in the elevated world. It takes a wrapped function and a wrapped value, applies the function if both are present/valid, and wraps the result back up. This is the key operation for applicative programming.
How does the applicative approach differ from the monadic approach?The applicative approach combines independently computed elevated values and can accumulate effects such as errors. The monadic approach sequences computations, where each step may depend on the previous one, and it can short-circuit if an earlier step fails.
Why does Validation work better with Apply than with Bind?Validation can accumulate errors from multiple independent inputs when used with Apply. With Bind, later computations are skipped after the first failure, so only the first error is seen. That makes applicative flow better for harvesting multiple validation errors.
What is the difference between using Map then Apply and lifting the function first?Both approaches are equivalent. You can either map the partially applied function and then apply the next argument, or lift the full function with Return/Some and apply arguments one by one. The chapter notes that lifting first is often more readable.
What is property-based testing, and how is it used in this chapter?Property-based testing checks whether a property or law holds for many randomly generated inputs. In this chapter, FsCheck is used to test laws such as the applicative law and monad laws by generating many Option values automatically.
How does LINQ help with multi-argument functions in monads?LINQ provides a much more readable syntax for nested Bind calls. Multiple from clauses translate into SelectMany calls, letting you write workflows with several dependent steps without deeply nested lambdas.
When should you prefer Apply over Bind, and when should you prefer Bind over Apply?Use Apply when the inputs are independent and the effect should combine them, especially when you want behavior like error accumulation. Use Bind when each step depends on the previous one or when you want fail-fast sequencing. The chapter’s rule of thumb is: use applicative flow when it gives useful behavior; otherwise use monadic flow, often via LINQ.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free