I'm trying to start using scalaz in my lift project. For that purpose I'm rewriting some code to meet the style. Consider a code for logging in a user:
def login: CssSel = {
var password = ""
def submit() {
if (doLogin) S.redirectTo("/index")
else S.error("Wrong password")
}
"name=pwd" #> SHtml.password(password, password = _) &
"type=submit" #> SHtml.onSubmitUnit(submit)
}
So, this should be rewritten using a state monad somehow. But I just don't get, how. Trying this:
val result = for {
s <- init[String]
pass <- SHtml.password(s, put(_))
newPass <- init[String]
res <- "name=pwd" #> pass &
"type=submit" #> SHtml.onSubmit { _ =>
if (User.logIn("username", newPass)) S.redirectTo("/index")
else S.error("Wrong password")
}
} yield (newPass, res)
result ! ""
UPD: Updated example, according to answers.
Any good tutorials/explanations on state monads in scalaz, showing how to use gets, put, etc?
vars in your code. For this to work you must have some means of chaining the state changes which the callback methods of Lift’sSHtmldo not support. (I think the whole concept of Lift is precisely that you use variables hidden in many closures.) I’d be happy to be proven wrong, though, and see a working example of this. - Debilski