Functional Programming – for fun and profit



Functional Programming – for fun and profit

0 0


functional-programming

funcional programming

On Github Szczyp / functional-programming

Functional Programming

for fun and profit

Created by szczyp and bpr

Functional Programming

The Next Big Thing

Oh hey, these are some notes. They'll be hidden in your presentation, but you can see them if you open the speaker notes window (hit 's' on your keyboard).

Not necessarily 'next'

  • Turing machine (1936) vs Lambda calculus (1930s)
  • Fortran vs LISP

Imperative style

Tell the machine how to do a thing

          var list = new List<int>();
for(int i = 0; i < 10; i++)
{
  if(i % 2 == 0)
    list.Add(i);
}
        

Declarative style

Tell the machine what to do

        var list = Enumerable.Range(0, 9).Where(i => i % 2 == 0).ToList();
            

Immutable data and functions over classes

Classes

  • Class = Data + Behaviour
  • Data is mutable
  • Coupled with behaviour for encapsulation

Data

  • Types represent possible states rather than memory layout
  • Product types
  • Sum types

Immutability

  • Without mutable state encapsulation is not needed
  • Passing around values is safe - they won't be changed
  • Simple concurrency

Functions

  • Data transformation
  • Composable
  • Also a data

F#

Case study

abstract class BankAccount
{
  protected decimal balance;
  public abstract decimal CurrentBalance { get; }

  public BankAccount (decimal balance)
  {
     this.balance = balance; 
  }
}
class Current : BankAccount
{
  public decimal CurrentBalance { get { return balance; } }
  public Current (decimal balance)
    : base(balance)
	{
	}
}
class TimeDeposit : BankAccount
{
  float rate;
  DateTime term;

  public TimeDeposit(decimal balance, float rate, DateTime term)
    : base(balance)
  {
    this.rate = rate;
    this.term = term;
  }

  public decimal CurrentBalance 
  { 
    get 
    {
      if (DateTime.Now > term)
        return balance + balance * rate;
      else
        return balance;
    } 
  }
}
type BankAccount =
       Current of decimal
     | TimeDeposit of balance:decimal * rate:float * term:DateTime
type Email = string
type Coords = float * float
type ContactInfo = { Name : string; Location : Coords; Email : Email }
type Contact = Active of ContactInfo | Inactive of ContactInfo * DateTime
let currentBalance = function
    Current b -> b
  | TimeDeposit(b,r,t) -> if DateTime.Now > t then b + b * r else b
let currentBalance acc = match acc with
    Current b -> b
  | TimeDeposit(b,r,t) -> if DateTime.Now > t then b + b * r else b
class CallDeposit : BankAccount
{
  float rate;

  public CallDeposit(decimal balance, float rate)
    : base(balance)
  {
    this.rate = rate;
  }

  public decimal CurrentBalance 
  {
    get { return balance + balance * rate; } 
  }
}
type BankAccount =
       Current of decimal
     | TimeDeposit of balance:decimal * rate:float * term:DateTime
     | CallDeposit of balance:decimal * rate:float
let currentBalance = function
    Current b -> b
  | TimeDeposit(b,r,t) -> if DateTime.Now > t then b + b * r else b
  | CallDeposit (b, r) -> b + b * r
// BankAccount
public abstract void Withdraw(decimal amount);

// Current
public void Withdraw(decimal amount)
{
  balance -= amount;
}

// TimeDeposit
public void Withdraw(decimal amount)
{
  if (DateTime.Now > term)
    balance -= amount;
}

// CallDeposit
public void Withdraw(decimal amount)
{
  if(amount <= balance)
    balance -= amount;
}
let withdraw a = function
    Current b -> Current(b-a)
  | TimeDeposit(b,r,t) as td -> if DateTime.Now > t then TimeDeposit(b-a,r,t) else td
  | CallDeposit(b,r) as cd -> if a <= b then CallDeposit(b-a,r) else cd