1. Trang chủ
  2. » Công Nghệ Thông Tin

Scala for Java Developers A Practical Primer

246 35 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Table of Contents

  • About the Author

  • About the Technical Reviewer

  • Acknowledgments

  • Preface

  • Part I: Scala Tour

    • Chapter 1: The Scala Language

      • As a Functional Programming Language

      • The Past

      • The Future

    • Chapter 2: Installing Scala

      • Getting Started

      • The Scala Interpreter

      • Scala Scripts

      • scalac

    • Chapter 3: Some Basic Syntax

      • Defining Values and Variables

      • Defining Functions

      • Operator Overloading and Infix Notation

      • Collections

      • Tuples

      • Java Interoperability

      • Primitive Types

    • Chapter 4: Scala’s Class Hierarchy

      • AnyVal

      • Unit

      • AnyRef

      • Bottom Types

    • Chapter 5: ScalaDoc

    • Chapter 6: Language Features

      • Working with Source Code

      • Working with Methods

      • Functional Programming

    • Chapter 7: Summary

  • Part II: Key Syntactical Differences

    • Chapter 8: Classes and Fields

      • Creating Classes

      • Derived Setters and Getters

      • Redefining Setters and Getters

      • Summary

    • Chapter 9: Classes and Objects

      • Classes Without Constructor Arguments

      • Additional Constructors

        • Using Default Values

      • Singleton Objects

      • Companion Objects

        • Other Uses for Companion Objects

    • Chapter 10: Classes and Functions

      • Anonymous Functions

      • Anonymous Classes

      • First-class Functions

        • Passing in Functions

        • Returning Functions

        • Storing Functions

      • Function Types

      • Functions vs. Methods

      • Lambdas vs. Closures

    • Chapter 11: Inheritance

      • Subtype Inheritance

      • Anonymous Classes

      • Interfaces/Traits

        • Methods on Traits

        • Converting Anonymous Classes to Lambdas

        • Concrete Fields on Traits

        • Abstract Fields on Traits

      • Abstract Classes

      • Polymorphism

        • Traits vs. Abstract Classes

      • Deciding Between the Options

    • Chapter 12: Control Structures

      • Conditionals

        • Ifs and Ternaries

        • Switch Statements

      • Looping Structures: do, while and for

      • Breaking Control Flow (break and continue)

      • Exceptions

    • Chapter 13: Generics

      • Parametric Polymorphism

        • Class Generics

        • Method Generics

        • Stack Example

      • Bounded Classes

        • Upper Bounds (<U extends T>)

        • Lower Bounds (<U super T>)

        • Wildcard Bounds (<? extends T, <? super T>)

        • Multiple Bounds

      • Variance

        • Invariance

        • Covariance

        • Contravariance

        • Variance Summary

  • Part III: Beyond Java to Scala

    • Chapter 14: Faking Function Calls

      • The apply Method

      • The update Method

        • Multiple update Methods

        • Multiple Arguments to update

      • Summary

    • Chapter 15: Faking Language Constructs

      • Curly Braces (and Function Literals)

        • Higher-Order Functions

        • Higher-Order Functions with Curly Braces

        • Call-by-Name

      • Currying

        • Scala Support for Curried Functions

      • Summary

    • Chapter 16: Pattern Matching

      • Switching

      • Patterns

      • Literal Matches

      • Constructor Matches

      • Type Query

      • Deconstruction Matches and unapply

        • Why Write Your Own Extractors?

      • Guard Conditions

    • Chapter 17: Map and FlatMap

      • Mapping Functions

        • It's Like foreach

      • FlatMap

      • Not Just for Collections

    • Chapter 18: Monads

      • Basic Definition

      • Option

        • The map Function

        • Option.get

        • Option.getOrElse

        • Monadically Processing Option

        • The Option.flatMap Function

      • More Formal Definition

      • Summary

    • Chapter 19: For Comprehensions

      • Where We Left Off

      • Using Null Checks

      • Using FlatMap with Option

      • How For Comprehensions Work

      • Finally, Using a For Comprehension for Shipping Labels

      • Summary

  • Part IV: Adopting Scala in Java Teams

    • Chapter 20: Adopting Scala

      • Avoid Not Enough

      • Don’t Do Too Much

      • Purely Functional FTW?

    • Chapter 21: What to Expect

      • The Learning Curve

        • Milestone 1: OO in Scala

        • Milestone 2: FP in the Small, OO in the Large

        • Milestone 3: FP in the Large

      • The Learning Continuum

        • Goals

    • Chapter 22: Tips

      • Be Clear

      • Get Guidance

      • Have a Plan

    • Chapter 23: Convert Your Codebase

    • Chapter 24: Manage Your Codebase

      • Conventions

      • What to Avoid

      • Other Challenges

  • Appendix A: Code Listings

    • Inheritance

      • Subtype Inheritance in Java

      • Anonymous Classes in Java

      • Subtype Inheritance in Scala

      • Anonymous Classes in Scala

    • Generics

      • Lower Bounds in Java

        • Example 1

        • Example 2

        • Example 3

      • Multiple Bounds in Java

        • Java Example 1

      • Lower Bounds in Scala

        • Example 1

        • Example 2

        • Example 3

      • Multiple Bounds in Scala

        • Example 1

    • Pattern Matching

      • Constructor Matches

        • Example 1

        • Example 2

        • Example 3

        • Example 4

      • Deconstruction Matches and Unapply

        • Scala Final Example

    • Map

      • Mapping Functions

      • FlatMap

  • Appendix B: Syntax Cheat Sheet

    • Values

    • Functions

    • Call-by-Name

    • Operator Overloading and Infix

    • Classes

    • Case Classes

    • Singleton Object

    • Inheritance

    • Fields

    • Collections

    • String Formatting

    • Apply Method

    • Update Method

    • Pattern Matching

    • Covariance Types

      • Example

    • Upper Type Bounds (extends)

    • Contravariant Types

    • Lower Type Bounds (super)

  • Index

Nội dung

Scala for Java Developers A Practical Primer — Toby Weston Scala for Java Developers A Practical Primer Toby Weston Scala for Java Developers: A Practical Primer Toby Weston London, United Kingdom ISBN-13 (pbk): 978-1-4842-3107-4 https://doi.org/10.1007/978-1-4842-3108-1 ISBN-13 (electronic): 978-1-4842-3108-1 Library of Congress Control Number: 2017963118 Copyright © 2018 by Toby Weston This work is subject to copyright All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed Trademarked names, logos, and images may appear in this book Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made The publisher makes no warranty, express or implied, with respect to the material contained herein Cover image by Freepik (www.freepik.com) Managing Director: Welmoed Spahr Editorial Director: Todd Green Acquisitions Editor: Steve Anglin Development Editor: Matthew Moodie Technical Reviewer: Jeff Friesen Coordinating Editor: Mark Powers Copy Editor: Francesca Louise White Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013 Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springersbm.com, or visit www.springeronline.com Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc) SSBM Finance Inc is a Delaware corporation For information on translations, please e-mail rights@apress.com, or visit http://www.apress.com/ rights-permissions Apress titles may be purchased in bulk for academic, corporate, or promotional use eBook versions and licenses are also available for most titles For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/9781484231074 For more detailed information, please visit http://www.apress.com/source-code Printed on acid-free paper In memory of Félix Javier García López Table of Contents About the Author��������������������������������������������������������������������������������������������������� xiii About the Technical Reviewer���������������������������������������������������������������������������������xv Acknowledgments�������������������������������������������������������������������������������������������������xvii Preface�������������������������������������������������������������������������������������������������������������������xix Part I: Scala Tour Chapter 1: The Scala Language As a Functional Programming Language�������������������������������������������������������������������������������������� The Past���������������������������������������������������������������������������������������������������������������������������������������� The Future������������������������������������������������������������������������������������������������������������������������������������� Chapter 2: Installing Scala Getting Started������������������������������������������������������������������������������������������������������������������������������ The Scala Interpreter�������������������������������������������������������������������������������������������������������������������� Scala Scripts��������������������������������������������������������������������������������������������������������������������������������� scalac�������������������������������������������������������������������������������������������������������������������������������������������� Chapter 3: Some Basic Syntax 11 Defining Values and Variables����������������������������������������������������������������������������������������������������� 11 Defining Functions���������������������������������������������������������������������������������������������������������������������� 12 Operator Overloading and Infix Notation������������������������������������������������������������������������������������� 14 Collections���������������������������������������������������������������������������������������������������������������������������������� 15 Tuples������������������������������������������������������������������������������������������������������������������������������������������ 17 Java Interoperability������������������������������������������������������������������������������������������������������������������� 18 Primitive Types���������������������������������������������������������������������������������������������������������������������������� 19 v Table of Contents Chapter 4: Scala’s Class Hierarchy 21 AnyVal����������������������������������������������������������������������������������������������������������������������������������������� 22 Unit���������������������������������������������������������������������������������������������������������������������������������������������� 23 AnyRef����������������������������������������������������������������������������������������������������������������������������������������� 24 Bottom Types������������������������������������������������������������������������������������������������������������������������������ 25 Chapter 5: ScalaDoc����������������������������������������������������������������������������������������������� 29 Chapter 6: Language Features�������������������������������������������������������������������������������� 33 Working with Source Code���������������������������������������������������������������������������������������������������������� 33 Working with Methods���������������������������������������������������������������������������������������������������������������� 34 Functional Programming������������������������������������������������������������������������������������������������������������� 36 Chapter 7: Summary 37 Part II: Key Syntactical Differences 39 Chapter 8: Classes and Fields 43 Creating Classes������������������������������������������������������������������������������������������������������������������������� 43 Derived Setters and Getters�������������������������������������������������������������������������������������������������������� 44 Redefining Setters and Getters��������������������������������������������������������������������������������������������������� 48 Summary������������������������������������������������������������������������������������������������������������������������������������ 52 Chapter 9: Classes and Objects 53 Classes Without Constructor Arguments������������������������������������������������������������������������������������� 53 Additional Constructors��������������������������������������������������������������������������������������������������������������� 55 Using Default Values�������������������������������������������������������������������������������������������������������������� 57 Singleton Objects������������������������������������������������������������������������������������������������������������������������ 58 Companion Objects��������������������������������������������������������������������������������������������������������������������� 61 Other Uses for Companion Objects���������������������������������������������������������������������������������������� 63 Chapter 10: Classes and Functions������������������������������������������������������������������������ 67 Anonymous Functions����������������������������������������������������������������������������������������������������������������� 67 Anonymous Classes�������������������������������������������������������������������������������������������������������������������� 68 vi Table of Contents First-class Functions������������������������������������������������������������������������������������������������������������������� 69 Passing in Functions������������������������������������������������������������������������������������������������������������� 70 Returning Functions�������������������������������������������������������������������������������������������������������������� 71 Storing Functions������������������������������������������������������������������������������������������������������������������ 72 Function Types���������������������������������������������������������������������������������������������������������������������������� 74 Functions vs Methods���������������������������������������������������������������������������������������������������������������� 74 Lambdas vs Closures����������������������������������������������������������������������������������������������������������������� 75 Chapter 11: Inheritance 79 Subtype Inheritance�������������������������������������������������������������������������������������������������������������������� 79 Anonymous Classes�������������������������������������������������������������������������������������������������������������������� 82 Interfaces/Traits�������������������������������������������������������������������������������������������������������������������������� 84 Methods on Traits������������������������������������������������������������������������������������������������������������������ 87 Converting Anonymous Classes to Lambdas������������������������������������������������������������������������� 94 Concrete Fields on Traits������������������������������������������������������������������������������������������������������� 95 Abstract Fields on Traits�������������������������������������������������������������������������������������������������������� 96 Abstract Classes������������������������������������������������������������������������������������������������������������������������� 97 Polymorphism����������������������������������������������������������������������������������������������������������������������������� 98 Traits vs Abstract Classes����������������������������������������������������������������������������������������������������� 99 Deciding Between the Options�������������������������������������������������������������������������������������������������� 103 Chapter 12: Control Structures����������������������������������������������������������������������������� 105 Conditionals������������������������������������������������������������������������������������������������������������������������������ 105 Ifs and Ternaries������������������������������������������������������������������������������������������������������������������ 105 Switch Statements�������������������������������������������������������������������������������������������������������������� 108 Looping Structures: do, while and for��������������������������������������������������������������������������������������� 113 Breaking Control Flow (break and continue)����������������������������������������������������������������������������� 116 Exceptions��������������������������������������������������������������������������������������������������������������������������������� 117 vii Table of Contents Chapter 13: Generics 121 Parametric Polymorphism��������������������������������������������������������������������������������������������������������� 121 Class Generics��������������������������������������������������������������������������������������������������������������������� 122 Method Generics����������������������������������������������������������������������������������������������������������������� 123 Stack Example��������������������������������������������������������������������������������������������������������������������� 123 Bounded Classes����������������������������������������������������������������������������������������������������������������������� 126 Upper Bounds ()������������������������������������������������������������������������������������������� 127 Lower Bounds ()���������������������������������������������������������������������������������������������� 128 Wildcard Bounds (

Ngày đăng: 30/12/2020, 15:35

TỪ KHÓA LIÊN QUAN