Swift, functional programming, and does it matter?

Alexis Gallagher @alexisgallagher Questions

• What’s new in Swift?

• Is Swift a functional ?

• And what is “functional” anyway?

• How useful is this really? What's'hardly'new'in'Swi,? Objective-C

• classes • methods • protocols • categories • functions • ARC • blocks • Cocoa values & collections Objective-C

Swift

• classes • methods • protocols • extensions • functions • ARC • closures • Cocoa values & collections What's'a'bit$new'in'Swi,? Objective-C

Swift + Swift

• classes • structs • methods • namespaces • protocols • access control • extensions • operator • functions overloading • ARC • ObjC interop • closures • Swift values & • Cocoa values collections & collections What's'really&new'in'Swi,? Objective-C

Swift + Swift + Swift

• classes • structs • enums with associated values • methods • namespaces • option types • protocols • access control • pattern matching • extensions • operator • generics • functions overloading • type inference • ARC • ObjC interop • immutability supports • closures • Swift values & • tuples • Cocoa values collections & collections Objective-C

Swift + Swift + Swift

• classes • structs • enums with associated values • methods • namespaces • option types • protocols • access control • pattern matching • extensions • operator • generics • functions overloading • type inference • ARC • ObjC interop • immutability supports • closures • Swift values & • tuples • Cocoa values collections & collections Every “really new” part of Swift originated in a functional programming language of the 1980s or earlier. FP JavaScript*is*brilliant*and*is* succeeding*...*and*I*think*it's*because* of*the*func:onal*stuff.

—"Douglas"Crockford,"2014 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. a"programming"style trea%ng(the func%on as#the#primary#unit#of#abstrac2on mathematical functions computational functions establish true relations do things which remain true

t0 =5 NSNumber * t0 = @5;

NSNumber * pos = f(t0); f(t0) = 50 pos; // => 50 f(t ) = 50 pos = f(t0); 0 pos; //=> 100; (surprise!) mathematical variables computational variables are names are like names for places, we give to values whose contents can change t0 =5 NSNumber * five = @5; t =5 five = @6; // ?! 0 five = @7; t0 =6 [five setIntegerValue:8]; // ?! ?! 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 STYLE supported by FEATURES

• Restrictions • purity and immutability guarantees • pure functions • immutable data • function literals • Idioms • function closures • combine, pass, & return • functions as first-class values functions like other values • enums (aka, sum types, tagged unions) • Type systems (sometimes) • algebraic data types • to check valid values • type inference • fancy types: first-order, recursive, dependent, higher-kinded, constrained, etc.. dynamic static Scheme (1975) ML (1973) SML (1984)

Erlang (1986) Haskell (1988)

OCaml (1996)

Scala (2003)

F# (2005) Clojure (2009)

Coq, Agda, Idris So is Swift “functional”? Haskell SML Scheme Clojure Swift ObjC purity & immutability help Yes, very. Y Y Y Y meh function literals, closures, Y Y Y Y Y meh first-classness 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 If this were 1998 Swift would be a niche functional language “Swift is not functional” June 10th, 2014 —Rob Napier

“I, for one, welcome our new Haskell overloads” Sep 29th, 2014 — Rob Napier “Objective-C without the C” implies something subtractive, but Swift dramatically expands the design space through the introduction of generics and functional programming concepts.” — Chris Lattner Should we care?

value of FP

• A useful toolbox for processing data, collections and streams.

• A salutary discipline for simplicity. Pure functions, plain values. When this is enough, it’s also the best. And it’s often enough.

• Enums & protocols interfaces clarify thinking, but types can complicate coding. (Safety benefits overrated.)

• FRP, ReactiveCocoa, functional GUI — I remain hopeful, skeptical. Objective-C

Swift + Swift + Swift

• classes • structs • enums with associated values • methods • namespaces • option types • protocols • access control • pattern matching • extensions • operator • generics • functions overloading • type inference • ARC • ObjC interop • immutability supports • closures • Swift values & • tuples • Cocoa values collections & collections “Objective-C without the C” implies something subtractive, but Swift dramatically expands the design space through the introduction of generics and functional programming concepts.” — Chris Lattner

FP != type systems

Where Swift/Cocoa fight FP • Support for immutable values, but…

• Inexpressive. Needs hash & equals boilerplate for value objects.

• Inexpressive. No support for “updated copy” of such value objects, nor of dictionaries, nor any KVC on structs.

• Unperformant? No functional data structures.

• Not expression-oriented. (e.g., switch returns no value)

• Cocoa is subclasserific.

• Videos, notes, and exercises from the Coursera “Programming Languages” course, a great intro to Scheme, SML, and Ruby.

• The talks by Rich Hickey (inventor of Clojure) are very rich: the Value of Values, the Language of the System, etc.. • Chris Eidhof, Florian Kugler, and Wouter Swierstra

• Functional Programming in Swift

• GitHub RAC devs (jspahrsummers, joshaber, & others)

• ReactiveCocoa. This work is indirectly influenced by typed FP.

• Maxwell Swadling

• SwiftZ. Currently translating lots of typed FP idioms into a Swift library.

• David Nolen.

• Om. Functional approach to writing GUIs in the browser, using ClojureScript with Facebook’s React, leveraging CSP-style concurrency. Untyped FP. @end Is ObjC going away? Is it “easier” now? Objective-C

Swift + Swift + Swift

• classes • structs • enums with associated values • methods • namespaces • option types • protocols • access control • pattern matching • extensions • operator • generics • functions overloading • type inference • ARC • ObjC interop • immutability supports • closures • Swift values & • tuples • Cocoa values collections & collections “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..” –"Advanced Swift Debugging in LLDB", WWDC2014, session 410, 20m—28m. 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 Swift is mostly conveniences

• Swift’s support for FP are essentially conveniences vs ObjC:

• unified syntax for value types and references types, for methods, functions, and closures,

• type inference

• easier declarations of immutability

• More substantive: enums with associated values. Type system?