Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Currying in Swift

445 views

Published on

Lightning talk from iOSDevUK 2014

Published in: Software
  • Be the first to comment

Currying in Swift

  1. 1. Currying in Swift A tale of state encapsulation Maxim Zaks @iceX33
  2. 2. Every presentation needs a theme
  3. 3. func amountWithVat(vat:Float, amount: Float) -> Float { return roundf((1 + vat) * amount) } let amount1 = amountWithVat(0.19, 100) let amount2 = amountWithVat(0.19, 200) let amount3 = amountWithVat(0.2, 300)
  4. 4. func amountWithVat(vat:Float)(amount: Float) -> Float { return roundf((1 + vat) * amount) } let addGermanVatTo = amountWithVat(0.19) let addUKVatTo = amountWithVat(0.2) let amount1 = addGermanVatTo(amount: 100) let amount2 = addGermanVatTo(amount: 200) let amount3 = addUKVatTo(amount: 300)
  5. 5. func amountWithVat( vat : Float, roundFunction : (Float)->Float = roundf) (amount: Float) -> Float { return roundFunction((1 + vat) * amount) } let addGermanVatTo = amountWithVat(0.19){ roundf($0 * 100) / 100 } let addUKVatTo = amountWithVat(0.2) let amount1 = addGermanVatTo(amount: 101) let amount2 = addGermanVatTo(amount: 200) let amount3 = addUKVatTo(amount: 300)
  6. 6. Should we consider currying as a revelation?
  7. 7. func amountWithVat(vat:Float)(amount: Float) -> Float { return roundf((1 + vat) * amount) } let addGermanVatTo = amountWithVat(0.19) let addUKVatTo = amountWithVat(0.2) let amount1 = addGermanVatTo(amount: 100) let amount2 = addGermanVatTo(amount: 200) let amount3 = addUKVatTo(amount: 300)
  8. 8. func amountWithVat(vat:Float) -> Float -> Float { return { (amount:Float) -> Float in return roundf((1 + vat) * amount) } } let addGermanVatTo = amountWithVat(0.19) let addUKVatTo = amountWithVat(0.2) let amount1 = addGermanVatTo(100) let amount2 = addGermanVatTo(200) let amount3 = addUKVatTo(300)
  9. 9. typedef float (^FloatToFloat)(float); FloatToFloat (^amountWithVat)(float) = ^FloatToFloat (float vat) { return ^(float amount){ return roundf((1 + vat) * amount); }; }; FloatToFloat addGermanVatTo = amountWithVat(.19); FloatToFloat addUKVatTo = amountWithVat(0.2); float amount1 = addGermanVatTo(100); float amount2 = addGermanVatTo(200); float amount3 = addUKVatTo(300);
  10. 10. Interesting application of function currying found out by Ole Begemann (http://oleb.net/blog/2014/07/swift-instance-methods-curried-functions/)
  11. 11. class BankAccount { var balance: Double = 0.0 func deposit(amount: Double) { balance += amount } } let account = BankAccount() account.deposit(100) // balance is now 100 let depositor = BankAccount.deposit depositor(account)(100) // balance is now 200
  12. 12. So every method is implemented internaly through function currying
  13. 13. One More Thing
  14. 14. Who knows the name of the show I used for the theme
  15. 15. Thank you Maxim Zaks @iceX33

×