bootstrapping a scala mindset (scala exchange 2014)

Download Bootstrapping a Scala Mindset (Scala eXchange 2014)

Post on 15-Jul-2015

794 views

Category:

Software

6 download

Embed Size (px)

TRANSCRIPT

Slide 1

Photo by ColorblindRain - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/11594851@N00Created with Haiku Deck

1

Photo by Zach Dischner - Creative Commons Attribution License https://www.flickr.com/photos/35557234@N07Created with Haiku Deck

Scala is a Cliff thats my quote no-one other than me said it, but Chad Fowler got pretty close:

"Scala is difficult to learn he wrote in his forward to Scala in Action, from 2013

Scala is a lot to chew on. Its got what seems way too many features.2

Photo by Andr Hofmeister - Creative Commons Attribution-NonCommercial License https://www.flickr.com/photos/54786327@N00Created with Haiku Deck

He continued:

Youre going to hurt yourself along the way

[However] its these jagged edges that improve us. If you approach this climb properly, youll reach the top sharper, more open-minded, and, best of all, less afraid.

I wish Id paid more attention to this when I started learning Scala.

This presentation pretty much deals with how I learned this the hard way.

3

* Senior Java Dev / Dev Lead* 5 or so years ago I Learned Ruby (ruby-eye-for-the-java-guy.blogspot.com) to get some DynaLang in my life (and done some Groovy-fiddling)* For the past few years Ive been Learning Scala to get some Functional in my life

As well as this I:* Stopped studying Maths at High School (Age 16 yrs)* And at university too the easy option and studied Psychology at University (fluffy!)4

Photo by Leo Reynolds - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/49968232@N00Created with Haiku Deck

So given that, and assuming youre not totally put off, whats my aim here?

By telling you things I learned, I can reduce your pain (N.b. Itll still hurt)

How?

* By mapping out the key stages involved in my moving from a Java to the beginnings of a Scala mindset

* And by telling you some things few folks remember to tell you

5

Photo by Guergui - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/49722723@N00Created with Haiku Deck

And what do you get if you follow this path?

Rather facetiously Im gonna claim that soon youll be using Monadic constructs without even feeling special.

But what this really means is youll have a better foundation mental model of the Scala language

Which will allow you launch into the next challenges the language presents

as you encounter more and more of its richness.6

Photo by Infidelic - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/87472505@N00Created with Haiku Deck

So are we ready? Lets go.

Part 1: Everything you know is wrong in which we learn just how far Andrew had to go when he started

7

Photo by gruntzooki - Creative Commons Attribution-ShareAlike License https://www.flickr.com/photos/37996580417@N01Created with Haiku Deck

First up, confession time. I was so clueless about Scala when I started I even had a misconception around the name:

IS:* Scales nicely - Play and Reactive Programming* Parallelises well - Immutability & Actors* More productive - High Level so much less boiler plate* More team members - Statically Typed -> Refactorable

BUT that's not where the name comes from

8

Photo by Peter Rivera - Creative Commons Attribution License https://www.flickr.com/photos/13057030@N00Created with Haiku Deck

In reality, it turns out that Scala is so named because it grows with the demands of its users (Programming in Scala 2nd Ed.)

Its layered Scala is much more like a bazaar than a cathedral (Programming in Scala 2nd Ed.)

Its designed to be extended and adapted, in that its Scala all the way down, via libraries that nevertheless feel native.

Consequently, you can apply it to a wide range of tasks. But you can also (must) bootstrap your way in.

What do I mean by all this? Patience patience, well soon see.

But first, more misconceptions.

9

The next thing to bear in mind is that, whether you Like it or not: There Will Be Math(s)

That's fine. But what you need to consider is that this maths is EVERYWHERE:

* It inspired the much terser, and mathematical-idiom driven syntax* It means things are in the wrong place, such as something as fundamental as the parameter types and return type on method signatures - because they're in the "right place" for mathematical notation* and they expect operator overloading (i.e. more weird looking code)* And maths folks use a lot of chaining so youll see a lot of that

In short, you're stumbling into an entirely different world

And thats the first reason why I claim everything you know is wrong everything I knew certainly was.

Or it felt like that at one point.10

Theres a reason for all this maths Scala is Functional and not Imperative. And functional == maths

I need to hammer this home (I wish someone had for me), and also unpick it a little: Scala is Functional not Imperative.

(N.b. the fact its also Object Oriented matters not a jot here.)

So before we complete this section, lets have an overview of the specific aspects Functional compared to Imperative programming:11

Heres what underpins a functional mindset:

1. Consider what information and transformations are required on data (rather than the steps and resulting state changes)2. That's because these state changes are non-existent (or mega-frowned upon)3. Which means that the order of execution is of low importance4. and the focus is primarily on functions and data as the primary manipulation units.

Thats almost diametrically opposed to how I, and I guess you think when you code in Java

(From: http://msdn.microsoft.com/en-GB/library/bb669144.aspx)12

Photo by thepartycow - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/44124327211@N01Created with Haiku Deck

To move to this new mindset, which is essential in order to wrap your head round something like Scala, you must jettison a lot of what you currently hold dear:

* the idiom (the names of things)* the familiar syntax (even the "places of things")* Pretty much your entire way of thinking about code: * No more State / Mutability (hands off!) * Worrying about how things work inside (you don't need to care) * And remembering the order in which they work (ditto) * Theres no more Looping for you ("iterate" / "traverse" instead) * Or functions only hanging off objects (a fundamental OO concept - now they can just float around...)

But its not all bad. If you bear this list in mind as you go you might remember to think Im feeling uncomfortable / experiencing pain; this probably means Im still holding onto an imperative mindset.

Let go.... Learn to let go...

13

Photo by @Doug88888 - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/29468339@N02Created with Haiku Deck

OK, so all we do is stop thinking in an imperative way and start learning to think functionally. Simple right? Sure, I have my list right here

Well no, because the Java-imperative baggage we have with us runs deep.

This means that some stuff about Scala is seemingly so simple when you first come across it youre like, uhuh, got it. Next!

but then you need to build on this and it turns out this dismissive approach didnt prepare you.

Scalas subtleties around things like its support for the Uniform Access Principle and related concepts is one of these key areas.

To examine it well revise the following:

* First up: functions as first-class citizens * Next: everything is an expression"* And finally: infix notation

When you look at them in close proximity you have your first taster of functional programming: look back at ourfour-point lists.:You dont care how something is done, or in which order its done, you just care about the resultThis lets you /let /go of a lot of mental baggage. That's the subtle (but potentially spectacularly large) benefit.

image "rock"

14

Photo by Masa Sakano - Creative Commons Attribution-ShareAlike License https://www.flickr.com/photos/42548354@N04Created with Haiku Deck

Lets start quite slowly. First up, it's reassuring to remember that in Scala, objects are still objects:

In fact I discovered that far from being hard to grasp,

the core class hierarchy in Scala was like a welcome friend

it gets so much more right than Java did: everything is an object

15

Photo by Brianellwood1 - Creative Commons Attribution-NonCommercial-ShareAlike License https://www.flickr.com/photos/92848318@N05Created with Haiku Deck

But did you spot the first subtlety slipping past you there?

"Everything is an object".

Naturally you'd expect this to mean that functions are objects too.

And they are. Lets revise what this means with some code

image "pebbles"

16val v = Vector(1, 2, 3, 4)v.foreach( print("> " + n) )

// from: Atomic Scala v1.1

Heres some not very surprising code: apart from perhaps that foreach is a higher order function which means it takes a function as a parameter.

The print(> + n) is the (anonymous) function being passed to it.17def show(n: Int): Unit = { print("> " + n) }val v = Vector(1, 2, 3, 4)v.foreach(show)

// from: Atomic Scala v1.1And then just pass the new show function-object to foreach.

Beautiful.

Next: E