scalazのcoreのjarに含まれ、独自のパッケージが存在しながらも、全く話題に上がらないundoについて書きます。
scalaz.undo以下にはUndoTとHistoryの2つのデータ型があります。
UndoはHistoryを状態とするStateモナドで、Historyはundo, redoの為のListと、currentのデータを持ちます。
例を作ってみます。
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._, 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 () | |
} |
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 件のコメント:
コメントを投稿