ebook img

Programming in Scala: A Comprehensive Step-by-step Guide PDF

754 Pages·2008·2.466 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 Programming in Scala: A Comprehensive Step-by-step Guide

Cover·Overview·Contents·Discuss·Suggest·Glossary·Index Programming in Scala Cover·Overview·Contents·Discuss·Suggest·Glossary·Index Programming in Scala Martin Odersky, Lex Spoon, Bill Venners artima ARTIMAPRESS MOUNTAINVIEW,CALIFORNIA Cover·Overview·Contents·Discuss·Suggest·Glossary·Index iv ProgramminginScala FirstEdition,Version6 MartinOderskyisthecreatoroftheScalalanguageandaprofessoratEPFLin Lausanne,Switzerland. LexSpoonworkedonScalafortwoyearsasapost-doc withMartinOdersky. BillVennersispresidentofArtima,Inc. ArtimaPressisanimprintofArtima,Inc. P.O.Box390122,MountainView,California94039 Copyright©2007,2008MartinOdersky,LexSpoon,andBillVenners. Allrightsreserved. FirsteditionpublishedasPrePrint™eBook2007 Firsteditionpublished2008 ProducedintheUnitedStatesofAmerica 1211100908 56789 ISBN-10: 0-9815316-1-X ISBN-13: 978-0-9815316-1-8 Nopartofthispublicationmaybereproduced,modified,distributed,storedina retrievalsystem,republished,displayed,orperformed,forcommercialor noncommercialpurposesorforcompensationofanykindwithoutpriorwritten permissionfromArtima,Inc. Allinformationandmaterialsinthisbookareprovided"asis"andwithout warrantyofanykind. Theterm“Artima”andtheArtimalogoaretrademarksorregisteredtrademarksof Artima,Inc. Allothercompanyand/orproductnamesmaybetrademarksor registeredtrademarksoftheirowners. Cover·Overview·Contents·Discuss·Suggest·Glossary·Index toNastaran-M.O. toFay-L.S. toSiew-B.V. Cover·Overview·Contents·Discuss·Suggest·Glossary·Index Overview Contents vii ListofFigures xvii ListofTables xix ListofListings xx Foreword xxvii Acknowledgments xxix Introduction xxxi 1. AScalableLanguage 39 2. FirstStepsinScala 58 3. NextStepsinScala 71 4. ClassesandObjects 93 5. BasicTypesandOperations 107 6. FunctionalObjects 129 7. Built-inControlStructures 149 8. FunctionsandClosures 173 9. ControlAbstraction 194 10. CompositionandInheritance 209 11. Scala’sHierarchy 237 12. Traits 245 13. PackagesandImports 264 14. AssertionsandUnitTesting 279 15. CaseClassesandPatternMatching 293 16. WorkingwithLists 327 17. Collections 359 18. StatefulObjects 387 19. TypeParameterization 410 20. AbstractMembers 435 21. ImplicitConversionsandParameters 465 22. ImplementingLists 487 23. ForExpressionsRevisited 500 24. Extractors 516 25. Annotations 532 26. WorkingwithXML 539 27. ModularProgrammingUsingObjects 553 28. ObjectEquality 568 29. CombiningScalaandJava 594 30. ActorsandConcurrency 607 31. CombinatorParsing 642 32. GUIProgramming 671 33. TheSCellsSpreadsheet 683 A. ScalascriptsonUnixandWindows 708 Glossary 709 Bibliography 725 AbouttheAuthors 728 Index 729 Cover·Overview·Contents·Discuss·Suggest·Glossary·Index Contents Contents vii ListofFigures xvii ListofTables xix ListofListings xx Foreword xxvii Acknowledgments xxix Introduction xxxi 1 AScalableLanguage 39 1.1 Alanguagethatgrowsonyou . . . . . . . . . . . . . . 40 1.2 WhatmakesScalascalable? . . . . . . . . . . . . . . . 45 1.3 WhyScala?. . . . . . . . . . . . . . . . . . . . . . . . 48 1.4 Scala’sroots . . . . . . . . . . . . . . . . . . . . . . . 55 1.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . 57 2 FirstStepsinScala 58 Step1. LearntousetheScalainterpreter . . . . . . . . . . . . 58 Step2. Definesomevariables . . . . . . . . . . . . . . . . . 60 Step3. Definesomefunctions . . . . . . . . . . . . . . . . . 62 Step4. WritesomeScalascripts . . . . . . . . . . . . . . . . 64 Step5. Loopwithwhile;decidewithif . . . . . . . . . . . 65 Step6. Iteratewithforeachandfor . . . . . . . . . . . . . 67 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Cover·Overview·Contents·Discuss·Suggest·Glossary·Index Contents viii 3 NextStepsinScala 71 Step7. Parameterizearrayswithtypes . . . . . . . . . . . . 71 Step8. Uselists . . . . . . . . . . . . . . . . . . . . . . . . 75 Step9. Usetuples . . . . . . . . . . . . . . . . . . . . . . . 80 Step10. Usesetsandmaps . . . . . . . . . . . . . . . . . . . 81 Step11. Learntorecognizethefunctionalstyle . . . . . . . . 86 Step12. Readlinesfromafile . . . . . . . . . . . . . . . . . 89 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 4 ClassesandObjects 93 4.1 Classes,fields,andmethods . . . . . . . . . . . . . . . 93 4.2 Semicoloninference . . . . . . . . . . . . . . . . . . . 98 4.3 Singletonobjects . . . . . . . . . . . . . . . . . . . . . 99 4.4 AScalaapplication . . . . . . . . . . . . . . . . . . . 102 4.5 TheApplicationtrait . . . . . . . . . . . . . . . . . . 105 4.6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . 106 5 BasicTypesandOperations 107 5.1 Somebasictypes . . . . . . . . . . . . . . . . . . . . . 107 5.2 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . 108 5.3 Operatorsaremethods . . . . . . . . . . . . . . . . . . 115 5.4 Arithmeticoperations . . . . . . . . . . . . . . . . . . 118 5.5 Relationalandlogicaloperations . . . . . . . . . . . . 119 5.6 Bitwiseoperations . . . . . . . . . . . . . . . . . . . . 121 5.7 Objectequality . . . . . . . . . . . . . . . . . . . . . . 123 5.8 Operatorprecedenceandassociativity . . . . . . . . . . 124 5.9 Richwrappers . . . . . . . . . . . . . . . . . . . . . . 127 5.10 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . 127 6 FunctionalObjects 129 6.1 AspecificationforclassRational . . . . . . . . . . . 129 6.2 ConstructingaRational . . . . . . . . . . . . . . . . 130 6.3 ReimplementingthetoStringmethod . . . . . . . . . 132 6.4 Checkingpreconditions . . . . . . . . . . . . . . . . . 133 6.5 Addingfields . . . . . . . . . . . . . . . . . . . . . . . 133 6.6 Selfreferences . . . . . . . . . . . . . . . . . . . . . . 135 6.7 Auxiliaryconstructors . . . . . . . . . . . . . . . . . . 136 6.8 Privatefieldsandmethods . . . . . . . . . . . . . . . . 138 Cover·Overview·Contents·Discuss·Suggest·Glossary·Index Contents ix 6.9 Definingoperators . . . . . . . . . . . . . . . . . . . . 139 6.10 IdentifiersinScala . . . . . . . . . . . . . . . . . . . . 141 6.11 Methodoverloading . . . . . . . . . . . . . . . . . . . 144 6.12 Implicitconversions . . . . . . . . . . . . . . . . . . . 146 6.13 Awordofcaution . . . . . . . . . . . . . . . . . . . . 147 6.14 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . 147 7 Built-inControlStructures 149 7.1 Ifexpressions . . . . . . . . . . . . . . . . . . . . . . 150 7.2 Whileloops . . . . . . . . . . . . . . . . . . . . . . . 151 7.3 Forexpressions . . . . . . . . . . . . . . . . . . . . . . 154 7.4 Exceptionhandlingwithtryexpressions . . . . . . . . 159 7.5 Matchexpressions . . . . . . . . . . . . . . . . . . . . 163 7.6 Livingwithoutbreakandcontinue . . . . . . . . . . 165 7.7 Variablescope . . . . . . . . . . . . . . . . . . . . . . 167 7.8 Refactoringimperative-stylecode . . . . . . . . . . . . 170 7.9 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . 172 8 FunctionsandClosures 173 8.1 Methods . . . . . . . . . . . . . . . . . . . . . . . . . 173 8.2 Localfunctions . . . . . . . . . . . . . . . . . . . . . . 175 8.3 First-classfunctions . . . . . . . . . . . . . . . . . . . 177 8.4 Shortformsoffunctionliterals . . . . . . . . . . . . . 179 8.5 Placeholdersyntax . . . . . . . . . . . . . . . . . . . . 180 8.6 Partiallyappliedfunctions . . . . . . . . . . . . . . . . 181 8.7 Closures . . . . . . . . . . . . . . . . . . . . . . . . . 184 8.8 Repeatedparameters . . . . . . . . . . . . . . . . . . . 188 8.9 Tailrecursion . . . . . . . . . . . . . . . . . . . . . . . 189 8.10 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . 193 9 ControlAbstraction 194 9.1 Reducingcodeduplication . . . . . . . . . . . . . . . . 194 9.2 Simplifyingclientcode . . . . . . . . . . . . . . . . . 198 9.3 Currying . . . . . . . . . . . . . . . . . . . . . . . . . 200 9.4 Writingnewcontrolstructures . . . . . . . . . . . . . . 202 9.5 By-nameparameters . . . . . . . . . . . . . . . . . . . 205 9.6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . 208 Cover·Overview·Contents·Discuss·Suggest·Glossary·Index Contents x 10 CompositionandInheritance 209 10.1 Atwo-dimensionallayoutlibrary . . . . . . . . . . . . 209 10.2 Abstractclasses . . . . . . . . . . . . . . . . . . . . . 210 10.3 Definingparameterlessmethods . . . . . . . . . . . . . 211 10.4 Extendingclasses . . . . . . . . . . . . . . . . . . . . 214 10.5 Overridingmethodsandfields . . . . . . . . . . . . . . 216 10.6 Definingparametricfields . . . . . . . . . . . . . . . . 217 10.7 Invokingsuperclassconstructors. . . . . . . . . . . . . 219 10.8 Usingoverridemodifiers . . . . . . . . . . . . . . . . 220 10.9 Polymorphismanddynamicbinding . . . . . . . . . . 222 10.10 Declaringfinalmembers . . . . . . . . . . . . . . . . . 224 10.11 Usingcompositionandinheritance . . . . . . . . . . . 226 10.12 Implementingabove,beside,andtoString . . . . . . 227 10.13 Definingafactoryobject . . . . . . . . . . . . . . . . . 229 10.14 Heightenandwiden . . . . . . . . . . . . . . . . . . . 231 10.15 Puttingitalltogether . . . . . . . . . . . . . . . . . . . 235 10.16 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . 236 11 Scala’sHierarchy 237 11.1 Scala’sclasshierarchy . . . . . . . . . . . . . . . . . . 237 11.2 Howprimitivesareimplemented . . . . . . . . . . . . 241 11.3 Bottomtypes . . . . . . . . . . . . . . . . . . . . . . . 243 11.4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . 244 12 Traits 245 12.1 Howtraitswork . . . . . . . . . . . . . . . . . . . . . 245 12.2 Thinversusrichinterfaces . . . . . . . . . . . . . . . . 248 12.3 Example: Rectangularobjects . . . . . . . . . . . . . . 249 12.4 TheOrderedtrait . . . . . . . . . . . . . . . . . . . . 252 12.5 Traitsasstackablemodifications. . . . . . . . . . . . . 254 12.6 Whynotmultipleinheritance? . . . . . . . . . . . . . . 258 12.7 Totrait,ornottotrait? . . . . . . . . . . . . . . . . . . 262 12.8 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . 263 13 PackagesandImports 264 13.1 Packages . . . . . . . . . . . . . . . . . . . . . . . . . 264 13.2 Imports . . . . . . . . . . . . . . . . . . . . . . . . . . 268 13.3 Implicitimports . . . . . . . . . . . . . . . . . . . . . 272 Cover·Overview·Contents·Discuss·Suggest·Glossary·Index

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.