object StateMonadExample {
def main(args: Array[String]) {
val startlist = List(1,2,3,4,5)
val finalstate = stateParse
val resultingList = finalstate ! startlist
println(resultingList)
if(resultingList == List(-1, 3, -3, 5, -5))
println("success")
else
println("fail!")
}
def stateParse: State[List[Int], List[Int]] = state[List[Int], List[Int]]( list => {
list.headOption match {
case None =>
(list, List[Int]()) //lets not infinitely recurse
case Some(i:Int) if (i%2 == 0) =>
(for(a <- pullAndAdd;
b <- stateParse)
yield(a :: b))(list)
case _ =>
(for(a <- pullAndInvert;
b <- stateParse)
yield (a :: b))(list)
}
})
def pullAndInvert = state[List[Int], Int](li => (li.tail, -li.headOption.getOrElse(0)))
def pullAndAdd = state[List[Int], Int](li => (li.tail, 1+li.headOption.getOrElse(0)))
/* copied from Scalaz for convenience, this is not my code, it's from Scalaz */
sealed trait State[S, +A] {
def apply(s: S): (S, A)
def state[S, A](f: S => (S, A)): State[S, A] = new State[S, A] {
def apply(s: S) = f(s)
}
def map[B](f: A => B): State[S, B] = state(apply(_) match {
case (s, a) => (s, f(a))
})
def flatMap[B](f: A => State[S, B]): State[S, B] = state(apply(_) match {
case (s, a) => f(a)(s)
})
def !(s: S): A = apply(s)._2
def ~>(s: S): S = apply(s)._1
def withs(f: S => S): State[S, A] = state(f andThen (apply(_)))
}
def state[S, A](f: S => (S, A)): State[S, A] = new State[S, A] {
def apply(s: S) = f(s)
}
}
import scala.xml._
import scala.xml.pull._
import scala.io.Source
object EventToNodeParser {
def main(args:Array[String]) {
val str = "<tag attr='somestuff'><sub:subtag><mostnested>SomeText</mostnested></sub:subtag><secondsubtag attr='blah'></secondsubtag><thirdsubtab></thirdsubtab></tag>"
var events = List[XMLEvent]()
val reader = new XMLEventReader(Source.fromString(str))
while (reader.hasNext) {
events = events :+ reader.next
}
println( "events = " + events)
val i = parseXMLEvents ! events
println("i = " + i)
}
def parseXMLEvents: State[List[XMLEvent], List[Node]] = state[List[XMLEvent], List[Node]]( list => {
list.headOption match {
case None =>
(list, List[Node]())
case Some(EvText(txt))=>
(list.tail, List[Node](Text(txt)))
case Some(EvElemStart(prefix,label,attr, scope)) =>
(for
{
children <- getChildren(label)
nodes <- parseXMLEvents
}
yield (new Elem(prefix, label, attr, scope, children:_*) :: nodes))(list.tail)
case _ =>
parseXMLEvents(list.tail)
}
})
def getChildren(endLabel: String) = state[List[XMLEvent], List[Node]](list => {
val (sublist, newlist) = list.span( _ match {
case EvElemEnd(_,label) if (label == endLabel)=>
false
case _ => true
})
sublist.headOption match {
case Some(e:XMLEvent) =>
(newlist, parseXMLEvents ! sublist)
case _ =>
(list, List[Node]())
}
})
/* copied from Scalaz for convenience */
sealed trait State[S, +A] {
def apply(s: S): (S, A)
def state[S, A](f: S => (S, A)): State[S, A] = new State[S, A] {
def apply(s: S) = f(s)
}
def map[B](f: A => B): State[S, B] = state(apply(_) match {
case (s, a) => (s, f(a))
})
def flatMap[B](f: A => State[S, B]): State[S, B] = state(apply(_) match {
case (s, a) => f(a)(s)
})
def !(s: S): A = apply(s)._2
def ~>(s: S): S = apply(s)._1
def withs(f: S => S): State[S, A] = state(f andThen (apply(_)))
}
def state[S, A](f: S => (S, A)): State[S, A] = new State[S, A] {
def apply(s: S) = f(s)
}
}--
You received this message because you are subscribed to the Google Groups "scala-functional" group.
To unsubscribe from this group, send email to scala-function...@googlegroups.com.