38
Functional Programming beyond map/filter/reduce JFokus 2018

Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Functional Programmingbeyond map/filter/reduce

JFokus 2018

Page 2: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Prof. Dierk König canoo

mittie

Page 3: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Monadsare just

monoids in the category of endofunctors.

Page 4: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Explanation PathMonoid it's simple

Functor you already know it

Endo- cool idea

Monoid of endofunctors = monad

Page 5: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Integers (+)2 + 3 == 5 2 + 0 == 2 0 + 3 == 3 ( 1+2 )+3 == 1+( 2+3 )

Page 6: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Integers (*)2 * 3 == 6 2 * 1 == 2 1 * 3 == 3 ( 2*3 )*4 == 2*( 3*4 )

Page 7: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Boolean (&&)a && b == c a && true == a true && b == b ( a && b ) && c == a &&( b && c )

Page 8: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Lists, Arrays (+)[1] + [2] == [1, 2] [1] + [ ] == [1] [ ] + [2] == [2] ([1]+[2])+[3] == [1]+([2]+[3])

Page 9: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Strings (+)"1" + "2" == "12" "1" + "" == "1" "" + "2" == "2" ( "Hi"+"," )+"JFokus" == "Hi"+( ","+"JFokus" )

Page 10: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Extracttype <> type => type

neutral element

left & right identity

associative

Page 11: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

AbstractWe call this concept "Monoid"short for "that thing, you know, that you can combine with another thing of the same type to produce a new thing of the type of the other two as long as there is a value of that type that when combined left or right does not change the value you combined that special value with and any such combination must yield the same result no matter which two you combine first."

Page 12: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

LEGO Monoid

Page 13: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

LEGO Monoid

Page 14: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

LEGO Monoid

Page 15: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

LEGO Neutral Brick

Page 16: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

LEGO Left & Right Identity

Page 17: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Generic Fold Right

Page 18: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Generic Fold Right

Page 19: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Generic Fold Right

Page 20: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Generic Fold Right

Page 21: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Generic Optimized Fold

Page 22: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Generic Optimized Fold

Page 23: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Generic Optimized Fold

Page 24: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Crazy Monoids (a -> b) <> (b -> c) == (a -> c)

Can a function type (a->b) be a monoid?

What is the operation?

What is the neutral element?

Is it associative?

Page 25: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Function Compositionco(f,g) = x => f(g(x));

id(x) = x;

co(id,g) == x => id(g(x)) // definition co == x => g(x) // apply id == g // qed

Page 26: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Functors map[1, 2, 3].map( x=> x+2 ) == [3, 4, 5]

(Just 1).map( x=> x+2 ) == Just 3

Nothing.map( whatever ) == Nothing

List, Tree, Stream, Optional, Pipeline, Error, Validation, Observable, Future, Promise,..

Page 27: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Functors compose[1, 2, 3].map( x => x + 2 ) .map( x => 3 * x ) ==[1, 2, 3].map( x => 3 * (x+2) )

co( functor.map(f), functor.map(g) ) == functor.map( co(f, g) )

functor.map(id) == id // only for completeness

Page 28: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Functors are not Monoids[1, 2, 3] .map( x => x.toString() ) == ["1","2","3"]

[Int] .map( Int -> String ) -> [String]

functor a .map( a -> b) -> functor b

Page 29: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Clever Idea:instead of (a->b) provide a special mapping with (a -> functor b),which is essentially a constructor for functors.

functor a <> functor b => functor b functor a .endo(a->functor b) => functor b

Page 30: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Clever Idea in Action[1, 2, 3].endo( x => replicate(x, x.toString()) ) == [ ["1"], ["2","2"], ["3","3","3"] ]

then flatten => ["1", "2", "2", "3", "3", "3"] //aka "join"

funcA.flatMap(f) = funcB.flatten(funcA.endo(f))

// aka "bind" or ">>="

Page 31: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Finalising the Monoidfunctor a .flatMap(a->functor b) => functor b (a->functor a) <> (a->functor b) => (a->functor b)

We need an (a-> functor a) ctor and flatMap (we already have map, so we only need flatten). If the functor is monoidal with flatMap as <> and ctor as neutal element, then we call it a Monad.

Page 32: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Wrapping upLet (m a) be a given monad over type a.(m a) has a ctor (a -> m a). // aka "return" or "pure"(m a) is a functor over a. (m (m a)) can be flattened to (m a).(a-> m a).flatMap( a -> m b) is a monoidal operation.

Alternative way of writing in pure FP style (>>=) :: Monad m => m a -> (a -> m b) -> m b

Page 33: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

TakeawaysAssociativity (monoid) requires pure functions.

Monoids can fold but they cannot escape.

Monads are the most versatile functors (map, filter, expand, reduce) that composes and folds without escaping.

Page 34: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Use CasesPurely functional state threadsList comprehensions, Streams (possibly reactive) CompletableFuture, Promises, ContinuationsLINQ-style database access Either, Validation, Exception handlingOptionality, Indeterminism, Parsers, Search treesSequencing IO actions, isolating UI actionsSTM transactions, …

Page 35: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

No IO in Transactions!

Page 36: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

Type inference FTWLesstricky

errors

Page 37: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

There is a world…… where logical reasoning rules and structure arises from consistency and a rich set of relations.

Exploring this world is like programming without implementation.

Purely functional programming opens the door.Consider Haskell, Frege, Purescript, Idris.

Page 38: Functional Programming beyond map/filter/reduce · 2019-09-11 · are just monoids in the category of endofunctors. Explanation Path Monoid it's simple Functor you already know it

mittie

Please give feedback! Prof. Dierk König canoo