Hipster oriented programming (Mobilization Lodz 2015)

54
hipster oriented programming

Transcript of Hipster oriented programming (Mobilization Lodz 2015)

Page 1: Hipster oriented programming (Mobilization Lodz 2015)

hipster oriented programming

Page 2: Hipster oriented programming (Mobilization Lodz 2015)

Jens Ravens

Developer at nerdgeschoss iOS/ OS X (Swift) Server / Backend (Ruby)

@JensRavens jensravens.de

nerdgeschoss.de

Page 3: Hipster oriented programming (Mobilization Lodz 2015)

I hate monads already. Even if it’s the most useful, elegant thing - it’s all what’s wrong about hype based hipster-programming in one word.

- tweet after UIKonf

Page 4: Hipster oriented programming (Mobilization Lodz 2015)

Is it really functional programming if you don't give a talk about it?

- @alejandrocrosa

Page 5: Hipster oriented programming (Mobilization Lodz 2015)

This talk is not about functional programming.

Page 6: Hipster oriented programming (Mobilization Lodz 2015)

What is a hipster anyway?

Page 7: Hipster oriented programming (Mobilization Lodz 2015)

a person who follows the latest trends and fashions, especially those regarded as being outside the cultural mainstream.

- Wikipedia

Page 8: Hipster oriented programming (Mobilization Lodz 2015)

orientation

Page 9: Hipster oriented programming (Mobilization Lodz 2015)

Imperative

Do this 5 times!

func nullToNil(dict: [String: AnyObject])->[String: AnyObject]{ var cleaned = [String: AnyObject]() for (key, value) in dict { if !(value is NSNull) { cleaned[key] = value } } return cleaned }

Page 10: Hipster oriented programming (Mobilization Lodz 2015)

Functional

Same input, same output.

let l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] let sum = l.reduce(+) // 55

Page 11: Hipster oriented programming (Mobilization Lodz 2015)

Object Oriented

Put your family of objects in a room and let them talk.

Page 12: Hipster oriented programming (Mobilization Lodz 2015)

Protocol Oriented

You have a lot of equatable, comparable, parseable, archiveable, transmittable, displayable objects.

Page 13: Hipster oriented programming (Mobilization Lodz 2015)

Spaghetti Oriented

JSON parsing.

Page 14: Hipster oriented programming (Mobilization Lodz 2015)

What is state anyway?

class ViewController: UIViewController, UITableViewDelegate { var selectedItem: Item? func updateView() { // do something to update the display } func tableView(tableView: UITableView,

didSelectRowAtIndexPath indexPath: NSIndexPath) { selectedItem = findItemAtIndex(indexPath) } }

Page 15: Hipster oriented programming (Mobilization Lodz 2015)

Question State How to recognise?

IP imperative

How? a lot of nested loops and assignments

OOP object oriented

How? (but encapsulated)

encapsulated state

seperation of concerns “unix principle”

POP protocol oriented

What? encapsulated objects implementing hundreds of protocols.

FP functional

What? no state. you have no idea what it’s doing, but it’s short and beautiful.

FRP functional reactive

When? over my dead lambda. you’re constantly flatMapping

Page 16: Hipster oriented programming (Mobilization Lodz 2015)

They all mix pretty well.

- pretty big secret

Page 17: Hipster oriented programming (Mobilization Lodz 2015)

bridging the worlds

Page 18: Hipster oriented programming (Mobilization Lodz 2015)

Monads.http://fuckingmonads.com

Page 19: Hipster oriented programming (Mobilization Lodz 2015)

Signals.jensravens.de/series/functional-reactive-programming-in-swift/

Page 20: Hipster oriented programming (Mobilization Lodz 2015)

Boxing and Optionals.

Page 21: Hipster oriented programming (Mobilization Lodz 2015)

Throwing Errors.

Page 22: Hipster oriented programming (Mobilization Lodz 2015)

Mapping Arrays and Collections.

Page 23: Hipster oriented programming (Mobilization Lodz 2015)

Type Safety.

Page 24: Hipster oriented programming (Mobilization Lodz 2015)

Protocols and Facades.

Page 25: Hipster oriented programming (Mobilization Lodz 2015)

combining ideas

Page 26: Hipster oriented programming (Mobilization Lodz 2015)

idea 1

treating your user as an interface

Page 27: Hipster oriented programming (Mobilization Lodz 2015)

user interface

RecipeRecipe

RecipeRecipe

Recipe

protocol User { func selectRecipe(recipies: [Recipe], completion:Recipe->Void) }

Page 28: Hipster oriented programming (Mobilization Lodz 2015)

user interface

RecipeRecipe

RecipeRecipe

Recipe

protocol User { func selectRecipe(recipies: [Recipe], completion:Recipe?->Void) }

Page 29: Hipster oriented programming (Mobilization Lodz 2015)

user interface

RecipeRecipe

RecipeRecipe

Recipe

protocol User { func selectSingle(collection: [Selectable], completion:Selectable?->Void) }

Page 30: Hipster oriented programming (Mobilization Lodz 2015)

user interface

RecipeRecipe

RecipeRecipe

Recipe

func downloadStuff(term: String, completion: Result<[Recipe]>->Void){ // networking and error handling }

class UserSelection: UIViewController, User { static func selectRecipe(presentingViewController: UIViewController) (recipies: [Recipe], completion: Result<Recipe> -> Void) { // present your view controller here and save the completion block } }

let searchVC = // your current vc let textField = // the textfield used for typing textField .textSignal .flatMap(downloadStuff) .flatMap(UserSelection.selectRecipe(searchVC)) .next { recipe in // do whatever you like } .error { error in // something went wrong on the way }

Page 31: Hipster oriented programming (Mobilization Lodz 2015)

idea 2

facades via typedef

Page 32: Hipster oriented programming (Mobilization Lodz 2015)

abstract servers

public typealias Headers = [String: String] public typealias Environment = [String: AnyObject] public typealias StatusCode = Int public typealias Response = (StatusCode, Headers, ContentStreamable)

public typealias Swagable = Request throws -> Response

let HelloWorld: Swagable = { env in return (200, [:], "Hello World") }

Page 33: Hipster oriented programming (Mobilization Lodz 2015)

idea 3

common behavior in protocols

Page 34: Hipster oriented programming (Mobilization Lodz 2015)

protocol Contextable { func showContext(gestureRecognizer: UIGestureRecognizer) func viewControllerForContext(context: Item) -> UIViewController? func contextViewControllerForContext(context: Item) -> UIViewController? func contextForGestureRecognizer(gestureRecognizer: UIGestureRecognizer) -> Item? // the only required method func contextForIndexPath(indexPath: NSIndexPath) -> Item? }

Page 35: Hipster oriented programming (Mobilization Lodz 2015)

extension Contextable { func viewControllerForContext(context: Item) -> UIViewController? { return self as? UIViewController } func contextForGestureRecognizer(gestureRecognizer: UIGestureRecognizer) -> Item? {

// find the clicked indexpath } }

Page 36: Hipster oriented programming (Mobilization Lodz 2015)

extension ChannelViewController: Contextable { @IBAction func triggerContext(gestureRecognizer: UIGestureRecognizer) { showContext(gestureRecognizer) } func contextForIndexPath(indexPath: NSIndexPath) -> Item? { return dataSource.itemForIndexPath(indexPath) } }

Page 37: Hipster oriented programming (Mobilization Lodz 2015)

These are just concepts.They won’t hurt you, they won’t take away your job.

Page 38: Hipster oriented programming (Mobilization Lodz 2015)

It’s more out there than just object oriented programming.

Page 39: Hipster oriented programming (Mobilization Lodz 2015)

object oriented programming has a lot of hidden gems you might not be aware of yet.

Page 40: Hipster oriented programming (Mobilization Lodz 2015)

learning from other plattforms

Page 41: Hipster oriented programming (Mobilization Lodz 2015)

these are amazing times to be a developer

Page 42: Hipster oriented programming (Mobilization Lodz 2015)

do not just read about your plattform

Page 43: Hipster oriented programming (Mobilization Lodz 2015)
Page 44: Hipster oriented programming (Mobilization Lodz 2015)
Page 45: Hipster oriented programming (Mobilization Lodz 2015)
Page 46: Hipster oriented programming (Mobilization Lodz 2015)
Page 47: Hipster oriented programming (Mobilization Lodz 2015)

What’s .next()?

Page 48: Hipster oriented programming (Mobilization Lodz 2015)

Functors, Applicatives and Monads in Pictures.

http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

Page 49: Hipster oriented programming (Mobilization Lodz 2015)

The Introduction to RP you’ve been missing.

https://gist.github.com/staltz/868e7e9bc2a7b8c1f754

Page 50: Hipster oriented programming (Mobilization Lodz 2015)

Imperative Programming In Swift (Graham Lee)

sicpers.info/2015/07/imperative-programming-in-swift/

Protocol-Oriented Programming in Objective-Csicpers.info/2015/06/protocol-oriented-programming-in-objective-c/

Mutable objects in immutable objects in object-oriented programming in functional programming in Swiftsicpers.info/2015/06/mutable-objects-in-immutable-objects-in-object-oriented-programming-in-functional-programming-in-swift/

Page 51: Hipster oriented programming (Mobilization Lodz 2015)

keep searching for new frontiers

Page 52: Hipster oriented programming (Mobilization Lodz 2015)

use the right tool for the right job

Page 53: Hipster oriented programming (Mobilization Lodz 2015)
Page 54: Hipster oriented programming (Mobilization Lodz 2015)

Thank you.

@JensRavensjensravens.de