Skip to content

Instantly share code, notes, and snippets.

@danbills
Created May 26, 2017 11:59
Show Gist options
  • Save danbills/81b09b2cdcfa0676329f117a0855e7b8 to your computer and use it in GitHub Desktop.
Save danbills/81b09b2cdcfa0676329f117a0855e7b8 to your computer and use it in GitHub Desktop.
| \gTraversing Data Structures using TypeClasses
---
| \gRecap
--
Semigroup
A,A => A
associative combine function
--
Monoid
A,A => A
zero
---
| \gFoldable
foldMap A => Monoid
--
monoid instance waiting in the wings (in "implicit scope")
---
| \gFoldable strong enough?
A => F[B] perhaps?
--
Something that can do something interesting w/ F
---
|\gExample
Have a list of inputs or potential inputs
--
Quarters to to a candy machine
---
|\gApplicative by example
--
Amazing applicatives:
State
Task
---
|\gState
generally the equation is as such:
--
//
val differentway_depending_onsomething_else: StateOperation[
]
```
---
|\gState Example
To create a State:
--
1) Create a type of out thin air
--
case class GumballMachine(
bank_of_coins: Int,
number_of_candies_to_dispense: Int,
door_locked: Boolean)
type WorldMachine[A] = State[GumballMachine, A]
//implicit val ICanHandleWorldState = Applicative[WorldState]
```
--
now write a bunch of functions that can take values and manipulate these
worlds
```
sealed trait Input
case object TurnKnob extends Input
case object Coin
```
now when someone calls traverse we can handle the output!
--
---
| \gState Recap
you get to declare the state of the universe as a single, well defined type
--
you get to consider unassociated data alongside the universe, and make a
decision what to do with the universe
--
you get to output a value of your choosing in an independent unassociated format
---
| \gTraverse
map function to a "value wrapped in another value"?
---
| \gTypeclass
when B has an instance in implicit scope that can do something with B
---
Credits
Based on Wadler's paper
Candy machine example from FP in Scala
This is know as
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment