Slide 1

Slide 1 text

Functions A Love Story Josh Abernathy

Slide 2

Slide 2 text

Functions Legos A Love Story Josh Abernathy

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Big things are made of many little things

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Functions! A Love Story!

Slide 11

Slide 11 text

Functions are the Lego block of programming. — Someone, probably.

Slide 12

Slide 12 text

Small building blocks

Slide 13

Slide 13 text

Big things are made of many little things

Slide 14

Slide 14 text

Functions!

Slide 15

Slide 15 text

func myFunction()

Slide 16

Slide 16 text

[...] a relation between a set of inputs and a set of permissible outputs with the property that each input is related to exactly one output. — Wikipedia

Slide 17

Slide 17 text

Consistent Relation

Slide 18

Slide 18 text

func CollectionType.dropFirst(n: Int) -> Self.SubSequence

Slide 19

Slide 19 text

Easy to reason about

Slide 20

Slide 20 text

Easy to compose

Slide 21

Slide 21 text

Func-y

Slide 22

Slide 22 text

func UIViewController.showViewController(vc: UIViewController, sender: AnyObject?)

Slide 23

Slide 23 text

Abstracted unit of work

Slide 24

Slide 24 text

Hard to reason about

Slide 25

Slide 25 text

Compose?

Slide 26

Slide 26 text

Functions!

Slide 27

Slide 27 text

Functions easy in the small

Slide 28

Slide 28 text

let myThings = originalThings.map { String("My Thing: \($0)") }

Slide 29

Slide 29 text

class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow! func applicationDidFinishLaunching(notification: NSNotification) { let output = runMyApp(input: ???) doSomethingWithOutput(output) } } func runMyApp(input: ???) -> ??? { }

Slide 30

Slide 30 text

Functions not so easy in the large

Slide 31

Slide 31 text

Are apps functions?

Slide 32

Slide 32 text

func myApp(input: ???) -> ???

Slide 33

Slide 33 text

wc -- word, line, character, and byte count func wc(input: String) -> Int

Slide 34

Slide 34 text

ls -- list directory contents func ls(currentDirectory: NSURL = default) -> [NSURL]

Slide 35

Slide 35 text

$ ls | wc -l

Slide 36

Slide 36 text

myApp -- social thought leadership through counting clicks func myApp(input: ???) -> ???

Slide 37

Slide 37 text

What is the input?

Slide 38

Slide 38 text

Touches + Camera + Network + Sensors (GPS, etc.) + Data on disk + … = State

Slide 39

Slide 39 text

What is the output?

Slide 40

Slide 40 text

UI (Mostly)

Slide 41

Slide 41 text

UIs are big, messy, mutable, stateful bags of sadness. — me, probably

Slide 42

Slide 42 text

func renderApp(state: State) -> UI

Slide 43

Slide 43 text

State change ← renderApp

Slide 44

Slide 44 text

State change ← renderApp State change ← renderApp State change ← renderApp

Slide 45

Slide 45 text

State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp State change ← renderApp

Slide 46

Slide 46 text

func renderApp(state: State) -> UI

Slide 47

Slide 47 text

Big things are made of many little things

Slide 48

Slide 48 text

func renderLogin(state: LoginState) -> UI func renderPhotos(state: PhotosState) -> UI func renderButton(title: String) -> UI func renderVerticalList(UIs: [UI]) -> UI

Slide 49

Slide 49 text

struct Button { let title: String ... } struct VerticalList { let children: [UI] }

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

UI is composition

Slide 52

Slide 52 text

Big functions are made of many little functions

Slide 53

Slide 53 text

Big UIs are made of many little UIs

Slide 54

Slide 54 text

class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow! func applicationDidFinishLaunching(notification: NSNotification) { doSomeMagic(render: renderApp, view: window.contentView) } } func renderApp(state: Int) -> UI { return renderVerticalList([ renderLabel("Click count: \(state)"), renderButton("Click me!", action: { updateState(state + 1) }), ]) }

Slide 55

Slide 55 text

React

Slide 56

Slide 56 text

React Native

Slide 57

Slide 57 text

ComponentKit

Slide 58

Slide 58 text

! Few !

Slide 59

Slide 59 text

Example

Slide 60

Slide 60 text

let appComponent = Component(initialState: 0, render: renderApp) class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow! func applicationDidFinishLaunching(notification: NSNotification) { appComponent.addToView(window.contentView) } } func renderApp(component: Component, state: Int) -> Element { return View( backgroundColor: .orangeColor()) .justification(.Center) .childAlignment(.Center) .direction(.Column) .children([ Label("You've clicked \(state) times!"), Button(title: "Click me!", action: { component.updateState { $0 + 1 } }) .margin(Edges(uniform: 10)) .width(100), ]) }

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

func renderApp(component: Component, state: Int) -> Element { return View( backgroundColor: .orangeColor()) .justification(.Center) .childAlignment(.Center) .direction(.Column) .children([ Label("You've clicked \(state) times!"), Button(title: "Click me!", action: { component.updateState { $0 + 1 } }) .margin(Edges(uniform: 10)) .width(100), ]) }

Slide 63

Slide 63 text

Now

Slide 64

Slide 64 text

Separate the calculation from the doing

Slide 65

Slide 65 text

Separate the function from the func-y

Slide 66

Slide 66 text

GitHub Desktop

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

@mdiep

Slide 69

Slide 69 text

struct Graph { let currentBranch: Branch? let compareBranch: Branch? ... } struct GraphUpdate { let type: GraphUpdateType init(oldGraph: GHComparisonGraph?, newGraph: GHComparisonGraph) ... }

Slide 70

Slide 70 text

// Graph.swift let newGraph = calculateGraph(repository) let update = GraphUpdate(oldGraph: oldGraph, newGraph: newGraph) // GraphViewController.swift switch (update.type) { case .Append: // do the needful ... }

Slide 71

Slide 71 text

Separate the calculation from the doing

Slide 72

Slide 72 text

$ ls | wc -l

Slide 73

Slide 73 text

Compose apps?

Slide 74

Slide 74 text

$ alias instagram="camera | filters"

Slide 75

Slide 75 text

$ alias my_app="touches | instagram"

Slide 76

Slide 76 text

There are a lot of unknowns

Slide 77

Slide 77 text

That's OK

Slide 78

Slide 78 text

See Also — "Boundaries" by Gary Bernhardt — "Tangible Functional Programming" by Conal Elliott — "Refactor the Mega-Controller" by Andy Matuschak — Most anything by Rich Hickey

Slide 79

Slide 79 text

Thanks!