Learning Functional Programming in Scala Alvin Alexander A logical, step by step approach to learning functional programming Copyright Learning Functional Programming in Scala Copyright 2017 Alvin J. Alexander Allrightsreserved. Nopartofthisbookmaybereproducedwithoutprior written permission from the author. Disclaimer: This book is presented solely for educational purposes, and it’s also a work in progress. While best efforts have been used in prepar- ing this book, the author makes no representations or warranties of any kind and assume no liabilities of any kind with respect to the accuracy or completeness of the contents, and specifically disclaim any implied war- ranties of merchantability or fitness of use for a particular purpose. The author shall not be held liable or responsible to any person or entity with respect to any loss or incidental or consequential damages caused, or al- leged to have been caused, directly or indirectly, by the information or programs contained herein. Any use of this information is at your own risk. The advice and strategies contained herein may not be suitable for your situation. Version 0.1.1, published May 15, 2017 (The cover photo was taken on the Dalton Highway in Alaska, some- where between Fairbanks and Prudhoe Bay.) Contents 1 Changelog 1 2 Preface 3 3 Introduction (or, Why I Wrote This Book) 5 4 Who This Book is For 11 5 Goals 15 6 Question Everything 23 7 Rules for Programming in this Book 33 8 One Rule for Reading this Book 39 9 What is “Functional Programming”? 41 10 What is This Lambda You Speak Of? 55 i ii CONTENTS 11 The Benefits of Functional Programming 73 12 Disadvantages of Functional Programming 99 13 The “Great FP Terminology Barrier” 123 14 Pure Functions 131 15 Grandma’s Cookies (and Pure Functions) 141 16 Benefits of Pure Functions 153 17 Pure Functions and I/O 163 18 Pure Function Signatures Tell All 173 19 Functional Programming as Algebra 181 20 A Note About Expression-Oriented Programming 197 21 Functional Programming is Like Unix Pipelines 203 22 Functions Are Variables, Too 225 CONTENTS iii 23 Using Methods As If They Were Functions 243 24 How to Write Functions That Take Functions as Input Parameters 255 25 How to Write a ‘map’ Function 279 26 How to Use By-Name Parameters 287 27 Functions Can Have Multiple Parameter Groups 303 28 Partially-Applied Functions (and Currying) 327 29 Recursion: Introduction 345 30 Recursion: Motivation 347 31 Recursion: Let’s Look at Lists 351 32 Recursion: How to Write a ‘sum’ Function 359 33 Recursion: How Recursive Function Calls Work 367 34 Visualizing the Recursive sum Function 373 iv CONTENTS 35 Recursion: A Conversation Between Two Developers 383 36 Recursion: Thinking Recursively 387 37 JVM Stacks and Stack Frames 397 38 A Visual Look at Stacks and Frames 407 39 Tail-Recursive Algorithms 417 40 A First Look at “State” 431 41 A Functional Game (With a Little Bit of State) 437 42 A Quick Review of Case Classes 455 43 Update as You Copy, Don’t Mutate 465 44 A Quick Review of for Expressions 477 45 How to Write a Class That Can Be Used in a for Expres- sion 487 46 Creating a Sequence Class to be Used in a for Compre- CONTENTS v hension 491 47 Making Sequence Work in a Simple for Loop 499 48 How To Make Sequence Work as a Single Generator in a for Expression 503 49 Enabling Filtering in a for Expression 509 50 How to Enable the Use of Multiple Generators in a for Expression 519 51 A Summary of the for Expression Lessons 531 52 Pure Functions Tell No Lies 535 53 Functional Error Handling (Option, Try, or Either) 543 54 Embrace The Idioms! 557 55 What to Think When You See That Opening Curly Brace561 A Explaining Scala’s val Function Syntax 581 vi CONTENTS B for expression translation examples 599
Description: