![Page 1: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/1.jpg)
Swift, functional programming,
& the future of Objective-C
Alexis Gallagher @alexisgallagherPart of a talk delivered on 2014-07-02
![Page 2: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/2.jpg)
Ques%ons
1. What's)new)in)Swi.?
2. Is)Swi.)a)func5onal)programming)language?
• And)what)is)"func5onal",)anyway?)Who)cares?
3. Is)ObjC)going)away?
• And)so,)is)everything)easier)now?
![Page 3: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/3.jpg)
What's'hardly'new'in'Swi,?
![Page 4: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/4.jpg)
Objective-C
• classes • methods • protocols • categories • functions • ARC • blocks • Cocoa values
& collections
![Page 5: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/5.jpg)
Objective-C
Swift
• classes • methods • protocols • extensions • functions • ARC • closures • Cocoa values
& collections
![Page 6: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/6.jpg)
just%syntax,%almost
ObjC:
// Foo.h@interface Foo : UIView@property (nonatomic,weak) UIView * myView@end
@implementation Foo- (CGFloat) addAtX:(CGFloat) x andY:(CGFloat) y { UIView * v = [[UIView alloc] initWithFrame:CGRectMake(x,y,10,10)]; [self addSubview:v]; self.myView = v; return x + y;}@end
![Page 7: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/7.jpg)
just%syntax,%almost
Swi$:
class Foo: UIView { weak var myView:UIView? func addAt(#x:CGFloat, y:CGFloat) -> CGFloat { let v = UIView(frame:CGRectMake(x,y,10,10)) self.addSubview(v) self.myView = v return x + y }}
![Page 8: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/8.jpg)
blocks'(closures)
ObjC:
NSInteger (^increment)(NSInteger) = ^NSInteger (NSInteger x) { return x+1;}
Swi$:
let increment = { (x:NSInteger) -> NSInteger in return x+1}
![Page 9: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/9.jpg)
What's'a'bit$new'in'Swi,?
![Page 10: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/10.jpg)
Objective-C
• structs • namespaces • operator
overloading • ObjC interop • Swift values &
collections
Swift+Swift
• classes • methods • protocols • extensions • functions • ARC • closures • Cocoa values
& collections
![Page 11: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/11.jpg)
structs&are&peers&to&classes
ObjC:
CGRect r = [view frame];CGFloat intTopY = CGRectGetMinY(CGRectIntegral(r));
Swi$:
extension CGRect { func intTopY() -> CGFloat { return CGRectGetMinY(CGRectIntegral(self)) }}let r = view.frame;let intTopY = r.intTopY;
![Page 12: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/12.jpg)
What's'really&new'in'Swi,?
![Page 13: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/13.jpg)
Objective-C
• enums with associated values • option types • pattern matching • generics • type inference • immutability supports • tuples
Swift+• structs • namespaces • operator
overloading • ObjC interop • Swift values &
collections
Swift+Swift
• classes • methods • protocols • extensions • functions • ARC • closures • Cocoa values
& collections
![Page 14: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/14.jpg)
Objective-C
• enums with associated values • option types • pattern matching • generics • type inference • immutability supports • tuples
Swift+• structs • namespaces • operator
overloading • ObjC interop • Swift values &
collections
Swift+Swift
• classes • methods • protocols • extensions • functions • ARC • closures • Cocoa values
& collections
![Page 15: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/15.jpg)
Every “really new” part of Swift originated in a functional programming
language of the 1980s or earlier.
![Page 16: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/16.jpg)
FP
![Page 17: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/17.jpg)
No#ma&er#what#language#you#work#in,#programming#in#a#func7onal#style#provides#benefits.#You#should#do#it#
whenever#it#is#convenient....—"John"Carmack,"2012.
![Page 18: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/18.jpg)
JavaScript*is*brilliant*and*is*succeeding*...*and*I*think*it's*because*
of*the*func:onal*stuff.—"Douglas"Crockford,"2014
![Page 19: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/19.jpg)
a"programming"styletrea%ng(the
func%onas#the#primary#unit#of#abstrac2on
![Page 20: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/20.jpg)
t0 = 5
f(t0) = 50
NSNumber * t0 = @5;
NSNumber * pos = f(t0);pos; // => 50
f(t0) = 50 pos = f(t0);pos; //=> 100; (surprise!)
computational functions do things
mathematical functions"establish true relations which remain true
![Page 21: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/21.jpg)
mathematical variables"are names we give to values
t0 = 5t0 = 5t0 = 6
computational variables"are like names for places, whose contents can change
NSNumber * five = @5;
five = @6; // ?!
five = @7;
[five setIntegerValue:8]; // ?! ?!
![Page 22: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/22.jpg)
FP style boils down to• Restrictions to emulate the
predictability of mathematical functions and variables
• Idioms to use functions for all abstraction — in defining other functions, in hiding information, etc.
• (Sometimes) type systems to provide compile-time checks on the values moving through functions
![Page 23: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/23.jpg)
• purity and immutability guarantees
• function literals • function closures • functions as first-class values
• enums (aka, sum types, tagged unions) • algebraic data types • type inference • fancy types: first-order, recursive,
dependent, higher-kinded, constrained, etc..
FEATURES
• Idioms • combine, pass, & return
functions like other values
• Restrictions • pure functions • immutable data
• Type systems (sometimes) • to check valid values
STYLE supported by
![Page 24: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/24.jpg)
Haskell (1988)
Scheme (1975)SML (1984)ML (1973)
Scala (2003)
OCaml (1996)
Clojure (2009) F# (2005)
Erlang (1986)
dynamic static
Coq, Agda, Idris
![Page 25: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/25.jpg)
So is Swift “functional”?
![Page 26: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/26.jpg)
Haskell SML Scheme Clojure Swift ObjC
purity & immutability help Yes, very. Y Y Y Y meh
function literals, closures, first-classness Y Y Y Y Y meh
expression-oriented Y Y Y Y N N
proper tail calls Y Y Y N ? ?
memory management Y Y Y Y meh meh
generics Y Y N N Y N
enum types Y Y N N Y N
type inference Y Y N N Y N
algebraic data types Y Y N N ? N
super DUPER fancy types Y ? N N N N
![Page 27: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/27.jpg)
If this were 1998 Swift would be a
niche functional language
![Page 28: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/28.jpg)
If this were 1998 Swift would be a
niche functional language
… but maybe we’re all functional now? or could be?
![Page 29: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/29.jpg)
Should we care?
![Page 30: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/30.jpg)
functional Swift: (embarrassingly trivial) HOFs
![Page 31: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/31.jpg)
Higher'order*func/ons:*filter
ObjC:
NSArray * inArray = @[@"Hello",@"Ciao"];NSArray * outArray = [inArray filteredArrayUsingPredicate: [NSPredicate predicateWithBlock: ^BOOL(NSString * item, NSDictionary *bindings) { return [item hasPrefix:@"Hello"]; }]];
Swi$:
let inArray = ["Hello","Ciao"];let outArray = inputArray.filter({ $0.hasPrefix("Hello") })
![Page 32: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/32.jpg)
Higher'order*func/ons:*map
ObjC:
NSArray * inArray = @[@"John Appleseed", @"Sally Appleseed"];NSMutableArray * outArray = [NSMutableArray array];for (NSString * name in inArray) { [outArray addObject:[name componentsSeparatedByString:@" "][0]];}
Swi$:
let inArray = ["John Appleseed", "Sally Appleseed"];let outArray = inArray.map({ $0.componentsSeparatedByString(" ")[0] })
![Page 33: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/33.jpg)
Higher'order*func/ons:*reduce
reduce!is!a!HOF!for!processing!a!collec1on!into!a!single!value.
Swi$:
let x = [1,2,3,4]let sum = x.reduce(0, +);sum // => 7let product = x.reduce(1, *);product //=> 24
![Page 34: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/34.jpg)
Closures: variable capture on purposefunc makeCounter() -> (() -> NSInteger) {! var x = 0! func increment() -> NSInteger {! x = x + 1;! return x;! }! return increment;!}!!var counter1 = makeCounter()!var counter2 = makeCounter()!!counter1() //=> 1!counter1() //=> 2!counter1() //=> 3!!counter2() //=> 1!
![Page 35: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/35.jpg)
functional Swift: JSON as ADT
![Page 36: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/36.jpg)
JSONNSJSONSerialization Class Reference
… An object that may be converted to JSON must have the following properties: • The top level object is an NSArray or NSDictionary. • All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
• All dictionary keys are instances of NSString. • Numbers are not NaN or infinity. !
Other rules may apply. Calling isValidJSONObject: or attempting a conversion are the definitive ways to tell if a given object can be converted to JSON data.
![Page 37: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/37.jpg)
JSON%as%algebraic%data%type
Enumera(on*with*associated*values
enum JSONValue { case JNumber(NSNumber) case JString(String) case JBool(Bool) case JNull case JArray(Array<JSONValue>) case JObject(Dictionary<String,JSONValue>) case JInvalid(NSError) // ...}
Source:(h*ps://github.com/lingoer/Swi7yJSON
![Page 38: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/38.jpg)
functional Swift: Improving on NSError
![Page 39: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/39.jpg)
NSError&is&ugly
ObjC:
NSError *error;NSData *data = [NSData dataWithContentsOfFile:file options:0 error:&error];if(data == nil) { // handle error}
![Page 40: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/40.jpg)
Either'type
Swi$:
enum DataResult { case Success(NSData) case Error(NSError) // ...}switch NSData(contentsOfFile:file, options:0) { case .Success(let data): // handle data here then return case .Error(let error): // handle error here then return}
![Page 41: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/41.jpg)
SwiftZ (by maxpow4h)Either and Result:!// Result represents something that could work or be an NSError. // Say we have 2 functions, the first fetches from a web services, // the second decodes the string into a User. // Both *could* fail with an NSError, so we use a Result<A>. func getWeb() -‐> Result<String> { var e: NSError? let str = doStuff("foo", e) return Result(e, str) } !func decodeWeb(str: String) -‐> Result<User> { var e: NSError? let user = decode(str, e) return Result(e, user) } !// We can compose these two functions with the `>>=` function. !let getUser: Result<User> = getWeb() >>= decodeWeb !switch (getUser) { case let .Error(e): println("NSError: \(e)") case let .Value(user): println(user.name) }
![Page 42: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/42.jpg)
Is ObjC going away? Is it “easier” now?
![Page 43: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/43.jpg)
Objective-C
• enums with associated values • option types • pattern matching • generics • type inference • immutability supports • tuples
Swift+• structs • namespaces • operator
overloading • ObjC interop • Swift values &
collections
Swift+Swift
• classes • methods • protocols • extensions • functions • ARC • closures • Cocoa values
& collections
![Page 44: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/44.jpg)
–"Advanced Swift Debugging in LLDB", WWDC2014, session 410, 20m—28m.
“Objective-C isn't really going anywhere.... Even if you start a brand new Swift app for the first time today after this session, you're going to use Cocoa, or Cocoa Touch. You're going to import Foundation. You're going to import UIKit. Those frameworks are written in Objective-C. That means, wherever you look around, there's going to be Objective-C in the picture. And you're going to have to deal with debugging mixed Swift / Objective-C situations..”
![Page 45: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/45.jpg)
Canaries in the ObjC coal mine
• Swift achieves genuine C-like performance
• Swift-only types appearing at public API boundaries
• Deprecation of the ObjC runtime’s more dynamic features
![Page 46: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/46.jpg)
• GitHub RAC devs (jspahrsummers, joshaber, & others)
• ReactiveCocoa. This work is indirectly influenced by typed FP.<https://github.com/ReactiveCocoa/ReactiveCocoa>
• Maxwell Swadling
• SwiftZ. Currently translating lots of typed FP idioms into a Swift library. <https://github.com/maxpow4h/swiftz>
• David Nolen.
• Om. Functional approach to writing GUIs in the browser, using ClojureScript with Facebook’s React, leveraging CSP-style concurrency. Untyped FP.<http://swannodette.github.io/2013/12/17/the-future-of-javascript-mvcs/>
• Upcoming book from ObjC.io authors.
![Page 47: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/47.jpg)
![Page 48: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/48.jpg)
@end
![Page 49: Swift, functional programming, and the future of Objective-C](https://reader033.vdocuments.us/reader033/viewer/2022051411/53fe33eb8d7f72db2d8b4658/html5/thumbnails/49.jpg)
• Videos, notes, and homework from the Coursera “Programming Languages” course, a great intro to Scheme, SML, and Ruby. <https://class.coursera.org/proglang-2012-001>
• Interesting way to do SICP: http://understudyapp.com
• The talks by Rich Hickey (inventor of Clojure) are very rich