Skip to content

Commit

Permalink
lessons: formatting, some nits, mostly: fmt -l10000000
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusae committed Aug 24, 2011
1 parent fe07a9c commit e2b5277
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 417 deletions.
42 changes: 12 additions & 30 deletions web/_posts/2011-05-01-lesson.textile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ layout: post
desc: Values, functions, classes, methods, inheritance, try-catch-finally. Expression-oriented programming
---

h1. Lesson 1

Today we'll cover:
This lesson covers:
* values
* expressions
* functions
Expand All @@ -18,14 +16,11 @@ Today we'll cover:

h2. About this class

The first few weeks will cover basic syntax and concepts. Then we'll start
to open it up with more exercises.
The first few weeks will cover basic syntax and concepts. Then we'll start to open it up with more exercises.

Some examples will be given as if written in the interpreter, others
as if written in a source file.
Some examples will be given as if written in the interpreter, others as if written in a source file.

Having an interpreter available makes it easy to explore a problem
space.
Having an interpreter available makes it easy to explore a problem space.


h2. Why Scala?
Expand All @@ -50,8 +45,7 @@ h2. How Scala?

h2. Think Scala

Scala is not just a nicer Java. You should learn it with a fresh mind,
you will get more out of these classes.
Scala is not just a nicer Java. You should learn it with a fresh mind, you will get more out of these classes.

h2. Start the Interpreter

Expand All @@ -73,11 +67,9 @@ scala> 1 + 1
res0: Int = 2
</pre>

res0 is an automatically created value name given by the interpreter
to the result of your expression. It has the type Int and contains the
Integer 2.
res0 is an automatically created value name given by the interpreter to the result of your expression. It has the type Int and contains the Integer 2.

Everything in Scala is an expression.
(Almost) everything in Scala is an expression.

h2. Values

Expand All @@ -97,9 +89,7 @@ scala> def addOne(m: Int): Int = m + 1
addOne: (m: Int)Int
</pre>

In Scala, you need to specify the type signature for function
parameters. The interpreter happily repeats the type signature back to
you.
In Scala, you need to specify the type signature for function parameters. The interpreter happily repeats the type signature back to you.

<pre>
scala> val three = addOne(2)
Expand Down Expand Up @@ -151,16 +141,11 @@ scala> calc.brand
res2: String = "HP"
</pre>


Contained are examples are defining methods with def and fields with
val. methods are just functions that can access the state of the class.
Contained are examples are defining methods with def and fields with val. methods are just functions that can access the state of the class.

h4. Constructor

Constructors aren't special methods, they are the code outside of
method definitions in your class. Let's extend our Calculator example
to take a constructor argument and use it to initialize internal
state.
Constructors aren't special methods, they are the code outside of method definitions in your class. Let's extend our Calculator example to take a constructor argument and use it to initialize internal state.

<pre>
class Calculator(brand: String) {
Expand Down Expand Up @@ -201,9 +186,7 @@ class EvenMoreScientificCalculator(brand: String) extends ScientificCalculator(b

h3. Expressions

Our BasicCalculator example gave an example of how Scala is
expression-oriented. The value color was bound based on an if/else
expression.
Our BasicCalculator example gave an example of how Scala is expression-oriented. The value color was bound based on an if/else expression.

h3. try-catch-finally

Expand Down Expand Up @@ -234,7 +217,6 @@ val result: Int = try {
}
</pre>

This is not an example of excellent programming style, just an example
of expressions in action. Better would be to re-throw.
This is not an example of excellent programming style, just an example of expressions in action. Better would be to re-throw.

Finally will be called after any exception has been handled.
54 changes: 14 additions & 40 deletions web/_posts/2011-05-02-lesson.textile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ layout: post
desc: Case classes, objects, packages, apply, update, Functions are Objects (uniform access principle), pattern matching.
---

h1. Lesson 2

Today we'll cover:
This lesson covers:
* apply
* objects
* case classes
Expand Down Expand Up @@ -63,12 +61,9 @@ scala> Timer.currentCount
res0: Long = 1
</pre>

Classes and Objects can have the same name. The object is called a 'Companion Object'. We commonly use Companion Objects for Factories.

Classes and Objects can have the same name. The object is called a
'Companion Object'. We commonly use Companion Objects for Factories.

Here is a trivial example that only serves to remove the need to use
'new' to create an instance.
Here is a trivial example that only serves to remove the need to use 'new' to create an instance.

<pre>
class Bar(foo: String)
Expand All @@ -80,8 +75,7 @@ object Bar {

h3. Case Classes

case classes are used to conveniently store data with a class. You can
construct them without using new.
case classes are used to conveniently store data with a class. You can construct them without using new.

<pre>
scala> case class Bottle(color: String)
Expand All @@ -96,8 +90,7 @@ res1: String = Blue

You will often see them used in Twitter code as record types.

case classes automatically have equality and nice toString methods
based on the constructor arguments.
case classes automatically have equality and nice toString methods based on the constructor arguments.

<pre>
scala> val blue = Bottle("Blue")
Expand All @@ -113,17 +106,13 @@ scala> blue.equals(blu)
res1: Boolean = true
</pre>


case classes can have methods.

h3. Functions are Objects

In Scala, we talk about object-functional programming often. What does
that mean?
In Scala, we talk about object-functional programming often. What does that mean?

We saw earlier that giving your class an apply method you can use it
like it's a function. Let's look in the other direction. Let's define
a simple function and see what's under the hood.
We saw earlier that giving your class an apply method you can use it like it's a function. Let's look in the other direction. Let's define a simple function and see what's under the hood.

<pre>
def addOne(x: Int) = x + 1
Expand All @@ -139,8 +128,7 @@ scala> addOne _
res1: (Int) => Int = <function1>
</pre>

Hmm, what is a function1? A Function1 is simply a trait for a function
that takes one argument.
Hmm, what is a function1? A Function1 is simply a trait for a function that takes one argument.

<pre>
scala> val addOne = new Function1[Int, Int] {
Expand All @@ -152,20 +140,11 @@ scala> addOne(1)
res2: Int = 2
</pre>

There is Function1 through 22. Why 22? It's an arbitrary magic
number. I've never needed a function with more than 22 arguments so it
seems to work out.
There is Function1 through 22. Why 22? It's an arbitrary magic number. I've never needed a function with more than 22 arguments so it seems to work out.

The syntatic sugar of apply helps unify the duality of object and
functional programming. You can pass classes around and use them as
functions and functions are just instances of classes under the
covers.

Does this mean that everytime you define a method in your class,
you're actually getting an instance of Function*? No, methods in
classes are methods. methods defined standalone in the repl are
Function* instances.
The syntatic sugar of apply helps unify the duality of object and functional programming. You can pass classes around and use them as functions and functions are just instances of classes under the covers.

Does this mean that everytime you define a method in your class, you're actually getting an instance of Function*? No, methods in classes are methods. methods defined standalone in the repl are Function* instances.

h3. Packages

Expand All @@ -177,8 +156,7 @@ package com.twitter.example

at the top of a file will declare everything in the file to be in that package.

Values and functions cannot be outside of a class or object. Objects
are a useful tool for organizing static functions.
Values and functions cannot be outside of a class or object. Objects are a useful tool for organizing static functions.

<pre>
package com.twitter.example
Expand All @@ -205,10 +183,7 @@ scala> object colorHolder {
defined module colorHolder
</pre>

This gives you a small hint that the designers of Scala designed
objects to be part of Scala's module system.


This gives you a small hint that the designers of Scala designed objects to be part of Scala's module system.

h3. Pattern Matching

Expand Down Expand Up @@ -238,8 +213,7 @@ times match {

Notice how we captured the value in the variable 'i'.

case classes are designed to be used with pattern matching. Let's
expand on our bottle example from earlier.
case classes are designed to be used with pattern matching. Let's expand on our bottle example from earlier.

<pre>
case class Bottle(color: String, ounces: Int)
Expand Down
39 changes: 12 additions & 27 deletions web/_posts/2011-05-03-lesson.textile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ layout: post
desc: Lists, Maps, functional combinators (map, foreach, filter, zip, folds)
---

h1. Lesson 3

Today we'll cover:
This lesson covers:
* Lists
* Maps
* Sets
Expand Down Expand Up @@ -171,8 +169,7 @@ res0: scala.collection.immutable.Set[Int] = Set(1, 2)

h3. Seq

Sequences have a defined order, they are Lists under the covers but
many APIs will use the more generic Seq as their return type.
Sequences have a defined order, they are Lists under the covers but many APIs will use the more generic Seq as their return type.

<pre>
scala> Seq(1, 1, 2)
Expand All @@ -189,8 +186,7 @@ scala> val hostPort = ("localhost", 80)
hostPort: (java.lang.String, Int) = (localhost, 80)
</pre>

Unlike case classes, they don't have named accessors, instead they
have accessors that are named by their position (1-based).
Unlike case classes, they don't have named accessors, instead they have accessors that are named by their position (1-based).

<pre>
scala> hostPort._1
Expand All @@ -211,16 +207,13 @@ hostPort match {

h2. Functional Combinators

This fancy name is giving to a standard set of functions that can be
applied to Lists and Maps.
This fancy name is giving to a standard set of functions that can be applied to Lists and Maps.

They are called combinators because they are meant to be combined. the
output of one function is often suitable as the input for another.
They are called combinators because they are meant to be combined. the output of one function is often suitable as the input for another.

h4. map

Evaluates a function over each element in the list, returning a list
with the same number of elements.
Evaluates a function over each element in the list, returning a list with the same number of elements.

<pre>
scala> numbers.map((i: Int) => i * 2)
Expand Down Expand Up @@ -257,9 +250,7 @@ doubled: Unit = ()

h4. filter

removes any elements where the function you pass in evaluates to
false. Functions that return a Boolean are often called predicate
functions.
removes any elements where the function you pass in evaluates to false. Functions that return a Boolean are often called predicate functions.

<pre>
scala> numbers.filter((i: Int) => i % 2 == 0)
Expand All @@ -285,8 +276,7 @@ res0: List[(Int, java.lang.String)] = List((1,a), (2,b), (3,c))

h4. partition

partition splits a list based on where it falls with respect to a
predicate function.
partition splits a list based on where it falls with respect to a predicate function.

<pre>
scala> List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).partition((i: Int) => i > 5)
Expand All @@ -311,8 +301,7 @@ scala> numbers.drop(5)
res0: List[Int] = List(6, 7, 8, 9, 10)
</pre>

dropWhile removes elements that don't match a predicate function. Here
we can re-implement drop.
dropWhile removes elements that don't match a predicate function. Here we can re-implement drop.

<pre>
scala> numbers.dropWhile((i: Int) => i < 6)
Expand Down Expand Up @@ -368,13 +357,11 @@ res0: Int = 55

h4. Generalized functional combinators

Now we've learned a grab-bag of functions for working with
collections.
Now we've learned a grab-bag of functions for working with collections.

What we'd like is to be able to write our own functional combinators.

Interestingly, every functional combinator shown above can be written
on top of fold. Let's see some examples.
Interestingly, every functional combinator shown above can be written on top of fold. Let's see some examples.

<pre>
def ourMap(numbers: List[Int], fn: Int => Int): List[Int] = {
Expand All @@ -391,9 +378,7 @@ Why <tt>List[Int]()</tt>? Scala wasn't smart enough to realize that you wanted a

h3. Map?

All of the functional combinators shown work on Maps, too. Maps can be
thought of as a list of pairs so the functions you write work on a
pair of the keys and values in the Map.
All of the functional combinators shown work on Maps, too. Maps can be thought of as a list of pairs so the functions you write work on a pair of the keys and values in the Map.

<pre>
scala> val extensions = Map("steve" -> 100, "bob" -> 101, "joe" -> 201)
Expand Down
20 changes: 6 additions & 14 deletions web/_posts/2011-05-04-lesson.textile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ desc: More functions! PartialFunctions, more Pattern Matching
---


h1. Lesson 4

h2. Advanced Pattern Matching and Functional Composition

Today we'll cover
This lesson covers:

* Functions (again)
** compose
Expand Down Expand Up @@ -143,14 +141,11 @@ scala> stevej match {
case User("stevej", _, _) => println("it's stevej")
</pre>

But what if we don't want User to be a case class? How can we make
User interact nicely with pattern matching without changing the User
class?
But what if we don't want User to be a case class? How can we make User interact nicely with pattern matching without changing the User class?

Use the extractor method: unapply.

Make a companion object with an unapply method. The unapply should
return an Option with a Tuple that matches your class' constructor.
Make a companion object with an unapply method. The unapply should return an Option with a Tuple that matches your class' constructor.

<pre>
object User {
Expand Down Expand Up @@ -185,14 +180,11 @@ They are multiple PartialFunctions composed together.

h3. Understanding PartialFunction

A function closes over an entire domain. In other words, a function
defined as (Int) => String takes any Int and returns a String.
A function closes over an entire domain. In other words, a function defined as (Int) => String takes any Int and returns a String.

A Partial Function is only partially closed over a domain. A Partial
Function (Int) => String might not accept every Int.
A Partial Function is only partially closed over a domain. A Partial Function (Int) => String might not accept every Int.

isDefinedAt is a method on PartialFunction that can be used to
determine if the PartialFunction will accept a given argument.
isDefinedAt is a method on PartialFunction that can be used to determine if the PartialFunction will accept a given argument.

<pre>
scala> val one: PartialFunction[Int, String] = { case 1 => "one" }
Expand Down
Loading

0 comments on commit e2b5277

Please sign in to comment.