swift, functional programming, and does it matter? · 2018-11-01 · swift, functional programming,...

43
Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher

Upload: others

Post on 03-Jun-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

Swift, functional programming,

and does it matter?

Alexis Gallagher @alexisgallagher

Page 2: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

Questions

• What’s new in Swift?

• Is Swift a functional programming language?

• And what is “functional” anyway?

• How useful is this really?

Page 3: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

What's'hardly'new'in'Swi,?

Page 4: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

Objective-C

• classes • methods • protocols • categories • functions • ARC • blocks • Cocoa values

& collections

Page 5: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

Objective-C

Swift

• classes • methods • protocols • extensions • functions • ARC • closures • Cocoa values

& collections

Page 6: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

What's'a'bit$new'in'Swi,?

Page 7: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

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

Page 8: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

What's'really&new'in'Swi,?

Page 9: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

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

Page 10: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

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

Page 11: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

Every “really new” part of Swift originated in a functional programming

language of the 1980s or earlier.

Page 12: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

FP

Page 13: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

JavaScript*is*brilliant*and*is*succeeding*...*and*I*think*it's*because*

of*the*func:onal*stuff.—"Douglas"Crockford,"2014

Page 14: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

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 15: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

a"programming"styletrea%ng(the

func%onas#the#primary#unit#of#abstrac2on

Page 16: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

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

Page 17: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

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]; // ?! ?!

Page 18: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

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 19: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

• 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 20: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

Haskell (1988)

Scheme (1975)SML (1984)ML (1973)

Scala (2003)

OCaml (1996)

Clojure (2009) F# (2005)

Erlang (1986)

dynamic static

Coq, Agda, Idris

Page 21: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

So is Swift “functional”?

Page 22: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

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 23: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

If this were 1998 Swift would be a

niche functional language

Page 24: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

“Swift is not functional” —Rob Napier

June 10th, 2014

“I, for one, welcome our new Haskell overloads” — Rob Napier Sep 29th, 2014

Page 25: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

“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

Page 26: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

Should we care?

Page 27: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”
Page 28: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

<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.

Page 29: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

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

Page 30: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

“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

Page 31: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”
Page 32: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”
Page 33: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”
Page 34: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

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.

Page 35: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”
Page 36: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

• 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..

Page 37: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

• 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/>

Page 38: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

@end

Page 39: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

Is ObjC going away? Is it “easier” now?

Page 40: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

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

Page 41: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

–"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 42: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

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 43: Swift, functional programming, and does it matter? · 2018-11-01 · Swift, functional programming, and does it matter? Alexis Gallagher @alexisgallagher. Questions ... Languages”

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?