Learning Scala in 10 Minutes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Learning Scala in 10 Minutes

maciek.makowski
Hi all,

I have been asked to provide a 10-minute introduction to Scala to the
participants of a coding dojo -- an hour-long session in which people
with no prior exposure to Scala will attempt to code a simple app in
this language. The audience will be experienced Java developers,
familiar with Ruby and aware of FP. As the intro time is very limited
I was thinking of just providing examples of more or less idiomatic
Scala code that would highlight features of the language that could
prove useful for the task at hand (which is implementing Conway's Game
of Life). Now, the issue with this is that being a Scala newbie myself
I'm not exactly sure the examples I prepared represent "idiomatic
Scala code" and promote the correct usage of the features of the
language, and that's why I'm seeking your feedback.

I came up with the following two tiny apps:

http://gist.github.com/379028
http://gist.github.com/379031

The features I would like to discuss are outlined in the comments
below the code. Do you think these examples make sense or can they be
improved? Is there a feature you think I should definitely mention in
that intro and which is currently missing?

Thanks,
Maciek
Reply | Threaded
Open this post in threaded view
|

Re: Learning Scala in 10 Minutes

Daniel Sobral
You may be interested in this: http://github.com/dcsobral/ConwayLife

Now, 10-minutes intro to Scala is... tough as hell. I'd start with heavy emphasis on Java->Scala conversions:

* import statements, "_" as wildcard, A => B as renaming
* package statements, package visibility/scope rules
* Put type after, not before
* Rely on type inference to ommit types outside method declarations
* class declaration and body make up default constructor
* def to declare methods
* last expression in a block is it's return value -- prefer that to explicit return statements
* if returns a value; try/catch returns a value
* val as final field + getter
* var as getter/setter
* fields are hidden
* always extends, never implements
* traits as interfaces, with keyword
* traits as interfaces + implementation
* Type parameters. Mention <: and >: for upper and lower bounds
* AnyVal and primitives as classes, Any vs AnyRef, autoboxing
* Unit vs void
* Null and Nothing as bottom types of AnyRef and Any
* Mention + and - briefly as co-variance and contra-variance signs -- say if they know what it means, that's fine, if they don't, just don't worry about it.

I'd then define a function in a strictly Object Oriented way: as instances of FunctionN. Explain the "apply" syntactic sugar, (A, B) => C syntactic sugar and { (x, y) => ... } function literal. Mention that () may be replaced with {} if the sole parameter is of type Function.

Then introduce for/yield statements comparing them to Ruby's list comprehensions. Briefly explain flatMap/map/filter (withFilter on 2.8) translation -- just show the code and tell them to figure it out. :-) Mention for without yield and its foreach translation.

Mention update() and x_=() syntactic sugars. Be sure to mention the latter requires a x getter as well.

Finally, introduce x match { case ... } as simple switch statement. Show how try/catch uses it as switch on types.

If time allows, explain case class/case object as a collection of factory, equals, hashCode, automatic "val" on parameters, plus a "deconstructor", which can be used on pattern matches.

I have trouble believing it can be done in 10 minutes. It is indeed better to produce code with all these things, and point them out.


On Mon, Apr 26, 2010 at 6:23 PM, <[hidden email]> wrote:
Hi all,

I have been asked to provide a 10-minute introduction to Scala to the
participants of a coding dojo -- an hour-long session in which people
with no prior exposure to Scala will attempt to code a simple app in
this language. The audience will be experienced Java developers,
familiar with Ruby and aware of FP. As the intro time is very limited
I was thinking of just providing examples of more or less idiomatic
Scala code that would highlight features of the language that could
prove useful for the task at hand (which is implementing Conway's Game
of Life). Now, the issue with this is that being a Scala newbie myself
I'm not exactly sure the examples I prepared represent "idiomatic
Scala code" and promote the correct usage of the features of the
language, and that's why I'm seeking your feedback.

I came up with the following two tiny apps:

http://gist.github.com/379028
http://gist.github.com/379031

The features I would like to discuss are outlined in the comments
below the code. Do you think these examples make sense or can they be
improved? Is there a feature you think I should definitely mention in
that intro and which is currently missing?

Thanks,
Maciek



--
Daniel C. Sobral

I travel to the future all the time.
Reply | Threaded
Open this post in threaded view
|

Re: Learning Scala in 10 Minutes

Michael Dürig
In reply to this post by maciek.makowski
 <maciek.makowski@...> writes:

>
> Hi all,
>
> I have been asked to provide a 10-minute introduction to Scala to the
> participants of a coding dojo -- an hour-long session in which people
> with no prior exposure to Scala will attempt to code a simple app in
> this language.

Well 10 minutes might be too short to cover all but I found 'A Tour of Scala'
very valuable: http://www.scala-lang.org/node/104

Michael



Reply | Threaded
Open this post in threaded view
|

Re: Learning Scala in 10 Minutes

maciek.makowski
Thanks for all the responses -- both on the list and private -- they
were helpful. I think I will stick to the examples which I prepared --
my objective is not giving a complete overview of the language but
just providing the dojo participants with tools that will help them
solve a particular task. As long as these examples are not
fundamentally wrong it should be enough for that session.

On Mon, Apr 26, 2010 at 11:52 PM, michid <[hidden email]> wrote:

>  <maciek.makowski@...> writes:
>
>>
>> Hi all,
>>
>> I have been asked to provide a 10-minute introduction to Scala to the
>> participants of a coding dojo -- an hour-long session in which people
>> with no prior exposure to Scala will attempt to code a simple app in
>> this language.
>
> Well 10 minutes might be too short to cover all but I found 'A Tour of Scala'
> very valuable: http://www.scala-lang.org/node/104
>
> Michael
>
>
>
>