Skip to content

Instantly share code, notes, and snippets.

@chriswebb09
Last active April 6, 2017 10:18
Show Gist options
  • Select an option

  • Save chriswebb09/9246114e6154e0d1b88dd0a9089c26e0 to your computer and use it in GitHub Desktop.

Select an option

Save chriswebb09/9246114e6154e0d1b88dd0a9089c26e0 to your computer and use it in GitHub Desktop.
Bank example
enum Transaction {
case deposit, withdraw
}
class Bank {
typealias Account = (String, Double)
typealias AccountTransaction = (Double, Account) -> Account
static func deposit(amount : Double, account : (name : String, balance : Double)) -> Account {
let newBalance : Double = account.balance + amount
return (account.name, newBalance)
}
static func withdraw(amount : Double, account : (name : String, balance : Double)) -> Account {
let newBalance : Double = account.balance - amount
return (account.name, newBalance)
}
static func chooseTransaction(transaction: Transaction) -> AccountTransaction {
switch transaction {
case .deposit:
return Bank.deposit
case .withdraw:
return Bank.withdraw
}
}
}
class Vault {
static func accessVault(password: String) -> String {
func openVault() -> String {
return "Vault opened"
}
func closeVault() -> String {
return "Vault closed"
}
if password == "secret" {
return openVault()
} else {
return closeVault()
}
}
}
var account1 = ("State Bank Personal", 1011.10)
var account2 = ("State Bank Business", 24309.63)
Bank.chooseTransaction(transaction: .deposit)(63.17, account1)
Vault.accessVault(password: "secret")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment