The Hipster Librarian's Guide to Teen Craft Projects (Hipster Librarian's Guide To...)
Hipster oriented programming (Mobilization Lodz 2015)
-
Upload
jens-ravens -
Category
Software
-
view
811 -
download
0
Transcript of Hipster oriented programming (Mobilization Lodz 2015)
hipster oriented programming
Jens Ravens
Developer at nerdgeschoss iOS/ OS X (Swift) Server / Backend (Ruby)
@JensRavens jensravens.de
nerdgeschoss.de
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
Is it really functional programming if you don't give a talk about it?
- @alejandrocrosa
This talk is not about functional programming.
What is a hipster anyway?
a person who follows the latest trends and fashions, especially those regarded as being outside the cultural mainstream.
- Wikipedia
orientation
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 }
Functional
Same input, same output.
let l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] let sum = l.reduce(+) // 55
Object Oriented
Put your family of objects in a room and let them talk.
Protocol Oriented
You have a lot of equatable, comparable, parseable, archiveable, transmittable, displayable objects.
Spaghetti Oriented
JSON parsing.
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) } }
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
They all mix pretty well.
- pretty big secret
bridging the worlds
Monads.http://fuckingmonads.com
Signals.jensravens.de/series/functional-reactive-programming-in-swift/
Boxing and Optionals.
Throwing Errors.
Mapping Arrays and Collections.
Type Safety.
Protocols and Facades.
combining ideas
idea 1
treating your user as an interface
user interface
RecipeRecipe
RecipeRecipe
Recipe
protocol User { func selectRecipe(recipies: [Recipe], completion:Recipe->Void) }
user interface
RecipeRecipe
RecipeRecipe
Recipe
protocol User { func selectRecipe(recipies: [Recipe], completion:Recipe?->Void) }
user interface
RecipeRecipe
RecipeRecipe
Recipe
protocol User { func selectSingle(collection: [Selectable], completion:Selectable?->Void) }
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 }
idea 2
facades via typedef
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") }
idea 3
common behavior in protocols
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? }
extension Contextable { func viewControllerForContext(context: Item) -> UIViewController? { return self as? UIViewController } func contextForGestureRecognizer(gestureRecognizer: UIGestureRecognizer) -> Item? {
// find the clicked indexpath } }
extension ChannelViewController: Contextable { @IBAction func triggerContext(gestureRecognizer: UIGestureRecognizer) { showContext(gestureRecognizer) } func contextForIndexPath(indexPath: NSIndexPath) -> Item? { return dataSource.itemForIndexPath(indexPath) } }
These are just concepts.They won’t hurt you, they won’t take away your job.
It’s more out there than just object oriented programming.
object oriented programming has a lot of hidden gems you might not be aware of yet.
learning from other plattforms
these are amazing times to be a developer
do not just read about your plattform
What’s .next()?
Functors, Applicatives and Monads in Pictures.
http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html
The Introduction to RP you’ve been missing.
https://gist.github.com/staltz/868e7e9bc2a7b8c1f754
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/
keep searching for new frontiers
use the right tool for the right job