| Scala | Docs | API | Intro | Getting Started | |
Scala is a multi-paradigm language mixining OO with functional aspects.
Martin
Wrote the 1.3 java compiler
Scala wrote functional language whilst staying true to the Java roots...
Ted
As much a paradigm shift from proceedural to OO was. Significantly different way you think about design.
Still fall back to Java OO tricks
Functional mindset
Rooted in mathematical
laurenso Churck - Lambda calculus
functional as in mathematics notatiion of function
for every x, there is a corresponding y - this implies no side efects
1+1 =
1x1 =
These can be combined to one function
f(x) = x + x
f(x) = x x x
Simplify to...
f(x,f)= x f x
Passing in a value of x that value never changes - its immutable
Notion of equality so we can do substitution
x + y = 5
x - y = 1
subtitution
x = 1+ y
(1+y ) + y = 5
we can now solve this
In java code
x = x +1 -- this means in terms of math that if x is 4 then 4 = 5 - so this is an imperitive language - chaning the order of steps causes problems
Intel and virtual machines would love to do things in a different order to get more efficientcy
Every chip has a memory map that tells you what you can do in terms of re-writes.
Similarity between a fucntional language and a spreadsheet - although there are some dependancies the order does not matter outside of those cell dependancies - no side effect
Imperitive language has side effects - told to print stuff to the console - hello world - this is a side effect - the h has to come before the e..
In a pure functional language the language ...
same input means same output - not always true in the java space...
Unit tests are there to also pick up these side affects
With fucntional, if there is not state that is changing, then there is no need to test for that kind of aspect
Functional has lazyness - not computing values until
Haskell - infinate lists - no terminating edge - haskell knows how to manufacture the next element so can do so in a Lazy way
Java is eager, we have to have the list before we can do anything
Concepts
Functions as first class values
- gigest differentater - can I pass a block of code around as a function
- can do this as Java - eg. runnable inner class - this is painful - Java 8 will have closures
- this is where a lot of language design is
Strongly typed, type-inferenced
The compiler can go to great lengths to fingure out what type something should be.
In java you have to put the type String in front of s = "John";
Scala - if I can figure out what type it is, I will do that - 80% of the time scala can figure it out - type inferences
Scala compiler can infer code that is more generic that you intedned it to be - scala can figure out generic types
Immutable values
Things you cannot change, as if things are defined as final
Scala is an impure hybrid OO/functional language - a fusion - Odersky
Expressions not statements
Everything yeilds a value
In Java we have statements.. dont return value
if (a)
b;
else
(c);
Expression...
result = a?b:c;
Very few things in Scala dont yeild a value... ocasionally you will get weird messages from the compiler if this is not considered
Tupples Lists
A built in construct, representing one or more fields represented by order
Haskell has got by with tupples
In scala you can use tupples instead of objects
A tupple is just data -
(a, 1, 2.0)
(a, ()=>, ...
Typically you dont have functions within a tupple, you would instead have a class
Lists are typically immutable - lightweight than arrays
Recursion
Lists work better because of recursion
Pattern matching
Looks like a switch case but has dozens of varieties.. can use to replace all your if, when, do, etc..
Partial application / Curring of functions
any functions taking parameters can be yeilded down to one function taking a function as a parameter
take small snippets of behavior and string them together
f(x,y) = x+ y
f(5,y) => f(y)
f(x,y,z)
f(x,y)
f(x)