SlideShare una empresa de Scribd logo
1 de 45
About Functional Programming(Robust Programming) AapoKyrölä, 23.3.2011 Sulake
Contents Motivation: some issues with OOP and Java Introduction to Functional Programming Functional Programming –style in Java
Motivation: Problems with Object-Oriented Programming and Java
Fundamental Design Problem of Java Not well-typed => Nullpointers Programmer should handle two cases explicitly: userToBan is either null or not null. OR, compiler should not allow assigning NULL to a reference (like in C++  &obj). By proper typing, compiler would prevent null-pointers…
In Standard ML
OOP: criticism Class/Object structure cumbersome to change. a class hierarchy might be natural from some point of view, but for other (future) uses cumbersome. Problems with Inheritance much functionality hidden in superclasses. Hard to manage: if behavior of superclass is changed, how does it affect all subclasses? Especially in UI: subclass may depend on a not-intended behavior of a superclass. Delegation usually better than inheritance (Spring) better modularity with IOP  than with object hierarchies. API dependencies. Makes refactoring hard.
Object-relational mapping Always hated… a) Intuitively, object should have an identity But when you have many copies of the object… b) Queries with criteria-objects or example objects just stink. Select * from sale where date>? and date<? order by date limit 10;
ORM (continues) c) Even bigger problem with relations Identity of the objects in collection? Cumbersome to control when you want the collections loaded (all that hibernate session stuff). d) SQL is great, easy and powerful. Why switch away?  Declarative programming is powerful.
Side effects When you call a method, it can change the state, even if it is named getX(). At least previously in Habbo: effects on database object cache. Or : calling a collection method in hibernate object, user.getFriends(). You need to know if users were loaded in order to reason what happens.
Who manages an object? Java does not have immutable objects Need to “wrap” them by hand (Collections.unmodifiableList()). Error only on run-time. How do you handle such error? Should be compile-time. Concurrency issues Who manages multi-threaded access to an object? Read-only objects do not have this problem… Easy to get deadlocks.
Key to more robust code	 Compiler and type-system should do more! Generics are even worse… End of rant.
Quick introduction to Functional Programming Languages
Contents Some history Functional vs. imperative languages Declarative programming. Functions as values. High-order functions. Referential transparency. Power of the Type system in Standard ML. All examples in Standard ML. Next: how to program “functionally in Java”.
History Lambda calculus (30s-40s): mathematical theory for computing with functions (Church). LISP (50s): motivated by artificial intelligence. Introduced high level data structures such as lists and trees.  Scheme, Clojure Meta-language: ML (70s): for writing proof checkers. Standard ML, OCaML, F# Haskell (80s-90s)
History (cont.) Functional programming has (so far) mostly been favored in the academia Mathematically consistent (no pointer-arithmetic). Clear semantics: define functions, not manipulate state. Focus on computation, not on memory addressing, low-level issues. Carnegie Mellon uses ML as teaching language on data structures and algorithms Many other universities use Scheme or Haskell. Learn the data structures & algorithms, not fight with segfaults or nullpointers.
FP: new coming? Functional languages compatible with mainstream languages: Microsoft’s F#, based on OCaML Scala, Clojure for Java (hybrids) Python and other script languages adopt many functional ideas, such as list.map() and lambda-functions. Focus on correctness, not performance Especially important in finance. Compilers have improved. But, Completely different way of programming: hard to change. Performance (and memory consumption) has been a big issue. Lack of good tools; cumbersome to program “stateful” code such as UI. However, Scala/Clojure/F# will help bridging the gap as they are compatible with Java/C#.
What is Functional Programming? Imperative program executes sequence of commands to compute a value and modifies the state in the process: Functional program is declarativeand uses pattern matching:  fun sumlist []    = 0          | (x::xs) = x + sumlist(xs); “sum of numbers in list is the value of head of the list plus the sum of the rest of the list. Sum of an empty list is zero.” Functions are usually defined recursively.
More function declarations > fun      factorial 1 = 1 #        | factorial n = n * factorial(n-1); “factorial of n is n multiplied by the factorial of n-1, if n is larger than 1. Otherwise, factorial for 1 is 1.” fun gcd(a : int, 0) = a     | gcd(a : int, b : int) =          if (a < b) then gcd(a, b mod a) else gcd(b, a mod b)
More function declarations More pattern matching for types:
Functional programming (cont.) Imperative program executes commands, functional program evaluates expressions. Functions have no side-effects – there is no state. “Monads” used for input/output etc. (not part of this talk) Values are bound to symbols – but they cannot be changed! Definition of variable is different: it is not an address in memory.
Functional programming (cont.) Functions are values, and can be passed as parameters (anonymous or named): Example : (* Create new functions that return square of the original function *) fun sqrfun (f : real->real) =  let       fun F2 (x:real) = f(x)*f(x)  in F2 end; valdblsin = sqrfun(Math.sin); > dblsin(2.0); val it = 0.8268218104: real
FP (cont.): Functionals map > valmylist = [1,2,3,4,5,6,7,8,9,10]; valmylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: int list > List.map(fn x=>x*x) mylist; val it = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]: int list foldl / foldr > List.foldl(fn (x,n)=>x+n)  0 mylist; val it = 55: int > List.foldlop+ 0 mylist; val it = 55: int > vallistsummer= List.foldl op+ 0; vallistsummer = fn: int list -> int > listsummer(mylist); val it = 55: int filter > valevenfilter = List.filter(fn (x)=> if (x mod 2 = 0) then true else false); valevenfilter = fn: int list -> int list 	> evenfilter(mylist); val it = [2, 4, 6, 8, 10]: int list Extremely consistent and clear code!
Referential Transparency = expression can be safely replaced by its values = no side-effects. Enables reasoning about semantics of programs.  Lazy evaluation. Automatic parallel evaluation: order of execution does not matter. Imperative programs are close to machine code, hard to read. Meaning of functional program is “as it is written”.
Type system Very powerful feature of ML (and Haskell): use it for your benefit. Compiler can detect many errors that Java compiler could not. (You can ignore the type system and use wildcards extensively, but then you lose much of the power of Functional Programming). Parametric polymorphism and algebraic data types instead of inheritance. Example…
Type system: algebraic datatypes Product types (records=structs) and sum-types (union of types, such as: user|pet|furniture)
ADT (cont.)
ADT (cont.) Let’s now add a new room object type, “wallitem”. But we forget to add a case to “blocks_coordinate”!
ADT (cont.) But compiler warns us about this!
ADT (cont.) Compiler also warns about redundant cases typical error. Consider this in Java perhaps you have blocksCoordinate() for each room object class – example may not be best. but, maybe in Catalog code we have special price for each item for Moderators: This will be implemented as a if/case in Catalog code, not as a method in User object. Easy to forget cases when new user types are added.
Parametric Polymorphism	 Close to C++ templates unlike Java generics, first-class types (compile-time). Recursive types - definition of a tree with node type a’: datatype ’a tree =  		     Empty | 			 Node of ’a tree * ’a * ’a tree
Miscellanious In practice, it is easy to very also inefficient code in SML (like it is with Java). Garbage collection. Standard ML has a sophisticated module system that neatly separates API and implementation. Lazy ML Haskell is lazily evaluated. Laziness is problematic for parallelism or reasoning about costs. OCaML brings objects to ML.	 (but no-one uses them…)
Functional Style with Java Some ideas
Functional Languages for JDK Scala gaining popularity. compiles into Java bytecode, compatible with Java. parallel computation features. Erlang-type concurrent messaging style programming also supported. Clojure LISP dialect.
1) Pattern: Immutable objects If a “manager” returns an object, usually it should be immutable – and a copy: create special immutable classes:  ImmutableUser, ImmutableRoom, etc. => Makes ownership of the object clear. better to make own classes that do not have methods to modify the object than throw an exception! Collections.unmodifiableList(…) do not cache objects – only manager caches. makes thread-safe programming easier. Favor tiny carrier objects instead of huge monstrous objects instead of User, favor UserId, UserAppearance. make it clear by naming, that the object does not represent the “actual object”, buti s  a “view” of it. especially important with DAOs!! Should be evident what data has been loaded to the data-object.
2) Fail fast Java does not do much sanity checking in compilation time, so try to make incorrect code fail quickly in test time. Use runtime exceptions – and  you should not prepare to handle these kind of errors.
3) Instead of modifying an object, create new ones Avoids race conditions. Java GC is really fast, and likes short-lived objects. Particulary: avoid recycling objects. It is very error-prone!
4) Assertions and Declarations Functional:  @MessageHandler(id=.., need_session=true), @Moderator Use conservative default values for parameters! This is much better than expecting programmers to check all cases. Assertive: @NotNull, @NoSideEffect, @Slow etc. not great, but better than nothing. Intuitive naming: don’t use getXX, if method has a side-effect! Long method-names that explain what method does are good: user = loginUserWithPassword(user, pass); user = loginUserWithToke(token); instead of user = login(user,pass);  user = login(token);
Do not switch assertions off! “… it is absurd to make elaborate security checks on debugging runs, when no trust is put into results, and then remove them in production runs, when an errorenous result could be expensive or disastrous. What would we think of a sailing enthusiast who wears his life-jacket when training on dry land but takes it off as soon as he goes to sea?” C.A.R Hoare (1989)
5) Poor man’s Closures	 Closures = anonymous/lambda functions example from Groovy: Unfortunately Java (not even 7) will support Closures. BUT, anonymous classes can be used for the same purpose – although with much boilerplate.
Closure example
6) Compute, do not store If computing a value is reasonably light-weight, it is better to do that than store a precomputed value. Avoids many errors of forgetting to update all fields of an object. (example on next slide)
Example
Discussion
Questions Do you have experience on functional programming? Opinion on using Hibernate vs. using explicit queries? Where is Java going? why is it so difficult to do simple things, like a simple web service, with Java? Convention vs. Configuration?
Resources PolyML: http://www.polyml.org/ Google: “ocaml jane street cmu” good video introduction to FP scala-lang.org

Más contenido relacionado

La actualidad más candente

Functional programming in scala
Functional programming in scalaFunctional programming in scala
Functional programming in scalaStratio
 
Course outline for c programming
Course outline for c  programming Course outline for c  programming
Course outline for c programming Rokonuzzaman Rony
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8icarter09
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to ScalaRahul Jain
 
The Evolution of Scala
The Evolution of ScalaThe Evolution of Scala
The Evolution of ScalaMartin Odersky
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersSkills Matter
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming LanguageRaghavan Mohan
 
Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)Ralf Laemmel
 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java langer4711
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript ProgrammingSehwan Noh
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8LivePerson
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8 Bansilal Haudakari
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in JavaErhan Bagdemir
 
flatMap Oslo presentation slides
flatMap Oslo presentation slidesflatMap Oslo presentation slides
flatMap Oslo presentation slidesMartin Odersky
 
Ajax and JavaScript Bootcamp
Ajax and JavaScript BootcampAjax and JavaScript Bootcamp
Ajax and JavaScript BootcampAndreCharland
 

La actualidad más candente (20)

Functional programming in scala
Functional programming in scalaFunctional programming in scala
Functional programming in scala
 
Course outline for c programming
Course outline for c  programming Course outline for c  programming
Course outline for c programming
 
Scalax
ScalaxScalax
Scalax
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Devoxx
DevoxxDevoxx
Devoxx
 
The Evolution of Scala
The Evolution of ScalaThe Evolution of Scala
The Evolution of Scala
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
 
Java 8 Lambda and Streams
Java 8 Lambda and StreamsJava 8 Lambda and Streams
Java 8 Lambda and Streams
 
Intro to Scala
 Intro to Scala Intro to Scala
Intro to Scala
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 
Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)Functional OO programming (as part of the the PTT lecture)
Functional OO programming (as part of the the PTT lecture)
 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in Java
 
flatMap Oslo presentation slides
flatMap Oslo presentation slidesflatMap Oslo presentation slides
flatMap Oslo presentation slides
 
Ajax and JavaScript Bootcamp
Ajax and JavaScript BootcampAjax and JavaScript Bootcamp
Ajax and JavaScript Bootcamp
 

Similar a About Functional Programming

Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyTypesafe
 
Go Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingGo Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingLex Sheehan
 
Clojure and The Robot Apocalypse
Clojure and The Robot ApocalypseClojure and The Robot Apocalypse
Clojure and The Robot Apocalypseelliando dias
 
The IoT Academy training part2 AI Architecture
The IoT Academy training part2 AI ArchitectureThe IoT Academy training part2 AI Architecture
The IoT Academy training part2 AI ArchitectureThe IOT Academy
 
Concurrency Constructs Overview
Concurrency Constructs OverviewConcurrency Constructs Overview
Concurrency Constructs Overviewstasimus
 
A Survey of Concurrency Constructs
A Survey of Concurrency ConstructsA Survey of Concurrency Constructs
A Survey of Concurrency ConstructsTed Leung
 
Martin Odersky - Evolution of Scala
Martin Odersky - Evolution of ScalaMartin Odersky - Evolution of Scala
Martin Odersky - Evolution of ScalaScala Italy
 
LISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesLISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesDominic Graefen
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answersAkash Gawali
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++Amresh Raj
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsRuth Marvin
 

Similar a About Functional Programming (20)

Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
Google06
Google06Google06
Google06
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
Introduction to es6
Introduction to es6Introduction to es6
Introduction to es6
 
Scala google
Scala google Scala google
Scala google
 
Go Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingGo Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional Programming
 
Clojure and The Robot Apocalypse
Clojure and The Robot ApocalypseClojure and The Robot Apocalypse
Clojure and The Robot Apocalypse
 
The IoT Academy training part2 AI Architecture
The IoT Academy training part2 AI ArchitectureThe IoT Academy training part2 AI Architecture
The IoT Academy training part2 AI Architecture
 
Concurrency Constructs Overview
Concurrency Constructs OverviewConcurrency Constructs Overview
Concurrency Constructs Overview
 
A Survey of Concurrency Constructs
A Survey of Concurrency ConstructsA Survey of Concurrency Constructs
A Survey of Concurrency Constructs
 
Martin Odersky - Evolution of Scala
Martin Odersky - Evolution of ScalaMartin Odersky - Evolution of Scala
Martin Odersky - Evolution of Scala
 
LISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesLISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love Parantheses
 
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...Towards Improving Interface Modularity in Legacy Java Software Through Automa...
Towards Improving Interface Modularity in Legacy Java Software Through Automa...
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
 

About Functional Programming

  • 1. About Functional Programming(Robust Programming) AapoKyrölä, 23.3.2011 Sulake
  • 2. Contents Motivation: some issues with OOP and Java Introduction to Functional Programming Functional Programming –style in Java
  • 3. Motivation: Problems with Object-Oriented Programming and Java
  • 4. Fundamental Design Problem of Java Not well-typed => Nullpointers Programmer should handle two cases explicitly: userToBan is either null or not null. OR, compiler should not allow assigning NULL to a reference (like in C++ &obj). By proper typing, compiler would prevent null-pointers…
  • 6. OOP: criticism Class/Object structure cumbersome to change. a class hierarchy might be natural from some point of view, but for other (future) uses cumbersome. Problems with Inheritance much functionality hidden in superclasses. Hard to manage: if behavior of superclass is changed, how does it affect all subclasses? Especially in UI: subclass may depend on a not-intended behavior of a superclass. Delegation usually better than inheritance (Spring) better modularity with IOP than with object hierarchies. API dependencies. Makes refactoring hard.
  • 7. Object-relational mapping Always hated… a) Intuitively, object should have an identity But when you have many copies of the object… b) Queries with criteria-objects or example objects just stink. Select * from sale where date>? and date<? order by date limit 10;
  • 8. ORM (continues) c) Even bigger problem with relations Identity of the objects in collection? Cumbersome to control when you want the collections loaded (all that hibernate session stuff). d) SQL is great, easy and powerful. Why switch away? Declarative programming is powerful.
  • 9. Side effects When you call a method, it can change the state, even if it is named getX(). At least previously in Habbo: effects on database object cache. Or : calling a collection method in hibernate object, user.getFriends(). You need to know if users were loaded in order to reason what happens.
  • 10. Who manages an object? Java does not have immutable objects Need to “wrap” them by hand (Collections.unmodifiableList()). Error only on run-time. How do you handle such error? Should be compile-time. Concurrency issues Who manages multi-threaded access to an object? Read-only objects do not have this problem… Easy to get deadlocks.
  • 11. Key to more robust code Compiler and type-system should do more! Generics are even worse… End of rant.
  • 12. Quick introduction to Functional Programming Languages
  • 13. Contents Some history Functional vs. imperative languages Declarative programming. Functions as values. High-order functions. Referential transparency. Power of the Type system in Standard ML. All examples in Standard ML. Next: how to program “functionally in Java”.
  • 14. History Lambda calculus (30s-40s): mathematical theory for computing with functions (Church). LISP (50s): motivated by artificial intelligence. Introduced high level data structures such as lists and trees. Scheme, Clojure Meta-language: ML (70s): for writing proof checkers. Standard ML, OCaML, F# Haskell (80s-90s)
  • 15. History (cont.) Functional programming has (so far) mostly been favored in the academia Mathematically consistent (no pointer-arithmetic). Clear semantics: define functions, not manipulate state. Focus on computation, not on memory addressing, low-level issues. Carnegie Mellon uses ML as teaching language on data structures and algorithms Many other universities use Scheme or Haskell. Learn the data structures & algorithms, not fight with segfaults or nullpointers.
  • 16. FP: new coming? Functional languages compatible with mainstream languages: Microsoft’s F#, based on OCaML Scala, Clojure for Java (hybrids) Python and other script languages adopt many functional ideas, such as list.map() and lambda-functions. Focus on correctness, not performance Especially important in finance. Compilers have improved. But, Completely different way of programming: hard to change. Performance (and memory consumption) has been a big issue. Lack of good tools; cumbersome to program “stateful” code such as UI. However, Scala/Clojure/F# will help bridging the gap as they are compatible with Java/C#.
  • 17. What is Functional Programming? Imperative program executes sequence of commands to compute a value and modifies the state in the process: Functional program is declarativeand uses pattern matching: fun sumlist [] = 0 | (x::xs) = x + sumlist(xs); “sum of numbers in list is the value of head of the list plus the sum of the rest of the list. Sum of an empty list is zero.” Functions are usually defined recursively.
  • 18. More function declarations > fun factorial 1 = 1 # | factorial n = n * factorial(n-1); “factorial of n is n multiplied by the factorial of n-1, if n is larger than 1. Otherwise, factorial for 1 is 1.” fun gcd(a : int, 0) = a | gcd(a : int, b : int) = if (a < b) then gcd(a, b mod a) else gcd(b, a mod b)
  • 19. More function declarations More pattern matching for types:
  • 20. Functional programming (cont.) Imperative program executes commands, functional program evaluates expressions. Functions have no side-effects – there is no state. “Monads” used for input/output etc. (not part of this talk) Values are bound to symbols – but they cannot be changed! Definition of variable is different: it is not an address in memory.
  • 21. Functional programming (cont.) Functions are values, and can be passed as parameters (anonymous or named): Example : (* Create new functions that return square of the original function *) fun sqrfun (f : real->real) = let fun F2 (x:real) = f(x)*f(x) in F2 end; valdblsin = sqrfun(Math.sin); > dblsin(2.0); val it = 0.8268218104: real
  • 22. FP (cont.): Functionals map > valmylist = [1,2,3,4,5,6,7,8,9,10]; valmylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: int list > List.map(fn x=>x*x) mylist; val it = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]: int list foldl / foldr > List.foldl(fn (x,n)=>x+n) 0 mylist; val it = 55: int > List.foldlop+ 0 mylist; val it = 55: int > vallistsummer= List.foldl op+ 0; vallistsummer = fn: int list -> int > listsummer(mylist); val it = 55: int filter > valevenfilter = List.filter(fn (x)=> if (x mod 2 = 0) then true else false); valevenfilter = fn: int list -> int list > evenfilter(mylist); val it = [2, 4, 6, 8, 10]: int list Extremely consistent and clear code!
  • 23. Referential Transparency = expression can be safely replaced by its values = no side-effects. Enables reasoning about semantics of programs. Lazy evaluation. Automatic parallel evaluation: order of execution does not matter. Imperative programs are close to machine code, hard to read. Meaning of functional program is “as it is written”.
  • 24. Type system Very powerful feature of ML (and Haskell): use it for your benefit. Compiler can detect many errors that Java compiler could not. (You can ignore the type system and use wildcards extensively, but then you lose much of the power of Functional Programming). Parametric polymorphism and algebraic data types instead of inheritance. Example…
  • 25. Type system: algebraic datatypes Product types (records=structs) and sum-types (union of types, such as: user|pet|furniture)
  • 27. ADT (cont.) Let’s now add a new room object type, “wallitem”. But we forget to add a case to “blocks_coordinate”!
  • 28. ADT (cont.) But compiler warns us about this!
  • 29. ADT (cont.) Compiler also warns about redundant cases typical error. Consider this in Java perhaps you have blocksCoordinate() for each room object class – example may not be best. but, maybe in Catalog code we have special price for each item for Moderators: This will be implemented as a if/case in Catalog code, not as a method in User object. Easy to forget cases when new user types are added.
  • 30. Parametric Polymorphism Close to C++ templates unlike Java generics, first-class types (compile-time). Recursive types - definition of a tree with node type a’: datatype ’a tree = Empty | Node of ’a tree * ’a * ’a tree
  • 31. Miscellanious In practice, it is easy to very also inefficient code in SML (like it is with Java). Garbage collection. Standard ML has a sophisticated module system that neatly separates API and implementation. Lazy ML Haskell is lazily evaluated. Laziness is problematic for parallelism or reasoning about costs. OCaML brings objects to ML. (but no-one uses them…)
  • 32. Functional Style with Java Some ideas
  • 33. Functional Languages for JDK Scala gaining popularity. compiles into Java bytecode, compatible with Java. parallel computation features. Erlang-type concurrent messaging style programming also supported. Clojure LISP dialect.
  • 34. 1) Pattern: Immutable objects If a “manager” returns an object, usually it should be immutable – and a copy: create special immutable classes: ImmutableUser, ImmutableRoom, etc. => Makes ownership of the object clear. better to make own classes that do not have methods to modify the object than throw an exception! Collections.unmodifiableList(…) do not cache objects – only manager caches. makes thread-safe programming easier. Favor tiny carrier objects instead of huge monstrous objects instead of User, favor UserId, UserAppearance. make it clear by naming, that the object does not represent the “actual object”, buti s a “view” of it. especially important with DAOs!! Should be evident what data has been loaded to the data-object.
  • 35. 2) Fail fast Java does not do much sanity checking in compilation time, so try to make incorrect code fail quickly in test time. Use runtime exceptions – and you should not prepare to handle these kind of errors.
  • 36. 3) Instead of modifying an object, create new ones Avoids race conditions. Java GC is really fast, and likes short-lived objects. Particulary: avoid recycling objects. It is very error-prone!
  • 37. 4) Assertions and Declarations Functional: @MessageHandler(id=.., need_session=true), @Moderator Use conservative default values for parameters! This is much better than expecting programmers to check all cases. Assertive: @NotNull, @NoSideEffect, @Slow etc. not great, but better than nothing. Intuitive naming: don’t use getXX, if method has a side-effect! Long method-names that explain what method does are good: user = loginUserWithPassword(user, pass); user = loginUserWithToke(token); instead of user = login(user,pass); user = login(token);
  • 38. Do not switch assertions off! “… it is absurd to make elaborate security checks on debugging runs, when no trust is put into results, and then remove them in production runs, when an errorenous result could be expensive or disastrous. What would we think of a sailing enthusiast who wears his life-jacket when training on dry land but takes it off as soon as he goes to sea?” C.A.R Hoare (1989)
  • 39. 5) Poor man’s Closures Closures = anonymous/lambda functions example from Groovy: Unfortunately Java (not even 7) will support Closures. BUT, anonymous classes can be used for the same purpose – although with much boilerplate.
  • 41. 6) Compute, do not store If computing a value is reasonably light-weight, it is better to do that than store a precomputed value. Avoids many errors of forgetting to update all fields of an object. (example on next slide)
  • 44. Questions Do you have experience on functional programming? Opinion on using Hibernate vs. using explicit queries? Where is Java going? why is it so difficult to do simple things, like a simple web service, with Java? Convention vs. Configuration?
  • 45. Resources PolyML: http://www.polyml.org/ Google: “ocaml jane street cmu” good video introduction to FP scala-lang.org

Notas del editor

  1. Havent been here for two years speaking. Currently Ph.D. student in Carnegie Mellon: working on intersection of machine learning and distributed systems. Will study more parallel algorithms etc. further – this topic is very relevant for that. QUESTION: who has experience with Haskell, Scala, Standard ML, LISP?
  2. Start with some provocation. A fundamental design flaw in Java, which contributes to unrobust code tremendously.What is wrong with this?What if userToBan is null? Either: compiler should never let banUser to be called with NULL, or one should make a case analysis:
  3. In Standard ML: need to use type parameter “option” if type is nullable. In that case, each function must define cases for both non-null user (SOME) or null-user (NONE).If option is not set, value can never have null assignment.
  4. There is a lot of criticism about OOP, but it is what people learn in school. Especailly “old school” OOP that uses inheritance heavily is very hard to manage. Of course there are good sides of OOP as well .
  5. Disclaimers!
  6. Has anyone used Scala, is it used? Sorry that I am not very well aware.
  7. Point: functional languages have been there from the beginning. And by original computer scientist, functional programming was the proper way to study computation. Anyway, FP is not a new thing!
  8. QUESTION: does Univ of Helsinki or HUT/Aalto teach functional languages? Lambda-calculus theory?
  9. Note: this definition of sumlist is quite inefficient, since the recursion blows up. But we will see better version of this later. This is just to illustrate how functions are declarative.
  10. Familiar from python, scala
  11. This can be extremely useful.
  12. are we using these?Especially for payments etc., recommend trying.
  13. .. otherwise, the code gets bloated with wide try-catches and the session state can get screwed up. It is better to fail and close the session if a programmer bug is encountered.
  14. It is quite bloated, but of course the map/MapClosure, etc. must be done only once.