Becoming a f(P) master

(a.k.a. Functional Programming concepts explained by Pokémon)

RubyConf Online 2015

Hi!

  • Computer Engineer
  • Programming
  • Electronics
  • Math <3 <3
  • Physics
  • Lego
  • Meetups
  • Animals
  • Coffee
  • GIFs

DISCLAIMER

Introductory content

Pokémon, GIFs and theory

Whys are people talking so much about

Functional Programming?

Let me try:

WAT

It's simple:

  • Lambda calculus
  • Lisp
  • Haskell
  • Scala
  • Clojure
  • Papers
  • Academia
  • Category Theory
  • λ
  • λ
  • λ

Pokémon

We give instructions to Pokémon in a battle

Programming

We also give instructions to code

i = i+1;

Pokémon

We must know how some things work:

Programming

We must know how to manage information in our software

  • Data types
  • Evolution
  • How to group them
  • Important attributes
  • Business rules
  • Pokémon types
  • Evolution
  • Team strategy
  • EV/IV
  • Moves
  • Items
  • Trades

level >=16

f(evol)

output level is the same as the input

How can I get a Charizard from a Charmander?

level >=16

f(evol)

output level is the same as the input

output level is the same as the input

f(up)

level = 36

level=36

f(evol)

level = 36

Or:

level >=36

f(evol)

output level is the same as the input

f(evol)

output level is the same as the input

High Order Function

f(x) => g(x) => h(x)

In some competitions, there is a maximum value for the sum of the levels of the Pokémon on the team

Team

  • Charizard, lvl 100
  • Lapras, lvl 100
  • Dragonite, lvl 100
  • Rhydon, lvl 100
  • Pidgeotto, lvl 100
  • Gengar, lvl 100

Level sum: 600

  • Gengar, lvl 100
  • Charizard, lvl 100
  • Lapras, lvl 100
  • Rhydon, lvl 100
  • Pidgeotto, lvl 100
  • Dragonite, lvl 100

Level sum: 600

  • Lapras, lvl 100
  • Gengar, lvl 100
  • Charizard, lvl 100
  • Rhydon, lvl 100
  • Pidgeotto, lvl 100
  • Dragonite, lvl 100
  • Pidgeotto, lvl 100
  • Lapras, lvl 100
  • Gengar, lvl 100
  • Dragonite, lvl 100
  • Charizard, lvl 100
  • Rhydon, lvl 100
Credits: @bitemyapp and @argumatronic - http://haskellbook.com/

sumEverythong AndGiveTheResult([100,100, 100,100,100,100])

fold|reduce|other_names

(reduce + [100 100 100 100 100 100])  ;;=> 600

What else can you have on your party rather than battle Pokémon?

Eggs

Team

  • Charizard, lvl 100
  • Lapras, lvl 100
  • Dragonite, lvl 100
  • Egg
  • Pidgeotto, lvl 100
  • Gengar, lvl 100

Level sum: 500

Egg does not interfere on the sum

Egg level = 0

Does not change the value

Egg works as an identity element to the level. 

  • Gengar, lvl 100
  • Charizard, lvl 100
  • Lapras, lvl 100
  • Egg
  • Pidgeotto, lvl 100
  • Dragonite, lvl 100

Level sum: 500

  • Lapras, lvl 100
  • Gengar, lvl 100
  • Charizard, lvl 100
  • Egg
  • Pidgeotto, lvl 100
  • Dragonite, lvl 100
  • Pidgeotto, lvl 100
  • Lapras, lvl 100
  • Gengar, lvl 100
  • Dragonite, lvl 100
  • Charizard, lvl 100
  • Egg

reduce

(reduce + 0[100 100 100 100 100])  ;;=> 500

identity element

collection

MONOIDS

Game items

Game items

[

]

Collection

Game Items

[

]

Collection

(map upgrade                        )  


;;=>

[

]

Game Items

[0]

[1]

Optional

Game Items

Optional

(map change              )  


;;=>
[
]
[
]

Game Items

[

]

In some part of the game you will have the badges

Future

Game Items

[

]

(map other_cont              )  


;;=>

[

]

Future

[ Pokéballs]

[ Bike ]

[ Badges ]

=>

=>

=>

[ Pokéballs]

[ Bike ]

[ Badges ]

[ Pokéballs]

[ Bike ]

[ Badges ]

=>

=>

=>

[ Pokéballs]

[ Bike ]

[ Badges ]

FUNCTORS

Move set

and moves

{Surf: 15},

{Fissure: 5},

{Solarbeam: 10},

{Eruption: 5}

Moves

[

]

{Surf: 15},

{Fissure: 5},

{Solarbeam: 10},

{Eruption: 5}

Moves

decrease_pp_used_move[{Surf: 15}]

[

]

[{Surf: 14}],

{Fissure: 5},

{Solarbeam: 10},

{Eruption: 5}

[

]

[]

[            ]

[                     ]

{Surf: 15},

{Fissure: 5},

{Solarbeam: 10},

{Eruption: 5}

Moves

[

]

{Surf: 14},

{Fissure: 5},

{Solarbeam: 10},

{Eruption: 5}

(flatten decrease_pp [{Surf:15}])  


;;=> [{Surf: 14}, {Fissure: 5}, {Solarbeam: 10}, {Eruption: 5}]

Collection

Item

[Scope Lens]

boost_move

{Fissure:5},

{Surf:15},

{Solarbeam: 10},

{Eruption: 5}

[

]

{Fissure:5},

[{Surf:15:boosted}],

{Solarbeam: 10},

{Eruption: 5}

[

]

Optional

Item

[Scope Lens]

{Fissure:5},

{Surf:15},

{Solarbeam: 10},

{Eruption: 5}

[

]

Optional

(flatten boost_move [{Surf:15}]
;;=> [{Fissure:5}, {Surf: 15:boosted}, {Solarbeam: 10}, {Eruption: 5}]

{Fissure:5},

{Surf:15},

{Solarbeam: 10},

{Eruption: 5}

[

]

Future

{Fissure:5},

{Surf:15},

{Solarbeam: 10},

{Eruption: 5}

[

]

Moves

forget

{Fissure:5},

[],

{Solarbeam: 10},

{Eruption: 5}

[

]

{Fissure:5},

{Surf:15},

{Solarbeam: 10},

{Eruption: 5}

[

]

Future

(flatten forget [{Surf:15}]
;;=> [{Fissure:5}, {Solarbeam: 10}, {Eruption: 5]}

{Fissure:5},

{Surf:15},

{Solarbeam: 10},

{Eruption: 5}

[

]

Moves

No matter which move set you have (if it exists, if you have items, if you haven't learned the move yet):

  • Get the element
  • Apply a function if convenient
  • flatten
  • Allow call chain

MONADS

"Meownads!" (credits with @_leticia)

Quick: Where do I use that?

More credits and references:

  • @lafp (wonderful GIFs)
  • @bitemyapp, @argumatronic and @FunctionalWorks
  • @txustice - http://blog.txus.io/
  • http://www.amazon.com/Functional-Programming-Practice-Bruce-Maclennan/dp/0201137445/ref=sr_1_9?ie=UTF8&qid=1445967378&sr=8-9&keywords=functional+programming
  • http://media.eurucamp.org/ (@eurucamp)
  • @_leticia ('Meownads')
  • http://stackoverflow.com/questions/2704652/monad-in-plain-english-for-the-oop-programmer-with-no-fp-background

Thank you! :)

Questions?

 

hannelita@gmail.com

@hannelita

RubyconfOnline pokemons_fp_en

By Hanneli Tavante (hannelita)

RubyconfOnline pokemons_fp_en

  • 936
Loading comments...

More from Hanneli Tavante (hannelita)