Inverted chapter dependency graph Chapter 1 • Functional programming tenets (cid:129) Functional features in C# (cid:129) Higher-order functions Chapter 3 (cid:129) Function signatures: notation and design Chapter 2 (cid:129) Designing types (cid:129) Function purity and side effects (cid:129) The Option type (cid:129) Purity and concurrency (cid:129) Purity and testability Chapter 4 (cid:129) The core functions: Map, Bind, ForEach, Where (cid:129) Regular vs. elevated types Chapter 5 (cid:129) Function composition Chapter 6 (cid:129) Encoding workflows (cid:129) Functional error handling (cid:129) An end-to-end workflow (cid:129) Representing outcomes with Either (cid:129) The Exceptional and Validation types Chapter 7 (cid:129) Partial application and currying Chapter 9 (cid:129) Modularizing and composing an app (cid:129) State, identity, and change (cid:129) The Aggregate function (cid:129) Immutable types (cid:129) Immutable data structures Chapter 8 (cid:129) Multi-argument functions with elevated types Chapter 10 (cid:129) The Apply function (cid:129) Immutable, append-only persistence (cid:129) The LINQ query pattern (cid:129) Event-sourcing concepts (cid:129) Event-sourcing architecture Chapter 11 (cid:129) Lazy computations (cid:129) Composing functions monadically (cid:129) Continuations Chapter 13 (cid:129) Asynchronous computations (cid:129) The Traverse function (cid:129) Combining different monadic effects Chapter 12 (cid:129) Stateful programs (cid:129) Stateful computations (cid:129) Generating random data Chapter 14 (cid:129) Data streams and IObservable (cid:129) Creating and transforming streams Chapter 15 (cid:129) Stateful programs (cid:129) The need for shared mutable state (cid:129) Message-passing concurrency (cid:129) Conventional APIs, agent-based implementations Functional Programming in C# Functional Programming in C# ENRICO BUONANNO MANNING SHELTER ISLAND For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected] ©2018 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Marina Michaels 20 Baldwin Road Technical development editor: Joel Kotarski PO Box 761 Review editor: Aleksandar Dragosavljevic´ Shelter Island, NY 11964 Project editor: Kevin Sullivan Copyeditor: Andy Carroll Proofreader: Melody Dolab Technical proofreaders: Paul Louth, Jürgen Hoetzel Typesetter: Gordan Salinovic Cover designer: Leslie Haimes Cartoons: Viseslav Radovic´, Richard Sheppard Graphic illustrations: Chuck Larson ISBN 9781617293955 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – EBM – 22 21 20 19 18 17 To the little monkey... brief contents PART 1 CORE CONCEPTS...........................................................1 1 ■ Introducing functional programming 3 2 ■ Why function purity matters 31 3 ■ Designing function signatures and types 52 4 ■ Patterns in functional programming 80 5 ■ Designing programs with function composition 102 PART 2 BECOMING FUNCTIONAL ...........................................121 6 ■ Functional error handling 123 7 ■ Structuring an application with functions 149 8 ■ Working effectively with multi-argument functions 177 9 ■ Thinking about data functionally 202 10 ■ Event sourcing: a functional approach to persistence 229 PART 3 ADVANCED TECHNIQUES............................................255 11 ■ Lazy computations, continuations, and the beauty of monadic composition 257 12 ■ Stateful programs and stateful computations 279 vii viii BRIEF CONTENTS 13 ■ Working with asynchronous computations 295 14 ■ Data streams and the Reactive Extensions 320 15 ■ An introduction to message-passing concurrency 345