Manning.Functional.Programming.in.Java.2017.1

所需积分/C币:4 2017-11-22 19:39:43 6.45MB PDF

Manning.Functional.Programming.in.Java.2017.1 Manning.Functional.Programming.in.Java.2017.1 Manning.Functional.Programming.in.Java.2017.1
Functional Programming HOW FUNCTIONAL TECHNIQUES IMPROVE YOUR AVA PROGRAMS PIERRE-YVES SAUMONT MANNING SHELTER ISLAND For online information and ordering of this and other manning books, please visit www.manning.com.THepublisheroffersdiscountsonthisbookwhenorderedinquantity For more information, please contact Special Sales Department Manning publications co 20 Baldwin road POBoⅹ761 Shelter island. ny11964 Email:orders@manning.com @2017 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 ermission 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 @o 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: Mark elston PO Box 761 Project editor: Janet vail Shelter island. ny 11964 editor: Andy carroll Proofreaders: Katie Tennant and melody dolab Technical proofreader: Alessandro campers Typesetter Dottie mar Cover designer: Leslie Haimes ISBN9781617292736 Printed in the united states of america 12345678910-EBM-222120191817 brief contents I What is functional programming? I 2 Using functions in Java 16 8 Making Java more functional 57 4 Recursion, corecursion, and memoization 94 Data handling with lists 124 6 Dealing with optional data 151 7 Handling errors and exceptions 176 8 Advanced list handling 203 9 Working with laziness 280 10 More data handling with trees 256 11 Solving real problems with advanced trees 290 12 Handling state mutation in a functional way 321 13 Functional input/output 342 14 Sharing mutable state with actors 370 15 Solving common problems functionally 394 contents preface xiii acknowledgments xvi about this book xvii What is functional programming? I 1. I What is functional programming? 2 1.2 Writing useful programs with no side effects 4 1.3 How referential transparency makes programs safer 6 1. 4 The benefits of functional programming 7 5 Using the substitution model to reason about programs pplying functional principles to a simple example 9 1.7 Pushing abstraction to the limit 14 1. 8 Summary 15 2 Using functions inJava 16 2.1 What is a function? 17 Functions in the real world 17 2.2 Functions in Java 22 Functional methods 23. Java functional interfaces and anonymous classes 28 Composing functions 29 Polymorphic functions 29. Simplifying the code by using lambdas 31 CONTENTS 2.3 Advanced function features 33 What about functions of several arguments? 33. Applying curried functions 34. Higher-order functions 35 Polymorphic higher-order functions 36 Using anonymous functions 39. Local functions 41 Closures 42 Partial function application and automatic currying 44 Switching arguments of partially applied functions 48 Recursive functions 49. The identity function 51 2.4 ava 8 functional interfaces 52 2.5 Debugging with lambdas 58 2.6 Summary 56 3 Making Java more functional 57 3.1 Making standard control structures functional 58 3.2 Abstracting control structures 59 Cleaning up the code63· An alternative to if…ee66 8.3 Abstracting iteration 71 Abstracting an operation on lists with mapping 72 Creating lists 73 Using head and tail operations 74 Functionally appending to a list 75- reducing and folding lists 75 Composing mappings and mapping compositions 82 Applying effects to lists 82 Approaching fu 83 Building corecursive lists 84 8. 4 USing the right types 87 Problems with standard types 87. Defining value types 9 The future of value types in Java 93 3.5 Summary 93 Recursion, corecursion and memoization 94 4.1 Understanding corecursion and recursion 95 Exploring corecursive and recursive addition examples 95 Implementing recursion in Java 96 Using tail call elimination 96 Using tail recursive methods and functions 97 Abstracting recursion 97. Using a drop-in replacement for stack-based recursive methods 101 4.2 Working with recursive functions 103 Using locally defined functions 104- Making functions tai recursive 104. Doubly recursive functions: the Fibonacci example 105. Making the list methods stacr-safe and recursive 108 CONTENTS 4.3 Composing a huge number of functions lll 4.4 Using memoization 114 Memoization in imperative programming 114 Memoization in recursive functions 115- Automatic memoization 117 4.5 Summary 123 5 Data handling with lists 124 5.1 How to classify data collections 125 Different types of lists 125. Relative expected list performance 126 Trading time against memory space, and time against complexity 127- In-place mutation 128 Persistent data structures 129 5.2 An immutable, persistent, singly linked list implementation 130 5.3 Data sharing in list operations 133 More list operations 135 5. 4 Using recursion to fold lists with higher-order functions 140 Heap-based recursive version of foldRight 146. Mapping and filtering lists 148 5.5 Summary 150 6 Dealing with obtional data 151 6.1 Problems with the null pointer 152 6.2 Alternatives to null references 153 6.3 The option data type 156 Getting a value from an Option 158. Applying functions to optional values 160. Dealing with Option composition 161 Option use cases 163 Other ways to combine options 167 Composing list with Option 169 6.4 Miscellaneous utilities for Option 171 Testing for Some or None 171. equals and hashcode 172 6.5 How and when to use Option 172 6.6 Summary 175 Handling errors and exceptions 176 7.1 The problems to be solved 177 vIll CONTENTS 7.2 The Either type 178 Composing Either 179 7. 3 The Result type 181 Adding methods to the Result class 183 7.4 Result patterns 184 7.5 Advanced Result handling 191 Applying predicates 191. Mapping failures 192. Adding factory methods 195. Applying effects 196 Advanced result composition 199 7.6 Summary 202 Advanced list handling 203 8.1 The problem with le en 204 The performance problem 204. The benefit of memoization 205 The drawbacks of memoization 205. Actual performance 207 8.2 Composing List and Result 207 Methods on List returning Result 208 Converting from List< result> to result< list> 209 8.3 Abstracting common List use cases 212 Zipping and unzipping lists 212. Accessing elements by 215. Splitting lists 217. Searching fo sublists 221. Miscellaneous functions for working with lists 222 8.4 Automatic parallel processing of lists 225 Not all computations can be parallelized 226 Breaking the list into sublists 226 Processing sublists in parallel 227 8.5 Summary 2 Working with laziness 230 9.1 Understanding strictness and laziness 230 Java is a strict language 231. The problem with strictness 232 9.2 Implementing laziness 288 9. 3 Things you cant do without laziness 234 9. 4 Why not use the Java 8 Stream? 285 9.5 Creating a lazy list data structure 236 Memoizing evaluated values 237. Manipulating streams 241

...展开详情
img
寒沧

关注 私信 TA的资源

上传资源赚积分,得勋章
最新资源