• Save
Deconstructing Functional Programming
Upcoming SlideShare
Loading in...5
×
 

Deconstructing Functional Programming

on

  • 1,002 views

Video and slides synchronized, mp3 and slide download available at URL http://bit.ly/1e4UMbN. ...

Video and slides synchronized, mp3 and slide download available at URL http://bit.ly/1e4UMbN.

Gilad Bracha explains how to distinguish FP hype from reality and to apply key ideas of FP in non-FP languages, separating the good parts of FP from its unnecessary cultural baggage. Filmed at qconsf.com.

Gilad Bracha is the creator of the Newspeak programming language and a software engineer at Google where he works on Dart. Previously, he was a VP at SAP Labs, a Distinguished Engineer at Cadence, and a Computational Theologist and Distinguished Engineer at Sun. He is co-author of the Java Language Specification, and a researcher in the area of object-oriented programming languages.

Statistics

Views

Total Views
1,002
Views on SlideShare
1,002
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Deconstructing Functional Programming Deconstructing Functional Programming Presentation Transcript

  • Deconstructing Functional Programming Gilad Bracha Wednesday, November 13, 13
  • Watch the video with slide synchronization on InfoQ.com! http://www.infoq.com/presentations /functional-pros-cons InfoQ.com: News & Community Site • 750,000 unique visitors/month • Published in 4 languages (English, Chinese, Japanese and Brazilian Portuguese) • Post content from our QCon conferences • News 15-20 / week • Articles 3-4 / week • Presentations (videos) 12-15 / week • Interviews 2-3 / week • Books 1 / month
  • Presented at QCon San Francisco www.qconsf.com Purpose of QCon - to empower software development by facilitating the spread of knowledge and innovation Strategy - practitioner-driven conference designed for YOU: influencers of change and innovation in your teams - speakers and topics driving the evolution and innovation - connecting and catalyzing the influencers and innovators Highlights - attended by more than 12,000 delegates since 2007 - held in 9 cities worldwide
  • The Heart of Darkness Wednesday, November 13, 13
  • What is Functional Programming? Definitions vary, but tend to involve Higher order functions (HOFs) Absence of effects Wednesday, November 13, 13
  • Higher Order Functions Functions are values Can be passed as arguments and returned as results Function literals are expressions that can be written anywhere an expression can appear Wednesday, November 13, 13
  • Languages with HOFs Smalltalk, Self, Newspeak, Dart, Javascript ... and now even Java (gasp) Wednesday, November 13, 13
  • Languages with Effects Lisp, Scheme, Racket, Clojure, ML, OCaml, F#, Erlang Wednesday, November 13, 13
  • Languages w/o HOFs Backus’ FP, SISAL, the original versions of Erlang ... These are functional languages; in fact FP was the original functional language Wednesday, November 13, 13
  • What is Functional Programming? 1. A style which utilizes HOFs and minimizes effects 2. A cult(ure) 3. All of the above In particular, there is no contradiction with object orientation (see Scala as an example) Wednesday, November 13, 13
  • HOFs/Closures HOFs are awesome. Lambdas even more so. The great classical HOFs: map, filter, reduce They’ve been around since Lisp & Smalltalk (i.e., long before the term FP was introduced) Wednesday, November 13, 13
  • User-defined Control Structures a > b ifTrue:[ a - b]. Wednesday, November 13, 13
  • User-defined Control Structures a > b ifTrue:[ a - b]. A boolean, an object, the target of ... Wednesday, November 13, 13
  • User-defined Control Structures a > b ifTrue:[ a - b]. The method, invoked with ... Wednesday, November 13, 13
  • User-defined Control Structures a > b ifTrue:[ a - b]. The argument, a block/lambda/closure Wednesday, November 13, 13
  • User-defined Control Structures a > b ifTrue:[ a - b]. In class True, ifTrue: evaluates its argument and returns the result. In class False, ifTrue: returns nil. Wednesday, November 13, 13
  • Tail Calls Look ma, no loops: while(b, s) { if b() then {s(); while(b,s)} else null} How to express computation unbounded in time but bounded in space. A language is functional if it supports proper tail calls? Wednesday, November 13, 13
  • Tail Calls What about debugging? Where’s my stack trace? http://gbracha.blogspot.com/2009/12/chased-byones-own-tail.html Wednesday, November 13, 13
  • Hindley-Milner If it typechecks, it works .... ? But error messages are terrible Leaks implementation information Wednesday, November 13, 13
  • Currying All functions take one argument, produce one result Extremely compositional, BUT We lose almost all structure Forced to rely on type system Wednesday, November 13, 13
  • Pattern Matching Nice Allows FPLs to pretend they don’t do dynamic typechecks and casts Usually second class First class patterns are interesting http://gbracha.blogspot.com/2010/05/patterns-of-dynamic-typechecking.html http://gbracha.blogspot.com/2010/06/patterns-as-objects-in-newspeak.html Wednesday, November 13, 13
  • Monads Wednesday, November 13, 13
  • A Monad by any other name would smell ... Wednesday, November 13, 13
  • A Monad by any other name would smell as sweet Wednesday, November 13, 13
  • Ye Highlands and ye Lowlands, Oh, where hae ye been? They hae slain the Earl O' Moray, And laid him on the green. The Bonny Earl O’Moray Wednesday, November 13, 13
  • Ye Highlands and ye Lowlands, Oh, where hae ye been? They hae slain the Earl O' Moray, And Lady Mondegreen. The Bonny Earl O’Moray, revised by Sylvia Wright Wednesday, November 13, 13
  • Ye Highlands and ye Lowlands, Oh, where hae ye been? They hae slain the Earl O' Moray, And Lady Monadgreen. The Bonny Earl O’Moray, revised by me Wednesday, November 13, 13
  • Monad Metaphors, aka Monadgreens Nuclear waste containers Romantic conquests Space Suits Monsters Macros Containers Conversations Black holes Wednesday, November 13, 13
  • Lady Monadgreen’s Curse Once you understand monads, you immediately become incapable of explaining them to anyone else Wednesday, November 13, 13
  • Monads http://gbracha.blogspot.com/2011/01/maybemonads-might-not-matter.html Wednesday, November 13, 13
  • abstract class FlatMappable { FlatMappable(a); flatMap(f); } Wednesday, November 13, 13
  • abstract class Monad { Monad(a); flatMap(f); } Wednesday, November 13, 13
  • abstract class Monad { Monad.unit(a); flatMap(f); } Wednesday, November 13, 13
  • abstract class Monad { Monad.return(a); flatMap(f); } Wednesday, November 13, 13
  • abstract class Monad { Monad.return(a); bind(f); } Wednesday, November 13, 13
  • abstract class Monad { Monad.return(a); operator * (f); } Wednesday, November 13, 13
  • abstract class Monad { Monad.return(a); operator >>= (f); } Wednesday, November 13, 13
  • abstract class FlatMappable { FlatMappable(a); flatMap(f); // map, then flatten } Wednesday, November 13, 13
  • abstract class Mappable { Mappable(a); map(f); } abstract class MappableAndFlattenable extends Mappable { MappableAndFlattenable(a): super(a); flatten(); } Wednesday, November 13, 13
  • abstract class FlatMappable extends MappableAndFlattenable { FlatMappable(a): super(a); flatMap(f) => map(f).flatten(); } Wednesday, November 13, 13
  • What’s the Contract Like? Sample Clause: new Mappable(x).map(f) == new Mappable( f(x)); Wednesday, November 13, 13
  • What’s the Contract Like? Sample Clause: new FlatMappable(x).flatMap(f) == f(x); Wednesday, November 13, 13
  • What’s the Contract Like? Sample Clause: new C(x).flatMap(f) == f(x); for any class C that implements the contract Wednesday, November 13, 13
  • The Whole Contract new C(x).flatMap(f) == f(x); c.flatMap((x)=> new C(x)) == c; c.flatMap((x)=> f(x).flatMap(g)) == c.flatMap(f).flatMap(g); Wednesday, November 13, 13
  • FlatMappable is more useful than you think Collections of all kinds: in-memory, databases, streams “Singleton collections”: functions, continuations, futures Wednesday, November 13, 13
  • A simple expression e.b.c what could possibly go wrong? Wednesday, November 13, 13
  • A simple expression e.b.c what could possibly go wrong? e could be null. Ok e == null ? null : e.b.c Wednesday, November 13, 13
  • A simple expression e.b.c what could possibly go wrong? e could be null. Ok e == null ? null : e.b.c oops, e.b could be null e == null ? null : e.b == null ? null : e.b.c Wednesday, November 13, 13
  • A simple expression e == null ? null : e.b == null ? null : e.b.c what about side effects? (var it = e) == null ? null : (var ab = it.b) == null ? null : ab.c Wednesday, November 13, 13
  • Safe Navigation Operator e?.b?.c e?.id(a1, .. an) is sugar for e.ifNotNull((x) => x.id(a1, .. an)) where we define class Object { ifNotNull(f) => this == null ? null : f(this); } Wednesday, November 13, 13
  • Promise Pipelining e <- id(a1, .. an) is sugar for e.then((x) => x.id(a1, .. an)) where we define class Object { then(f) => f(this); } Wednesday, November 13, 13
  • Safe Promise Pipelining e <- id(a1, .. an) is sugar for e.then((x) => x.id(a1, .. an)) where we define class Object { then(f) => this == null ? null : f(this); } Wednesday, November 13, 13
  • Async message pipelining e <- id(a1, .. an) is sugar for e.then((x) => x.id(a1, .. an)) where we define class Object { then(f) => Future.immediate(this).then(f); } Wednesday, November 13, 13
  • Scalar operators extended pointwise to collections e.* id(a1, .. an) is sugar for e.map((x) => x.id(a1, .. an)) where we define class Object { map(f) => f(this); } [‘abc’, ‘de’, ‘wxyz’].* length evaluates to [3, 2, 4] Wednesday, November 13, 13
  • Scalar operators extended pointwise to collections e.* id(a1, .. an) is sugar for e.map((x) => x.id(a1, .. an)) where we define class Object { map(f) => this == null ? null : f(this); } [‘abc’, null, ‘wxyz’].* length evaluates to [3, null, 4] Wednesday, November 13, 13
  • Stream transformers mouseclicks.* x Wednesday, November 13, 13
  • Is There a Pattern Here? The Curse of the Monadgreens prevents me from discussing this further In practice, we see generalizations of set notation rather than navigation LINQ is an example. However, you didn’t need to have even heard of monads to invent LINQ. Just look at Smalltalk’s collection API, Glorp etc. http://gbracha.blogspot.com/2011/01/maybemonads-might-not-matter.html Wednesday, November 13, 13
  • The Killer App for FP? Wednesday, November 13, 13
  • Wednesday, November 13, 13
  • The Killer App for FP? Live Programming http://gbracha.blogspot.com/2012/11/debug-mode-is-only-mode.html http://gbracha.blogspot.com/2013/04/making-methods-live.html https://www.youtube.com/watch?v=74WqdS_58uY Wednesday, November 13, 13
  • Summary Much to learn from FP FP and OOP are often complementary Filter out propaganda Separate cultural baggage from core values Wednesday, November 13, 13
  • The Newspeak eye on slide 5 was designed by Victoria Bracha and is used by permission. The cartoon on slide 2 comes from http://xkcd.com/ 1270/ and is licensed by xkcd.com under http:// creativecommons.org/licenses/by-nc/2.5/ The rose on slide 16 is by Kikuo Teranishi and licensed under http://creativecommons.org/licenses/by-sa/3.0/ deed.en Wednesday, November 13, 13
  • Watch the video with slide synchronization on InfoQ.com! http://www.infoq.com/presentations/functional -pros-cons