The Scala API Application Programmers Interface Reinventing the

  • Slides: 15
Download presentation
The Scala API Application Programmer’s Interface

The Scala API Application Programmer’s Interface

Reinventing the wheel n When you learn a foreign language, you learn both the

Reinventing the wheel n When you learn a foreign language, you learn both the grammar (syntax) and the vocabulary n n n 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”) n You can write (almost) any program using only the syntax n n n if (me == "Tarzan") you = "Jane" You are not “speaking” Scala, you are writing it Learn to use the libraries! 2

Converting to lowercase n scala> : paste // Entering paste mode (ctrl-D to finish)

Converting to lowercase n 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'). to. Char } 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! n scala> val lc 2 = s. to. Lower. Case lc 2: String = letters and digits, 1 2 3! 3

Discovering methods in the REPL n In the REPL, you can hit a tab

Discovering methods in the REPL n In the REPL, you can hit a tab after a period to find out what methods are available n n as. Instance. Of code. Point. Before compare. To. Ignore. Case content. Equals get. Bytes intern last. Index. Of offset. By. Code. Points replace. All starts. With to. Char. Array to. Upper. Case char. At code. Point. Count concat ends. With get. Chars is. Empty length region. Matches replace. First sub. Sequence to. Lower. Case trim Or you can type a few letters after the period, then hit tab: n n scala> "abc". TAB + code. Point. At compare. To contains equals. Ignore. Case index. Of is. Instance. Of matches replace split substring to. String scala> "abc". to. TAB to. Char. Array to. Lower. Case to. String to. Upper. Case Notice the + in the first list above 4

Operators and methods n n n Operators are things like +, *, <=, &&,

Operators and methods n n n 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 n n scala> 5. max(7) res 0: Int = 7 scala> 5 max 7 res 1: 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 n n scala> 5 + 7 res 2: Int = 12 scala> 5. +(7) res 3: Int = 12

The Scala API n Scala comes with a LARGE library of classes! n n

The Scala API n Scala comes with a LARGE library of classes! n n Scala can also use the classes that come with Java n n n Each class may contain many, many methods Java also provides lots of classes and methods Altogethere 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 n n 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 n Here’s the good news: n n Just about any

Using the Scala API n Here’s the good news: n n 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: n It’s huge! It’s scary! It’s intimidating! n n n 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

This is where you start 8

Enter a class/object name to go there 9

Enter a class/object name to go there 9

Difference between “C” and “O” n n Numbers have types: 5 is an Int,

Difference between “C” and “O” n n 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 n n Double is itself an object n n scala> 5. 75. round res 5: 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 n scala> Double. Max. Value res 7: Double = 1. 7976931348623157 E 308

Scroll down to find what you need 11

Scroll down to find what you need 11

Don’t panic! n Using the Scala API can be intimidating n n n It’s

Don’t panic! n Using the Scala API can be intimidating n n n 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) n n The code in the libraries has been written by experts Using the library methods makes your code simpler and easier to read n n scala> val absrad = if (rad < 0) -rad else rad absrad: Double = 1. 5707963267948966 scala> val absrad = rad. absrad: Double = 1. 5707963267948966

Using the Java API 13

Using the Java API 13

Where to look n The following packages are imported by default; you don’t have

Where to look n The following packages are imported by default; you don’t have to do anything: n scala. _ n n n Contains operations on numbers and various other things java. lang. _ n n (meaning everything from the scala package) (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 n n 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

The End 15