# 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

# Let me try:

WAT

## It's simple:

• Lambda calculus
• Lisp
• Scala
• Clojure
• Papers
• 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
• Pokémon types
• Evolution
• Team strategy
• EV/IV
• Moves
• Items

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

# Higher 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`

# Eggs

## Team

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

# 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`

# Collection

```(map upgrade                        )

```
`;;=>`

# Optional

```(map change              )

```
`;;=>`
`[`
`]`
`[`
`]`

# ]

In some part of the game you will have the badges

# ]

```(map other_cont              )

```
`;;=>`

# 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}

[

]

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}

[

]

{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

"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)