creative scala

Download Creative Scala

Post on 16-Dec-2015

8 views

Category:

Documents

1 download

Embed Size (px)

DESCRIPTION

ff

TRANSCRIPT

  • ..

    ..

    Creave ScalaDave Gurnell and Noel Welsh

    February 2015

    underscore

    Copyright 2014 Dave Gurnell and Noel Welsh.

  • 2Creave ScalaFebruary 2015

    Copyright 2014 Dave Gurnell and Noel Welsh.

    Published by Underscore Consulng LLP, Brighton, UK.

    Copies of this, and related topics, can be found at hp://underscore.io/training.

    Team discounts, when available, may also be found at that address.

    Contact the author regarding this text at: hello@underscore.io.

    Our courses, workshops, and other products can help you and your team create beer so[ware and have more fun. Formore informaon, as well as the latest Underscore tles, please visit hp://underscore.io/training.

    Disclaimer: Every precauon was taken in the preparaon of this book. However, the author and Underscore Consulng LLPassume no responsibility for errors or omissions, or for damages that may result from the use of informaon (including program

    lisngs) contained herein.

  • Contents

    Foreword 7

    Notes on the Pre-Release Edion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

    Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

    1 Expressions, Values, and Types 9

    1.1 Simple Scala Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

    1.1.1 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

    1.1.2 Method Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

    1.1.3 Constructor Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

    1.1.4 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

    1.1.5 Condionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

    1.1.6 Blocks and Side-Eects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

    1.2 Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

    1.2.1 Primive Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

    1.2.2 Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

    1.2.3 Colour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

    1.3 Take Home Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

    1.3.1 Substuon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

    1.3.2 Types in Scala . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

    2 Declaraons 19

    2.1 Value Declaraons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

    2.2 Method Declaraons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

    2.3 Extended Exercise: Color Palees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

    2.3.1 Color Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

    2.3.2 Analogous Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

    2.3.3 Beyond Two-Color Palees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

    2.4 Take Home Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

    3

  • 4 CONTENTS

    3 (Funconal) Programming 27

    3.1 Recursive Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

    3.2 Funcons as Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

    3.3 Higher Order Methods and Funcons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

    3.4 Take Home Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

    4 Collecons 37

    4.1 Creang Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

    4.2 Transforming Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

    4.3 Take Home Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

    5 Summary 43

    5.1 Representaons and Interpreters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

    5.2 Abstracon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

    5.3 Composion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

    5.4 Expression-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

    5.5 Types are a Safety Net . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

    5.6 Funcons as Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

    5.7 Final Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

    5.8 Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

    A Syntax Quick Reference 47

    A.1 Literals and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

    A.2 Value and Method Declaraons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

    A.3 Funcons as Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

    A.4 Doodle Reference Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

    B Soluons to Exercises 51

    B.1 Expressions, Values, and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

    B.1.1 Soluon to: Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

    B.1.2 Soluon to: Colour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

    B.2 Declaraons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

    B.2.1 Soluon to: Value Declaraons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

    B.2.2 Soluon to: Method Declaraons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

    B.2.3 Soluon to: Complementary Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

    B.2.4 Soluon to: Complementary Colors Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

    B.2.5 Soluon to: Analogous Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

    B.2.6 Soluon to: Analogous Colors Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

    B.2.7 Soluon to: Beyond Two-Color Palees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

    B.3 (Funconal) Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

  • CONTENTS 5

    B.3.1 Soluon to: Recursive Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

    B.3.2 Soluon to: Recursive Algorithms Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

    B.3.3 Soluon to: Higher Order Methods and Funcons . . . . . . . . . . . . . . . . . . . . . . . . . . 55

    B.4 Collecons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

    B.4.1 Soluon to: Transforming Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

  • 6 CONTENTS

  • Foreword

    Creave Scala is aimed at developers who have no prior experience in Scala. It is designed to give you a fun introduconto funconal programming. We assume you have some familiarity with another programming language but lile or noexperience with Scala or other funconal languages.

    Our goal is to demonstrate the building blocks that Scala developers use to create programs in a clear, succinct, declaravemanner. Working through the exercises in the book should take a few hours, a[er which we hope you will have a feel ofwhat Scala can do for your applicaons.

    Although this book will give you the basic mental model required to become competent with Scala, you wont nishknowing everything you need to be self-sucient. For further advancement we recommend considering one of the manyexcellent Scala textbooks out there, including our own Essenal Scala.

    If you are working through the exercises on your own, we highly recommend joining our Gier chat room to provide gethelp with the exercises and provide feedback on the book.

    The text of Creave Scala is open souce, as is the source code for the Doodle drawing library used in the exercises. Youcan grab the code from our Github account. Contact us on Gier or by email if you would like to contribute.

    Thanks for downloading and happy creave programming!

    Dave and Noel

    Notes on the Pre-Release Edion

    WarningThis is an early access release of Creave Scala. This means there are unnished aspects as detailed below. Theremay be typos and errata in the text and examples.

    If you spot any