多选项Scala模式匹配的有效方法
我有下面的函数,它根据一些条件返回一个标志 试着看看是否有更好的Scala方法来达到同样的效果 基本上,根据人员的状态,最终将应用不同的规则来推导pass\u ind标志 如果个人身份是学生或失业,则对收入有一些规定;如果个人身份不是学生或失业,则对分数和收入有不同的规定多选项Scala模式匹配的有效方法,scala,Scala,我有下面的函数,它根据一些条件返回一个标志 试着看看是否有更好的Scala方法来达到同样的效果 基本上,根据人员的状态,最终将应用不同的规则来推导pass\u ind标志 如果个人身份是学生或失业,则对收入有一些规定;如果个人身份不是学生或失业,则对分数和收入有不同的规定 def IncomeScreenStatus(status_cd: Option[String], score: Option[Int],
def IncomeScreenStatus(status_cd: Option[String],
score: Option[Int],
income: Option[Double]) : String = {
var pass_ind : String = "F"
if (score.isDefined && income.isDefined && status_cd.isDefined) {
val score : Int = score.get
val income : Double = income.get
val status_cd : String = status_cd.get
if (status_cd == "STUDENT" || status_cd == "UNEMPLOYMENT") {
pass_ind = (status_cd, income) match {
case ("UNEMPLOYMENT", income) if income <= 7000 => "P"
case ("STUDENT", income) if income <= 18000 => "P"
case _ => "F"
}
}
else {
pass_ind = (income, score) match {
case (income, score) if (score < 100 || score > 150) && income <= 8500 => "P"
case (income, score) if (score <= 167 && score >= 100) && income <= 10500 => "P"
case _ => "F"
}
}
}
pass_ind
}
def IncomeScreenStatus(状态:选项[String],
分数:选项[Int],
收入:选项[Double]:字符串={
var pass\u ind:String=“F”
if(score.isDefined&&income.isDefined&&status\u cd.isDefined){
val分数:Int=score.get
val收入:Double=income.get
val status\u cd:String=status\u cd.get
如果(状态_cd==“学生”| |状态_cd==“失业”){
通过索引=(状态、收入)匹配{
案例(“失业”,收入),如果收入为“P”
如果收入为“P”,则案例(“学生”,收入)
案例=>“F”
}
}
否则{
通过(收入、分数)比赛{
案例(收入、分数)如果(分数<100 | |分数>150)和收入“P”
案例(收入,分数)如果(分数=100)和收入“P”
案例=>“F”
}
}
}
传球
}
也许是这样的?(我没有试过,不能保证它是正确的。)
def IncomeScreenStatus(mb_status_cd:Option[String],mb_score:Option[Int],mb_income:Option[Double]):String={
def nsu(分数:整数,收入:双倍)={//既不是学生也不是失业者
if((分数<100 | |分数>150)和收入nsu(分数,收入)
案例=>“F”
}
}
可能是这样的?(我没有试过,不能保证它是正确的。)
def IncomeScreenStatus(mb_status_cd:Option[String],mb_score:Option[Int],mb_income:Option[Double]):String={
def nsu(分数:整数,收入:双倍)={//既不是学生也不是失业者
if((分数<100 | |分数>150)和收入nsu(分数,收入)
案例=>“F”
}
}
这是第一条裂缝
def IncomeScreenStatus(status_cd: Option[String]
,score : Option[Int]
,income : Option[Double]) : String = {
for {
stat <- status_cd
scr <- score
incm <- income
} yield stat match {
case "UNEMPLOYMENT" => if (incm <= 7000) "P" else "F"
case "STUDENT" => if (incm <= 18000) "P" else "F"
case _ => if ((scr < 100 || scr > 150) && incm <= 8500 ||
(scr <= 167 && scr >= 100) && incm <= 10500) "P" else "F"
}
}.getOrElse("F")
def IncomeScreenStatus(状态\u cd:选项[字符串]
,分数:选项[Int]
,收入:选项[Double]):字符串={
为了{
统计这是第一条裂缝
def IncomeScreenStatus(status_cd: Option[String]
,score : Option[Int]
,income : Option[Double]) : String = {
for {
stat <- status_cd
scr <- score
incm <- income
} yield stat match {
case "UNEMPLOYMENT" => if (incm <= 7000) "P" else "F"
case "STUDENT" => if (incm <= 18000) "P" else "F"
case _ => if ((scr < 100 || scr > 150) && incm <= 8500 ||
(scr <= 167 && scr >= 100) && incm <= 10500) "P" else "F"
}
}.getOrElse("F")
def IncomeScreenStatus(状态\u cd:选项[字符串]
,分数:选项[Int]
,收入:选项[Double]):字符串={
为了{
stat首先,我对Scala采用的推荐代码样式没有什么意见
- 变量/函数/定义名称应为
camelCase
incomeScreenStatus
而不是incomeScreenStatus
statusCd
而不是status\u cd
passInd
而不是pass\u ind
现在,
def incomeScreenStatus(状态CD:Option[String],
分数:选项[Int],
收入:选项[Double]:字符串={
(分数、收入、状态)匹配{
案例(一些(得分),一些(收入),一些(状态CDV))=>{
(状态CDV、incomeV、scoreV)匹配{
案例(“失业”,如果收入为“p”
案例(“学生”,如果收入为“p”
案例-if(得分<100 | |得分>150)和收入“P”
案例uuif(分数=100)和收入“P”
案例=>“F”
}
}
案例=>“F”
}
}
首先,我对Scala推荐采用的代码样式没有什么意见
- 变量/函数/定义名称应为
camelCase
incomeScreenStatus
而不是incomeScreenStatus
statusCd
而不是status\u cd
passInd
而不是pass\u ind
现在,
def incomeScreenStatus(状态CD:Option[String],
分数:选项[Int],
收入:选项[Double]:字符串={
(分数、收入、状态)匹配{
案例(一些(得分),一些(收入),一些(状态CDV))=>{
(状态CDV、incomeV、scoreV)匹配{
案例(“失业”,如果收入为“p”
案例(“学生”,如果收入为“p”
案例-if(得分<100 | |得分>150)和收入“P”
案例uuif(分数=100)和收入“P”
案例=>“F”
}
}
案例=>“F”
}
}
在函数式编程中,最好使用小函数而不是大函数。我更喜欢做以下事情:
def screenStudent(income:Double):String = {
if (income <= 18000) "P" else "F"
}
def screenUnemployment(income:Double):String = {
if (income <= 7000) "P" else "F"
}
def screenOthers(income:Double, score:Int):String = {
(income, score) match {
case (income, score) if (score < 100 || score > 150) && income <= 8500 => "P"
case (income, score) if (score <= 167 && score >= 100) && income <= 10500 => "P"
case _ => "F"
}
}
def incomeScreenStatus(
status_cd: Option[String],
score: Option[Int],
income: Option[Double]
) : String =
(status_cd , score, income) match {
case (Some("STUDENT"), Some(_), Some(i)) => screenStudent(i)
case (Some("UNEMPLOYMENT"), Some(_), Some(i)) => screenUnemployment(i)
case (Some(_), Some(s), Some(i)) => screenOthers(i, s)
case _ => "F"
}
def屏幕学生(收入:双倍):字符串={
如果(收入“F”
}
}
def incomeScreenStatus(
状态光盘:选项[字符串],
分数:选项[Int],
收入:期权[双倍]
):字符串=
(状态、得分、收入)匹配{
案例(一些(“学生”)、一些(u)、一些(i))=>屏幕学生(i)
案例(部分(“失业”)、部分(u)、部分(i))=>屏幕失业(i)
案例(一些,一些,一些(i))=>screenOthers(i,s)
案例=>“F”
}
在函数式编程中,最好使用小函数而不是大函数。我更喜欢做以下事情:
def screenStudent(income:Double):String = {
if (income <= 18000) "P" else "F"
}
def screenUnemployment(income:Double):String = {
if (income <= 7000) "P" else "F"
}
def screenOthers(income:Double, score:Int):String = {
(income, score) match {
case (income, score) if (score < 100 || score > 150) && income <= 8500 => "P"
case (income, score) if (score <= 167 && score >= 100) && income <= 10500 => "P"
case _ => "F"
}
}
def incomeScreenStatus(
status_cd: Option[String],
score: Option[Int],
income: Option[Double]
) : String =
(status_cd , score, income) match {
case (Some("STUDENT"), Some(_), Some(i)) => screenStudent(i)
case (Some("UNEMPLOYMENT"), Some(_), Some(i)) => screenUnemployment(i)
case (Some(_), Some(s), Some(i)) => screenOthers(i, s)
case _ => "F"
}
def屏幕学生(收入:双倍):字符串={
如果(收入“F”
}
}
def incomeScreenStatus(
状态光盘:选项[字符串],
分数:选项[Int],
收入:期权[双倍]
):字符串=
(状态、得分、收入)匹配{
案例(一些(“学生”)、一些(u)、一些(i))=>屏幕学生(i)
案例(一些(“失业”)、一些(u)、一些(i))=>screenUnemplo
object Example {
sealed trait Status
case object Student extends Status
case object Unemployed extends Status
case object Other extends Status
object PoorStudent {
def unapply(income: Int) = if (income <= 18000) Some(income) else None
}
object PoorUnemployed {
def unapply(income: Int) = if (income <= 7000) Some(income) else None
}
object IncomeScoreRange {
def unapply(score: Int, income: Double) =
if (((score < 100 || score > 150) && income <= 8500) || (income <= 10500 && (100 to 167 contains score)))
Some((score, income))
else
None
}
def compute(status: Option[Status], score: Option[Int], income: Option[Double]): String = {
(status, score, income).zipped
.collect {
case (Student, _, PoorStudent(_)) => "P"
case (Unemployed, _, PoorUnemployed(_)) => "P"
case (Other, IncomeScoreRange(_, _)) => "P"
}
.headOption
.getOrElse("F")
}
}