Coder Social home page Coder Social logo

scalabook's Introduction

Programming in Scala, 4th Edition

Code snippets and notes from "Programming in Scala, 4th Edition" by M. Odersky, L. Spoon & B. Venners

Chapter 2: First Steps in Scala

Step 1: The scala interpretor:

18:06:32 ✔ ~/Github/origin/scala4ED (master) :: scala
Welcome to Scala 2.13.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_144).
Type in expressions for evaluation. Or try :help.

scala> 1 + 2
res0: Int = 3

scala> res0 + 10
res1: Int = 13

Step 2: Define some variables

scala> val msg = "Hello, World!"
msg: String = Hello, World!

The type of msg is java.lang.String, becuase Scala strings are implemented by Java's String class

Note the type inference.

scala> val msg2: java.lang.String = "Hello again, World!"
msg2: String = Hello again, World!

scala> val msg3: String = "Hello, yet again, World!"
msg3: String = Hello, yet again, World!

scala> println(msg)
Hello, World!

scala> msg = "Goodbye cruel world!"
           ^
       error: reassignment to val

Multi-line:

scala> val multiLine =
     | "This is the next line."
multiLine: String = This is the next line.

scala> val blankLines =
     |
     |
You typed two blank lines.  Starting a new command.

Step 3: Define some functions

scala> def max(x: Int, y: Int): Int = {
     | if (x > y) x
     | else y
     | }
max: (x: Int, y: Int)Int

scala> max(3, 5)
res3: Int = 5

scala> def greet() = println("Hello, World!")
greet: ()Unit

When you define the greet() function, the interpretor will respond with greet: ()Unit. "greet" is, of course, the name of the function. The empty parentheses indicate the function takes no parameters. And Unit is greet'sresult type. A result type of Unit indicates the function returns no interesting value. Scala's Unit type is similar to Java's void type; in fact, every void returning method in Java is mapped to a Unit-returning method in Scala. Methods with the result type of Unit, therefore are only executed for the side effects. In the case of greet(), the side effect is a friendly greeting printed to standard output.

scala> :quit

Step 4: Write some scala scripts

11:12:39 ✔ ~/Github/origin/scala4ED (master) :: cat ./src/main/scala/chapter01/hello.scala
println("Hello, World!, from a script")

11:12:19 ✘ ~/Github/origin/scala4ED (master) :: scala ./src/main/scala/chapter01/hello.scala
Hello, World!, from a script

Step 5: Loop with while; decide with if

Chapter 3: Next Steps in Scala

scalabook's People

Contributors

tallamjr avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.