ebook img

Professional Scala PDF

232 Pages·2016·5.272 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 Professional Scala

PROFESSIONAL SCALA INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv CHAPTER 1 Language Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 CHAPTER 2 Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 CHAPTER 3 Java Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 CHAPTER 4 Simple Build Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 CHAPTER 5 Maven . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 CHAPTER 6 Scala Style/Lint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 CHAPTER 7 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 CHAPTER 8 Documenting Your Code with Scaladoc . . . . . . . . . . . . . . . . . . . . . . 95 CHAPTER 9 Type System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 CHAPTER 10 Advanced Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . 165 CHAPTER 11 Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 CHAPTER 12 Scala.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 ffi rs.indd 05/12/2016 Page i PROFESSIONAL Scala Aliaksandr Bedrytski Janek Bogucki Alessandro Lacava Matthew de Detrich Benjamin Neil ffi rs.indd 05/12/2016 Page iii Professional Scala Published by John Wiley & Sons, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2016 by John Wiley & Sons, Inc., Indianapolis, Indiana Published by John Wiley & Sons, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-1-119-26722-5 ISBN: 978-1-119-26725-6 (ebk) ISBN: 978-1-119-26726-3 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifi cally disclaim all warranties, including without limitation warranties of fi tness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional ser- vices. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the pub- lisher endorses the information the organization or Web site may provide or recommendations it may make. Further, read- ers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read. For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with stan- dard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://book- support.wiley.com. For more information about Wiley products, visit www.wiley.com. Library of Congress Control Number: 2016937234 Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trade- marks or registered trademarks of John Wiley & Sons, Inc. and/or its affi liates, in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book. ffi rs.indd 05/12/2016 Page iv ABOUT THE AUTHORS ALIAKSANDR BEDRYTSKI is a passionate software engineer at Worldline Lyon, France. Even though it’s been more than four years since he discovered Scala, he’s still in love with the language and is trying to push its boundaries every day. In his spare time he’s learning something new about space, physics, and bleeding edge technologies. He is currently working as a lead developer on a data- analysis project featuring Hadoop, Spark-Scala, Hive, and other Big Data tools.  JANEK BOGUCKI is a co-founder at Inferess Inc. and principal consultant (Machine Learning and Scala) at Combination One. He has a background in mathematics with an ongoing interest in com- puter science, data science, machine learning, graph theory, and development methodologies. He lives in Kent, UK with his wife Rebecca, son Theo, two cats, one dog, and a variable number of chickens. ALESSANDRO LACAVA holds a degree in telecommunications engineering. He’s had extensive experience with OOP before becoming very passionate about functional programming. He is cur- rently working as a lead designer and developer on different types of applications using mainly, but not only, Scala. He’s also a contributor of pretty famous open source projects, such as shape- less and cats. MATTHEW DE DETRICH is a tech lead for Monetise Pty Ltd, where he primarily works on full stack applications with backends written in Scala. Matthew has a passion for designing, building, and integrating complex multi-domain systems from the ground up. In his spare time he is exploring new developments in the Scala space, such as Scala.js. BENJAMIN NEIL is a full stack engineer at AppThis LLC. He is a polyglot engineer who has had the privilege of working the past eight years making websites, services, and tools for amazing compa- nies. He is obsessed with Scala, Vim, DevOps, and making services scale smoothly. ffi rs.indd 05/12/2016 Page v ABOUT THE TECHNICAL EDITORS JACOB PARK is a Scala and Typesafe enthusiast; he embraces Akka, Play, and Spark, all with Scala, to solve various problems regarding distributed systems with additional technologies such as Cassandra and Kafka. As a believer in open-source software, he contributes to various projects on GitHub related to Akka and Cassandra: https://github.com/jparkie. When he’s not working, Jacob can be found at various Scala and Typesafe conferences and meetups, either as an attendee or a presenter. ARIEL SCARPINELLI is a senior Java developer at VirtualMind and is a passionate developer with more than 15 years of professional experience. He currently leads four agile teams for a U.S.-based enterprise SaaS company. He has experience in a lot of languages but is currently focused on Java and JavaScript with some PHP and Python. RADU GANCEA is a software engineer and consultant at Eloquentix with a Java background (also C/ C++). He is involved in projects in the energy and advertising sectors. His current focus is on Scala and other JVM languages and frameworks. ffi rs.indd 05/12/2016 Page vi CREDITS PROJECT EDITOR PROFESSIONAL TECHNOLOGY & STRATEGY Charlotte Kughen DIRECTOR Barry Pruett TECHNICAL EDITORS Jacob Park BUSINESS MANAGER Ariel Scarpinelli Amy Knies Radu Gancea EXECUTIVE EDITOR PRODUCTION EDITOR Jim Minatel Barath Kumar Rajasekaran PROJECT COORDINATOR, COVER COPY EDITOR Brent Savage Troy Mott PROOFREADER MANAGER OF CONTENT DEVELOPMENT Nancy Bell AND ASSEMBLY Mary Beth Wakefi eld INDEXER Nancy Guenther PRODUCTION MANAGER Kathleen Wisor COVER DESIGNER Wiley MARKETING MANAGER David Mayhew COVER IMAGE ©Sergey Nivens/Shutterstock ffi rs.indd 05/12/2016 Page vii CONTENTS INTRODUCTION xv CHAPTER 1: LANGUAGE FEATURES 1 Static Types and Type Inference 2 Implicit Parameters, Conversions, and Their Resolution 3 Case Class, Tuples, and Case Object 5 Abstract Class, Traits, and Sealed 6 Pattern Matching 8 Statements Are Expressions 9 String Interpolation 9 Scala Collections, immutable and mutable 10 For Comprehension 12 Packages, Companion Objects, Package Objects, and Scoping 13 AnyVal, AnyRef, Any, and the Type Hierarchy 16 Summary 17 CHAPTER 2: FUNCTIONAL PROGRAMMING 19 Immutability 20 Pure Functions 22 Recursion 23 Higher-Order Functions 26 Core Collection Methods 27 Methods Returning a Collection 29 Methods Returning a Value 31 Currying and Partially Applied Functions 32 Null Handling (Option) 34 Strict versus Non-Strict Initialization 35 Summary 36 CHAPTER 3: JAVA COMPATIBILITY 37 Scala and Java Collections 37 Interfaces and Traits 40 Scala/Java Enumerations 42 Summary 43 ftoc.indd 05/11/2016 Page ix CONTENTS CHAPTER 4: SIMPLE BUILD TOOL 45 Basic Usage 46 Project Structure 47 Single Project 47 Scopes 49 Custom Tasks 50 Dependencies 50 Resolvers 51 Advanced Usage 52 Advanced Dependencies 53 Testing in the Console 55 Release Management 56 Deploying to Sonatype 56 Packaging with SBT-Native-Packager 58 Creating a Docker Image 59 Common SBT Commands 60 Useful Plugins 61 Summary 62 CHAPTER 5: MAVEN 63 Getting Started with Maven and Scala 64 Introducing scala-maven-plugin 67 Adding Library Dependencies 70 Using the REPL 71 Getting Help 72 Running Tests 72 Joint Compilation with Java 74 Accelerating Compilation with Zinc 76 Summary 77 CHAPTER 6: SCALA STYLE/LINT 79 Scala with Style 79 Scaliform 81 Scapegoat 82 WartRemover 82 Scoverage 84 Summary 84 x ftoc.indd 05/11/2016 Page x CONTENTS CHAPTER 7: TESTING 85 ScalaTest 86 Unit Tests 87 Integration Testing 87 Data-Driven Tests 88 Performance Testing 89 Acceptance Testing 90 Mocks 92 Load Testing 93 Summary 94 CHAPTER 8: DOCUMENTING YOUR CODE WITH SCALADOC 95 Why Document Your Code? 96 Revealing the Benefi ts 96 Bookending the Continuum 96 Choosing What to Document 96 Scaladoc Structure 97 Overall Layout 97 Index Pane 98 Content Pane 100 Invoking the Scaladoc Tool 106 Wiki Syntax 108 Formatting with Inline Wiki Syntax 108 Structuring with Block Elements 110 Linking 113 Locating Scaladoc 117 Tagging 117 Everyday Tagging 117 Tagging for Groups 123 Advanced Tagging 125 Invoking scaladoc: Additional Options 132 Integrating Scaladoc Creation with Your Project 133 Confi guring Maven 133 Confi guring SBT 134 Publishing Scaladoc 134 Tables and CSS 136 Summary 138 xi ftoc.indd 05/11/2016 Page xi CONTENTS CHAPTER 9: TYPE SYSTEM 139 What Is a Type System? 140 Static versus Dynamic Typing 140 What Static Type Systems Are Good For 141 What Dynamic Type Systems Are Good For 141 Scala’s Unifi ed Type System 141 Value Classes 143 Polymorphism 145 Subtype Polymorphism 145 Parametric Polymorphism 146 Ad Hoc Polymorphism 146 Bounds 149 Context Bounds 149 Upper and Lower Bounds 150 Variance 151 Other Niceties 155 Self-Type Annotations 155 Self-Recursive Types 158 Abstract Type Members 159 Dynamic Programming 161 Structural Types 161 Dynamic Trait 162 Summary 164 CHAPTER 10: ADVANCED FUNCTIONAL PROGRAMMING 165 Higher-Kinded Types 165 Functional Design Patterns 167 Functor 167 Applicative Functor 170 Monad 172 Semigroup 173 Monoid 174 Summary 176 CHAPTER 11: CONCURRENCY 179 Synchronize/Atomic Variables 181 Future Composition 184 Parallel Collections 187 Reactive Streams 192 STM 195 xii ftoc.indd 05/11/2016 Page xii

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.