Download Scala API

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
The Scala API
Application Programmer’s Interface
Reinventing the wheel

When you learn a foreign language, you learn both the grammar
(syntax) and the vocabulary



If you learn just a few hundred words, you can usually make yourself
understood (though you may not understand other people), but that doesn’t
make you fluent in the language
If you were writing in the foreign language, you would use a dictionary to
express yourself better
When you learn a programming language, you learn both the
syntax and the libraries (“vocabulary”)

You can write (almost) any program using only the syntax



if (me == "Tarzan") you = "Jane"
You are not “speaking” Scala, you are writing it
Learn to use the libraries!
2
Converting to lowercase

scala> :paste
// Entering paste mode (ctrl-D to finish)
val s = "Letters AND digits, 1 2 3!"
var lc = ""
for (ch <- s) {
if (ch >= 'A' && ch <= 'Z') {
lc = lc + (ch - 'A' + 'a').toChar
} else {
lc = lc + ch
}
}
(Entered ctrl-D)
// Exiting paste mode, now interpreting.
s: String = Letters AND digits, 1 2 3!
lc: String = letters and digits, 1 2 3!

scala> val lc2 = s.toLowerCase
lc2: String = letters and digits, 1 2 3!
3
Discovering methods in the REPL

In the REPL, you can hit a tab after a period to find out what methods are available


asInstanceOf
codePointBefore
compareToIgnoreCase
contentEquals
getBytes
intern
lastIndexOf
offsetByCodePoints
replaceAll
startsWith
toCharArray
toUpperCase
charAt
codePointCount
concat
endsWith
getChars
isEmpty
length
regionMatches
replaceFirst
subSequence
toLowerCase
trim
Or you can type a few letters after the period, then hit tab:


scala> "abc".TAB
+
codePointAt
compareTo
contains
equalsIgnoreCase
indexOf
isInstanceOf
matches
replace
split
substring
toString
scala> "abc".toTAB
toCharArray
toLowerCase
toString
toUpperCase
Notice the + in the first list above
4
Operators and methods



Operators are things like +, *, <=, &&, etc.
In Scala, operators are really methods, and are in the API along with all the
other methods
Here’s a neat trick: If a method takes exactly one argument (in addition to the
object you are talking to), you can use it like a binary operator




scala> 5.max(7)
res0: Int = 7
scala> 5 max 7
res1: Int = 7
You can use this trick to save typing and make your code more readable
Less usefully, you can treat binary operators as methods


scala> 5 + 7
res2: Int = 12
scala> 5 .+(7)
res3: Int = 12
The Scala API

Scala comes with a LARGE library of classes!


Scala can also use the classes that come with Java



Each class may contain many, many methods
Java also provides lots of classes and methods
Altogether there are thousands of methods you can use!
The Scala API (Application Programmer’s Interface) is a large
collection of web pages that tells you what classes and methods
are available


Access it online at http://www.scala-lang.org/api/current/
To have it available when you are not online, download the API from
http://www.scala-lang.org/download/2.10.2.html
6
Using the Scala API

Here’s the good news:


Just about any common task you can think of, the wizards at Sun,
Oracle, and Typesafe have already written (and debugged!) it for you
Here’s the bad news:

It’s huge! It’s scary! It’s intimidating!





No, really. It is.
Class, object, and method descriptions often use mysterious words and
syntax that you don’t yet understand
Some of what you want isn’t in the Scala API, it’s in the Java API
Some methods are poorly documented or not documented at all
Nevertheless, you have to learn to use the API at least a little
bit
7
This is where you start
8
Enter a class/object name to go there
9
Difference between “C” and “O”




Numbers have types: 5 is an Int, 5.0 is a Double
Types are defined by classes
5.0 is an object of the class Double
The methods in “C” are the methods that can be applied to individual
Doubles, because they belong to the class Double


Double is itself an object


scala> 5.75.round
res5: Long = 6
It isn’t a number, it’s an object that describes certain kinds of numbers—those
we think of as “doubles”
The methods in “O” are those that can be applied to Double itself

scala> Double.MaxValue
res7: Double = 1.7976931348623157E308
Scroll down to find what you need
11
Don’t panic!

Using the Scala API can be intimidating



It’s big! And a lot of it is confusing or incomprehensible
The trick is to ignore everything you don’t understand
Getting comfortable with the API is an essential part of
learning to program in Scala (or any other language)


The code in the libraries has been written by experts
Using the library methods makes your code simpler and
easier to read


scala> val absrad = if (rad < 0) -rad else rad
absrad: Double = 1.5707963267948966
scala> val absrad = rad.abs
absrad: Double = 1.5707963267948966
Using the Java API
13
Where to look

The following packages are imported by default; you don’t have
to do anything:

scala._


(meaning everything from the scala package)
Contains operations on numbers and various other things
java.lang._

(meaning everything from the java.lang package)
Most useful for the operations on strings

These packages are imported by default because you will need
them in virtually every program

Later on, you may find you need to important certain other
packages


For example, if you need trigonometric functions, import scala.math
If you want to write a graphical user interface, see scala.swing
14
The End
15