Your SlideShare is downloading. ×
About Functional Programming
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

About Functional Programming


Published on

My presentation at Sulake ( about functional programming languages and how to adopt a functional-style in Java development. I argue that functional programs are more robust than …

My presentation at Sulake ( about functional programming languages and how to adopt a functional-style in Java development. I argue that functional programs are more robust than imperative ones.

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide
  • 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?
  • 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:
  • 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.
  • 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 .
  • Disclaimers!
  • Has anyone used Scala, is it used? Sorry that I am not very well aware.
  • 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!
  • QUESTION: does Univ of Helsinki or HUT/Aalto teach functional languages? Lambda-calculus theory?
  • 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.
  • Familiar from python, scala
  • This can be extremely useful.
  • are we using these?Especially for payments etc., recommend trying.
  • .. 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.
  • It is quite bloated, but of course the map/MapClosure, etc. must be done only once.
  • Transcript

    • 1. About Functional Programming(Robust Programming)
      AapoKyrölä, 23.3.2011
    • 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…
    • 5. In Standard ML
    • 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 and lambda-functions.
      Focus on correctness, not performance
      Especially important in finance.
      Compilers have improved.
      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) =
      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
      > valmylist = [1,2,3,4,5,6,7,8,9,10];
      valmylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: int list
      > 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
      > 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.
    • 25. Type system: algebraic datatypes
      Product types (records=structs) and sum-types (union of types, such as: user|pet|furniture)
    • 26. ADT (cont.)
    • 27. ADT (cont.)
      Let’s now add a new room object type, “wallitem”.
      But we forget to add a case to
    • 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
      gaining popularity.
      compiles into Java bytecode, compatible with Java.
      parallel computation features.
      Erlang-type concurrent messaging style programming also supported.
      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!
      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.
    • 40. Closure example
    • 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)
    • 42. Example
    • 43. Discussion
    • 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
      Google: “ocaml jane street cmu”
      good video introduction to FP