On Github Szczyp / functional-programming
Created by szczyp and bpr
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).
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);
}
Tell the machine what to do
var list = Enumerable.Range(0, 9).Where(i => i % 2 == 0).ToList();
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