Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

理解 Either 和 Option #11

Open
zhongl opened this issue Aug 8, 2013 · 0 comments
Open

理解 Either 和 Option #11

zhongl opened this issue Aug 8, 2013 · 0 comments
Labels

Comments

@zhongl
Copy link
Member

zhongl commented Aug 8, 2013

这儿有道题, 从实际的项目中简化而来:

case class User(id: Long, nickname: String, password: String, mobile: String)

object Users {
  def userBy(mobile: String): Option[User] = 
    if (mobile == "01234567890") Some(User(1L, "allen", "******", "01234567890")) else None

  def authenticate(mobile: String, password: String): Either[User, String] = ???
}

assert(Users.authenticate("allen", "******") == Right("Invalid mobile"))
assert(Users.authenticate("01234567891", "******") == Right("Mobile not found"))
assert(Users.authenticate("01234567890", "??????") == Right("Invalid password"))
assert(Users.authenticate("01234567890", "******") == Left(User(1L, "allen", "******", "01234567890")))

请根据assert提示实现authenticate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant