Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher
Questions What s new in Swift? Is Swift a functional programming language? 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 methods protocols extensions functions ARC closures Cocoa values & collections structs namespaces access control operator overloading ObjC interop Swift values & 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 establish true relations computational functions do things which remain true t 0 =5 NSNumber * t0 = @5; f(t 0 ) = 50 NSNumber * pos = f(t0); pos; // => 50 f(t 0 ) = 50 pos = f(t0); pos; //=> 100; (surprise!)
mathematical variables are names we give to values computational variables are like names for places, whose contents can change t 0 =5 t 0 =5 t 0 =6 NSNumber * five = @5; five = @6; //?! five = @7; [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 pure functions immutable data purity and immutability guarantees Idioms combine, pass, & return functions like other values function literals function closures functions as first-class values enums (aka, sum types, tagged unions) Type systems (sometimes) to check valid values algebraic data types type inference fancy types: first-order, recursive, dependent, higher-kinded, constrained, etc..
dynamic Scheme (1975) static ML (1973) SML (1984) Erlang (1986) Haskell (1988) OCaml (1996) Scala (2003) Clojure (2009) F# (2005) 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, 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
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 Rob Napier Sep 29th, 2014
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?
<opinion>value of FP</opinion> 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. <https://class.coursera.org/proglang-2012-001> 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. <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/>
@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?