2012年12月15日土曜日

UndoT

この記事はScalaz Advent Calendarの15日目の記事です。

scalazのcoreのjarに含まれ、独自のパッケージが存在しながらも、全く話題に上がらないundoについて書きます。

scalaz.undo以下にはUndoTとHistoryの2つのデータ型があります。

UndoはHistoryを状態とするStateモナドで、Historyはundo, redoの為のListと、currentのデータを持ちます。

例を作ってみます。

import scalaz._, Scalaz._
import scalaz.effect._, IO._
object Undo extends SafeApp {
import scalaz.undo._, UndoT._
override def runc = for {
(s1, s2) <- (for {
_ <- hput[IO, String]("hello")
_ <- hput[IO, String]("world")
current = UndoT(get[History[String]].lift[IO])
h1 <- undo[IO, String] >> current
h2 <- redo[IO, String] >> current
} yield h1.current -> h2.current).eval("initialize")
_ <- putStrLn(s1) >> putStrLn(s2)
} yield ()
}
view raw undo.scala hosted with ❤ by GitHub
この時、Historyはこのように変遷しています。

History("initialize", Nil, Nil) // eval("initialize")
History("hello", List("initialize"), Nil) // hput("hello")
History("world", List("hello", "initialize"), Nil) // hput("world")
History("hello", List("initialize"), List("world")) // undo
History("world", List("hello", "initialize"), Nil) // redo


これを使っているユーザーがどれだけいるのか気になるところです。

これだけでは物足りないと思ったので、MonadStateについて書こうと思いましたが、UndoTのMonadStateのインスタンスの定義がおかしい気がする。

なので今日はpull requestを投げて終わります。

0 件のコメント:

コメントを投稿