This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Node(id:Int) | |
type SyncResponse = Either[String,List[Node]] | |
class Network { | |
var nodes: ListBuffer[Node] = ListBuffer.empty | |
val rng = scala.util.Random | |
/** | |
* Removes dead nodes | |
*/ | |
def purge:SyncResponse = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo{ | |
private var fooState = 0 | |
def next:Int = { fooState = fooState + 1; fooState } | |
def getFooState:Int = fooState | |
} | |
val foo = new Foo() | |
foo.next | |
foo.next | |
val result = foo.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalaz._ | |
import Scalaz._ | |
object StateTLearning extends App with StateTFunctions { | |
case class DB(v: Int) | |
val initial = DB(1) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sum(i:Int) = StateChange[Better,Int](s => Better.sum(i)(s)) | |
// Lets run the sum computation, and add all the results together | |
def composeAddResultS(one:Int,two:Int,three:Int): StateChange[Better,Int] = | |
sum(one).doAgainWithNewState( | |
firstResult => | |
sum(two).doAgainWithNewState( secondResult => | |
sum(three).mapResult( thirdResult => firstResult + secondResult + thirdResult) )) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object UseScalaz { | |
def main(args: Array[String]): Unit = { | |
import scalaz._ | |
case class Better(total: List[Int]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// we notice S => (S,A) is useful, almost composable and testable, but awkward... lets do better | |
// Lets create a type that wraps the computation, and add some useful combinators: mapResult, doAgainWithResultAndNewState | |
case class StateChange[S,A](run: S => (S,A)){ | |
def mapResult[B](f:A => B):StateChange[S,B] = StateChange{ (s:S) => | |
val(s2,a) = run(s) | |
(s2,f(a)) | |
} | |
/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// we desire this pattern Better => (Better,Int) | |
type BetterUpdate = Better => (Better,Int) | |
val firstF:BetterUpdate = Better.sum(1) | |
val secondF:BetterUpdate = Better.sum(2) | |
val thirdF:BetterUpdate = Better.sum(3) | |
// the external state is threaded through a sequence of computations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Better(total : List[Int]) | |
object Better{ | |
def sum(i:Int)(b:Better):(Better,Int) = { | |
val result = if(b.total.length > 1) 99 else b.total.sum | |
val newBetter = b.copy(total = i :: b.total) | |
(newBetter, result) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TraditionalMutation{ | |
var total : List[Int] = Nil // Mutable List | |
/* | |
Add i to total, return it's sum | |
This method hides the effect of updating total | |
*/ | |
def sum(i:Int):Int = { | |
total = i :: total // SIDE EFFECT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object MutableBad{ | |
class MDate(dayIn:Int){ var day = dayIn } | |
type Task = Int | |
def scheduleTask(task:Task,date:MDate) = ??? | |
var d = new MDate(22) |
OlderNewer