Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

FP using Scala

No description
by

O Ko

on 8 May 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of FP using Scala

Thank you ! Introduction Functional programming using Scala why should we bother? Functional programming general information FP basics programming language Conclusion that's all folks Moore's law "over the history of computing hardware, the number of transistors on integrated circuits doubles approximately every two years" By 2020 we'll hit eventual limits of transistors miniaturization on atomic levels Horizontal scaling Moore's law is now achieved mostly by increasing number of cores not CPU cycles.
Huge data volumes to process require
horizontal scaling What's in for us ?
The need for writing correct multithreaded code is constantly increasing. Classic concurrent programming manage concurrent execution of multiple threads

mutable state shared by multiple processes

have to sync accesses using locks, semaphores CP problems thread-model with shared mutable data is not deterministic
if 2 threads are doing X+1 on shared X we can't be really sure if 2nd will find X=0 or X=1 hard to code, test and integrate

lots of "weird" situations which are hard to reproduce

deadlocks, race-conditions... Another approach AVOID MUTABLE STATE one of the principles of Functional Programming Actors model

model origins
actor = math model of concurrent computation
lightweight , self-contained
encapsulates actions and data
async communication using messages with immutable data
act in own process space not sharing data popular way to use approach Actors : basic example Actor2 Actor3 ActorN [A] [A|B|...N] Actor1 [B] [N] Actor2 ActorN Actor3 [ResultA] [ResultB] [ResultN] Actor1 async immutable message do something with [Result] data processing split data into chunks Now let's see the bricks of FP which make easy that kind of processing General info programming paradigm, invented in 1930
computation = evaluation of math functions


FP can be effectively combined with OO Let's make a sandwich
or
imperative VS functional approaches Sandwich algo take bread
spread butter on the bread
put cheese on the bread
return sandwich put (cheese, spread (butter, bread) ) Actors model

model origins
actor = math model of concurrent computation
lightweight , self-contained
encapsulates actions and data
async communication using messages with immutable data
act in own process space not sharing data popular way to use approach f(x) : y FP is orthogonal to imperative programming
in IP computation =
sequence of procedures with mutable data Imperative Functional Let's change cheese to sausage createSandwich (lower, middle,upper) bread , butter, sausage Imperative Functional take lower
spread middle on the lower
put upper on the middle
return sandwich put (upper, spread (middle, lower) little abstraction over our elements Let's change "spread" action to "put" createSandwich (lower, middle,upper, mode) bread , butter, sausage, put Imperative take lower
if mode=='put' put middle on the lower
else spread middle on the lower
put upper on the middle
return sandwich we're losing flexibility

we can still
create second function (duplicating functions)
use a callback interface Let's change "spread" action to "put" createSandwich (lower, middle,upper, mode) bread , butter, sausage, put Functional put (upper, action (middle, lower) just another function passed in params higher-order function Functional languages (not exhaustive) JVM Functional -oriented Could be used in functional style What's so good about FP?
simple reasoning principles (functions)
better modularity (integration, testing)
ease of parallelization (for multicore and cloud computing ) elegant, concise, maintainable and efficient code 2001, Martin Odersky at EPFL
(Ecole Polytechnique Fédérale de Lausanne)
current version 2.10
general purpose language
functional & OO in the same time
compiled to JVM bytecode
complete Java compatibility Who's using Scala? FP base lines Avoid state Hidden and implicit state is bad Avoid mutable data => no side effects f(x) called with x=2 should always give the same result (a.k. referential transparency) Function is central unit of work Try to think in functions to resolve a problem Immutables single assignment
one place to look for variable, no global state
produces smaller functions (methods) Persistent data structures easy to reason with in CP
immutable structures
persistent = supports multiple ersions
new values by sharing structure with existing ones
slower than normal ones (but not that much)
scalable on multi-core context Tree-like LinkedList zs = xs++ys Recursion as flow control Tail optimization same method call frame , just parameter substitution Functions are first-class citizens function literal Higher -order functions Functions as return values Currying is the technique of transforming a function that takes multiple arguments in such a way that it can be called as a chain of functions, each with a single argument (partial application) Closures is a function or reference to a function that ‘closes’ over its surrounding scope. so when the closure is executed it still has access to that lexical scope Why it's a bad idea? I hope that this talk has given you a first impression about FP.

I think that FP has potential and we should pay attention to its further development. A lot of interesting topics are not covered in this talk
(pattern matching, lazy evaluation ...)

So If you are still curious about FP and you want to go further in the next section I'll give you some sources to continue with. What's next? FP with Scala Course by EPFL @ Coursera.org https://class.coursera.org/progfun-002/class/index What is the principle which states that
same input should always give the same result? Goody Question function adequacy
function transparency
referential transparency referential transparency Books
Programming in Scala, Second Edition by by Martin Odersky, Lex Spoon, and Bill Venners

Lectures
Functional Programming Principles in Scala by Martin Odersky

@Slideshare.net
http://www.slideshare.net/Leeheng/functional-programming-1795891
http://www.slideshare.net/asolntsev/functional-programming-in-java REFERENCES at least it should be... Organisation to promote Scala Enterprise stack Scala
Akka (actors model impl.)
Play 2 (web MVC)
Full transcript