Slide 1

Slide 1 text

Parallel Universes Reader & Coreader for Dependency Injection Ì INCL. COMONADS, MONADS & THE DISTANT PAST Elmar Kretzer @elmkretzer

Slide 2

Slide 2 text

- talk to network - use Date() - persist data Does your app even: - you name it Why care?

Slide 3

Slide 3 text

HOW TO? ... wire it together? ... test it peacefully? ... set it up?

Slide 4

Slide 4 text

highly flexible duplication of code mental overhead error prone Keep It Pass On 1 2

Slide 5

Slide 5 text

3 OPTIONS: „DO IT MANUALLY“ „USE A FRAMEWORK“ „DO SOMETHING ELSE“

Slide 6

Slide 6 text

Let’s talk about „something else“ Functional Programming Weirder names for weird concepts. Some/None/Few/All people like it.

Slide 7

Slide 7 text

But Swift does use/allow/force functional patterns. Sometimes you don’t even notice. Swift Guess who’s not a Functional PL:

Slide 8

Slide 8 text

empowered by fp

Slide 9

Slide 9 text

FP & DI – Cool & How?

Slide 10

Slide 10 text

http://hackage.haskell.org/package/mtl-1.1.0.2/docs/Control-Monad-Reader.html „Represents a computation, which can read values from a shared environment, pass values from function to function, and execute sub-computations in a modified environment.“ 1/2 READER MONAD

Slide 11

Slide 11 text

very abstract return sth. based on sth. else afterwards pass it on to get sth. that itself returns sth. based on sth. else

Slide 12

Slide 12 text

Describe dependent operations. Provide How at the end. 100 + 10 + 5 + 2 100 * 10 * 5 * 2 100 - 10 - 5 - 2 100 / 10 / 5 / 2

Slide 13

Slide 13 text

„In more technical language the Reader comonad is left adjoint to the Reader monad.“ 2/2 READER COMONAD https://hackage.haskell.org/package/category-extras-0.53.0/docs/Control-Comonad-Reader.html

Slide 14

Slide 14 text

very co-abstract hold sth. and sth. else afterwards get sth. new from sth. and sth. else and return the new with sth. else

Slide 15

Slide 15 text

Describe dependent access. Provide from WHAT at the end. RealWorld -> Api -> Server -> Time MockWorld - > Api -> Server -> Time

Slide 16

Slide 16 text

Reader vs Coreader Comonad extend M a -> M a -> b -> M b Produce -> Consume Cokleisli Monad flatMap M a -> a -> M b -> M b Consume -> Produce Kleisli

Slide 17

Slide 17 text

Coreader No. B/C Controller, Storyboards and so forth. Reader Yes. No one is holding you back. Worth it & simple to use in iOS?

Slide 18

Slide 18 text

What do you do? Corridor
 µFramework for a Coreader-like 
 access to dependencies. Combined with Reader: full-fledged control. github.com/symentis/Corridor

Slide 19

Slide 19 text

Pro? Unified Handling of DI. No business code for passing on Dependencies. Dependencies are transparent. Testing simply takes different Environments. Less code. More safety.

Slide 20

Slide 20 text

Follow-Up article including gists on: http://elm4ward.github.io/ Corridor on GitHub: https://github.com/symentis/Corridor Me on Twitter: @elmkretzer thanks for your time!