ebook img

Functional Reactive Programming PDF

362 Pages·2016·2.85 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Functional Reactive Programming

Stephen Blackheath Anthony Jones F Heinrich Apfelmus OREWORD BY M A N N I N G Functional Reactive Programming ii Functional Reactive Programming STEPHEN BLACKHEATH ANTHONY JONES MANNING SHELTER ISLAND iv 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] ©2016 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 elemental chlorine. Manning Publications Co. Development editor: Jennifer Stout 20 Baldwin Road Technical development editor: Dennis Sellinger PO Box 761 Review editor: Aleksandar Dragosavljevic Shelter Island, NY 11964 Project editor: Tiffany Taylor Copyeditor: Tiffany Taylor Proofreader: Melody Dolab Typesetter: Marija Tudor Cover designer: Marija Tudor ISBN: 9781633430105 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – EBM – 21 20 19 18 17 16 brief contents 1 ■ Stop listening! 1 2 ■ Core FRP 26 3 ■ Some everyday widget stuff 60 4 ■ Writing a real application 65 5 ■ New concepts 94 6 ■ FRP on the web 111 7 ■ Switch 131 8 ■ Operational primitives 169 9 ■ Continuous time 186 10 ■ Battle of the paradigms 201 11 ■ Programming in the real world 215 12 ■ Helpers and patterns 232 13 ■ Refactoring 262 14 ■ Adding FRP to existing projects 273 15 ■ Future directions 283 v vi BRIEF CONTENTS contents foreword xv preface xvii acknowledgments xviii about this book xix about the cover xxii 1 Stop listening! 1 1.1 Project, meet complexity wall 2 1.2 What is functional reactive programming? 3 A stricter definition 3 ■ Introducing Sodium 4 1.3 Where does FRP fit in? The lay of the land 4 1.4 Interactive applications: what are events? 5 1.5 State machines are hard to reason about 6 1.6 Interactive applications without the bugs 7 1.7 Listeners are a mainstay of event handling, but … 7 1.8 Banishing the six plagues of listeners 8 1.9 Why not just fix listeners? 9 1.10 “Have you tried restarting it?” or why state is problematic 9 1.11 The benefit of FRP: dealing with complexity 10 1.12 How does FRP work? 11 Life cycle of an FRP program 13 1.13 Paradigm shift 14 Paradigm 14 ■ Paradigm shift 15 1.14 Thinking in terms of dependency 15 vii viii CONTENTS 1.15 Thinking declaratively: what the program is, not what it does 17 1.16 Conceptual vs. operational understanding of FRP 19 Opening your mind to FRP 20 ■ What’s really going on when the code runs? 21 1.17 Applying functional programming to event-based code 22 1.18 Summary 25 2 Core FRP 26 2.1 The Stream type: a stream of events 27 2.2 The map primitive: transforming a value 30 Transforming a stream 31 2.3 The components of an FRP system 32 Combining primitives 32 ■ Separating I/O from logic 33 2.4 Referential transparency required 33 2.5 The Cell type: a value that changes over time 34 Why Stream and Cell? 36 ■ The constant primitive: a cell with a constant value 37 ■ Mapping cells 37 2.6 The merge primitive: merging streams 38 Simultaneous events 39 ■ Collection variants of merge 42 How does merge do its job? 42 2.7 The hold primitive: keeping state in a cell 43 2.8 The snapshot primitive: capturing the value of a cell 45 2.9 Looping hold and snapshot to create an accumulator 47 Forward references 47 ■ Constructing FRP in an explicit transaction 48 ■ Accumulator code 49 Does snapshot see the new value or the old value? 50 2.10 The filter primitive: propagating an event only sometimes 52 2.11 The lift primitive: combining cells 53 2.12 The never primitive: a stream that never fires 55 2.13 Referential transparency dos and don’ts 55 2.14 FRP cheat sheet 57 2.15 Summary 58 3 Some everyday widget stuff 60 3.1 Spinner as a standalone SWidget 60 3.2 Form validation 62 3.3 Summary 64 CONTENTS ix 4 Writing a real application 65 4.1 The petrol pump example 66 4.2 Running the petrol pump example 68 4.3 Code, meet outside world 68 4.4 The life cycle of a petrol pump fill 73 Code for LifeCycle 75 4.5 Is this really better? 77 4.6 Counting liters delivered 78 4.7 Showing dollars’ of fuel delivered 79 4.8 Communicating with the point-of-sale system 82 4.9 Modularity illustrated: a keypad module 85 4.10 Notes about modularity 87 The form of a module 87 ■ Tuples vs. classes 88 Explicit wiring 88 ■ When inputs and outputs proliferate 88 Some bugs are solved, some aren’t 88 ■ Testability 89 4.11 Adding a preset dollar amount 89 4.12 What have you achieved? 92 4.13 Summary 93 5 New concepts 94 5.1 In search of the mythical von Neumann machine 94 Why so slow? The cache 96 ■ The madness of bus optimization 98 ■ How does this relate to FRP? 101 5.2 Compositionality 101 When complexity gets out of control 101 ■ Reductionism and engineering 102 ■ Compositionality is no longer optional 104 5.3 Lack of compositionality illustrated 104 Why the OO version lacks compositionality 105 5.4 Compositionality: eliminating whole classes of bugs 106 5.5 Don’t pull out the rug: use immutable values 107 Immutable data structures 107 5.6 Clarity of intent 108 5.7 The consequences of cheap abstraction 109 5.8 Summary 109 6 FRP on the web 111 6.1 RxJS 112

Description:
Summary Functional Reactive Programming teaches the concepts and applications of FRP. It offers a careful walk-through of core FRP operations and introduces the concepts and techniques you'll need to use FRP in any language. Purchase of the print book includes a free eBook in PDF, Kindle, and ePub f
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.