Mixing functional programming approaches in an object oriented language

Click here to load reader

download Mixing functional programming approaches in an object oriented language

of 150

  • date post

    26-Aug-2014
  • Category

    Technology

  • view

    1.169
  • download

    6

Embed Size (px)

description

My slides from Biz Tech Delhi

Transcript of Mixing functional programming approaches in an object oriented language

  • Applying functional programming approaches in object oriented languages
    Mark Needham
    ThoughtWorks 2010
  • C# 1.0
  • int[] ints = new int[] {1, 2, 3, 4, 5}

  • int[] Filter(int[] ints){ ArrayList results = new ArrayList(); foreach (int i in ints) {if (i % 2 == 0){results.Add(i);} } return results.ToArray(typeof(int));}
  • int[] ints = new int[] {1, 2, 3, 4, 5}

  • int[] Filter(int[] ints){ ArrayList results = new ArrayList(); foreach (int i in ints) {if (i > 3 == 0){results.Add(i);} } return results.ToArray(typeof(int));}
  • int[] Filter(int[] ints){ ArrayList results = new ArrayList(); foreach (int i in ints) {if (i % 2 == 0){results.Add(i);} } return results.ToArray(typeof(int));}
  • int[] Filter(int[] ints){ ArrayList results = new ArrayList(); foreach (int i in ints) {if (i >3 == 0){results.Add(i);} } return results.ToArray(typeof(int));}
  • interface IIntegerPredicate{bool Matches(int value);}
  • class EvenPredicate : IIntegerPredicate{bool Matches(int value){return value % 2 == 0;}}
  • class GreaterThan3Predicate : IIntegerPredicate{bool Matches(int value){return value > 3;}}
  • int[] Filter(int[] ints, IIntegerPredicate predicate){ ArrayList results = new ArrayList(); foreach (int i in ints) {if (predicate.Matches(i)){results.Add(i);} } return results.ToArray(typeof(int));}
  • int[] ints = new int[] {1, 2, 3, 4, 5 };int[] even = Filter(ints, new EvenPredicate());int[] greaterThan3 = Filter(ints, new GreaterThan3Predicate());
  • interface IIntegerPredicate{bool Matches(int value);}
  • bool delegate IntegerPredicate(int value);
  • bool Even(int value){return value % 2 == 0;}
  • bool GreaterThan3(int value){return value > 3;}
  • int[] Filter(int[] ints, IntegerPredicate predicate){ ArrayList results = new ArrayList(); foreach (int i in ints) { if (predicate(i)){results.Add(i);} } return results.ToArray(typeof(int));}
  • int[] ints = new int[] {1, 2, 3, 4, 5 };int[] even = Filter(ints,new IntegerPredicate(Even));int[] greaterThan3 = Filter(ints, new IntegerPredicate(GreaterThan3));
  • C# 2.0

  • Inference
    int[] ints = new int[] {1, 2, 3, 4, 5 };
    int[] even = Filter(ints,new IntegerPredicate(Even));
    int[] greaterThan3 = Filter(ints,
    new IntegerPredicate(GreaterThan3));
  • Inference
    int[] ints = new int[] {1, 2, 3, 4, 5 };
    int[] even = Filter(ints, Even);
    int[] greaterThan3 = Filter(ints,GreaterThan3);
  • Generics
    delegate bool IntegerPredicate(int value);
  • Generics
    delegate bool Predicate (T value);
  • Generics
    int[] Filter(int[] ints, IntegerPredicate predicate)
    {
    ArrayList results = new ArrayList();
    foreach (int i in ints)
    {
    if (predicate(i))
    {
    results.Add(i);
    }
    }
    return results.ToArray(typeof(int));
    }
  • Generics
    T[] Filter(T[] values, Predicate predicate)
    {
    List results = new List();
    foreach (T i in value)
    {
    if (predicate(i))
    {
    results.Add(i);
    }
    }
    return results.ToArray();
    }
  • Iterators
    IEnumerable Filter(IEnumerable values, Predicate p)
    {
    List results = new List();
    foreach (T i in value)
    {
    if (p(i))
    {
    results.Add(i);
    }
    }
    return results;
    }
  • Iterators
    IEnumerable Filter(IEnumerable values, Predicate p)
    {
    foreach (T i in value)
    {
    if (p(i))
    {
    yield return i;
    }
    }
    }
  • Anonymous Methods
    IEnumerable greaterThan3 = Filter(ints, GreaterThan3);
  • Anonymous Methods
    IEnumerable greaterThan3 = Filter(ints,
    delegate(int value) { return value > 3; });
  • Anonymous Methods
    int minimumValue = 3;
    IEnumerable greaterThan3 = Filter(ints,
    delegate(int value) { return value > minimumValue; });
  • C# 3.0

  • Lambdas
    int minimumValue = 3;
    IEnumerable greaterThan3 = Filter(ints,
    delegate(int value) { return value > minimumValue; });
  • Lambdas
    int minimumValue = 3;
    IEnumerable greaterThan3 = Filter(ints,
    value => value > minimumValue);
  • More Type Inference
    int minimumValue = 3;
    IEnumerable greaterThan3 = Filter(ints,
    value => value > minimumValue);
  • More Type Inference
    int minimumValue = 3;
    var greaterThan3 = Filter(ints, value => value > minimumValue);
  • Extension Methods
    int minimumValue = 3;
    var greaterThan3 = Filter(ints, value => value > minimumValue);
  • Extension Methods
    int minimumValue = 3;
    var greaterThan3 = ints.Filter(value => value > minimumValue);
  • LINQ
  • LINQ
    New delegates in System namespace
    Action, Action, Func, Func etc.
  • LINQ
    New delegates in System namespace
    Action, Action, Func, Func etc.
    System.Linq
    Extension methods Where, Select, OrderBy etc.
  • LINQ
    New delegates in System namespace
    Action, Action, Func, Func etc.
    System.Linq
    Extension methods Where, Select, OrderBy etc.

    Some compiler magic to translate sql style code to method calls
  • LINQ
    var even = ints.Where(value => value % 2 == 0)

    var greaterThan3 = ints.Where(value => value > 3)

    or
    var even = from value in ints
    where value % 2 == 0
    select value

    var greaterThan3 = from value in ints
    where value > 3
    select value

  • InterfacesDelegatesAnonymous methods Lambdas
  • So this functional programming thing
  • http://www.flickr.com/photos/stuartpilbrow/2938100285/sizes/l/
  • Higher order functions
  • var ints = new int[] {1, 2, 3, 4, 5 };var greaterThan3 = ints.Where(value => value > 3)var even = ints.Where(value => value % 2 == 0)
  • Pure functions
  • Immutability
  • Lazy evaluation
  • Iterators
    IEnumerable Filter(IEnumerable values, Predicate p)
    {
    foreach (T i in value)
    {
    if (p(i))
    {
    yield return i;
    }
    }
    }
  • Recursion & Pattern Matching
  • Transformational Mindset
    We can just pass functions around instead in most cases
    - find an example where it still makes sense to use the GOF approach though.
  • Input -> ??? -> ??? -> ??? ->Output
  • http://www.emt-india.net/process/petrochemical/img/pp4.jpg
  • So why should you care?
  • Functional can fill in the gaps in OO code

  • Large
    Medium
    Small
    Programming in the
  • Programming in the
    a high level that affects as well as crosscuts multiple classes and functions
    Large
    Medium
    Small
  • Programming in the
    a single API or group of related APIs in such things as classes, interfaces, modules
    Large
    Medium
    Small
  • Programming in the
    individual function/method bodies
    Large
    Medium
    Small