【发布时间】:2021-10-20 02:07:23
【问题描述】:
我目前正在实现业务逻辑代码,该代码依赖于自定义 scala 库,该库有很多不纯的函数,我们有很多代码依赖于这个库并且无法重构,因为它会影响其他人。因此,我正在尝试编写尽可能纯的 scala 代码,并且需要针对少数用例的建议。
我有一个执行以下操作的 LDAP 实用程序库 根据用户名和密码进行认证并返回布尔值,但是如果有连接异常或未找到用户则直接抛出异常。 根据 UUID 和参数(如名字和姓氏)读取和更新数据的作用几乎相同 我的身份验证用例方法如下
def authenticateToUpdate(userName:String,password:String):Option[Boolean] =
Some(ldapService.authenticate(username,password))
// or using either type to get the message as well
def authenticateToUpdate(userName:String,password:String):Either[String,Boolean]=
(ldapService.authenticate(username,password)) match {
case true => Right(true)
case false => Left("Validation failed, Invalid Password")
case exception: Exception => Left(exception.toString)
}
//##for Some type
authenticateToUpdate(userName,password).fold(InvalidPassword)(updateOrder(orderId,userName))
//##for Either type
// Looking into how to implement
所以我想尽可能将我的代码编写为函数式和接近纯函数的代码,需要帮助和建议如何处理这种情况来编写函数式 scala 代码。
【问题讨论】:
-
上面的代码示例与我的工作代码非常相似,但不一样,请忽略未知变量名和其他。因为我想展示我的方法,并想知道我做对了
-
如果库抛出异常,将调用包装在
Try
标签: scala functional-programming purely-functional