swift, functional programming, and does it matter? · 2018-11-01 · swift, functional programming,...
TRANSCRIPT
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
• structs • namespaces • access control • operator
overloading • ObjC interop • Swift values &
collections
Swift+Swift
• classes • methods • protocols • extensions • functions • ARC • closures • Cocoa values
& collections
What's'really&new'in'Swi,?
Objective-C
• enums with associated values • option types • pattern matching • generics • type inference • immutability supports • tuples
Swift+• structs • namespaces • access control • operator
overloading • ObjC interop • Swift values &
collections
Swift+Swift
• classes • methods • protocols • extensions • functions • ARC • closures • Cocoa values
& collections
Objective-C
• enums with associated values • option types • pattern matching • generics • type inference • immutability supports • tuples
Swift+• structs • namespaces • access control • operator
overloading • ObjC interop • Swift values &
collections
Swift+Swift
• classes • methods • protocols • extensions • functions • ARC • closures • Cocoa values
& 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"styletrea%ng(the
func%onas#the#primary#unit#of#abstrac2on
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 functionsestablish true relations which remain true
mathematical variablesare names we give to values
t0 = 5t0 = 5t0 = 6
computational variablesare like names for places, whose contents can change
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
• 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
Haskell (1988)
Scheme (1975)SML (1984)ML (1973)
Scala (2003)
OCaml (1996)
Clojure (2009) F# (2005)
Erlang (1986)
dynamic static
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” —Rob Napier
June 10th, 2014
“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
• enums with associated values • option types • pattern matching • generics • type inference • immutability supports • tuples
Swift+• structs • namespaces • access control • operator
overloading • ObjC interop • Swift values &
collections
Swift+Swift
• classes • methods • protocols • extensions • functions • ARC • closures • Cocoa values
& 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
• enums with associated values • option types • pattern matching • generics • type inference • immutability supports • tuples
Swift+• structs • namespaces • access control • operator
overloading • ObjC interop • Swift values &
collections
Swift+Swift
• classes • methods • protocols • extensions • functions • ARC • closures • Cocoa values
& collections
–"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..”
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?