使用scala模式匹配代替java switch case的好处是什么? [英] What is the advantage of using scala pattern matching instead of java switch case?

查看:555
本文介绍了使用scala模式匹配代替java switch case的好处是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家都说模式匹配是函数式语言的一大特色。为什么?



我不能简单地使用ifs和切换所有内容吗?



我想理解使用模式匹配而不是常规过程编程的优点ifs和switch case

解决方案

我首先想说明你不要使用模式匹配替代switch语句。 Scala没有 switch 语句,它具有的是匹配块,表面上看起来与switch语句非常相似。



使用模式匹配匹配块可以完成 switch 所做的所有操作,等等。



A)它不仅限于Oracle选择在语言规范(字符串和枚举)中祝福的基本类型和其他类型。如果你想在你自己的类型上匹配,就直接前进!



B)模式匹配也可以提取。例如,用一个元组:

  val tup =(hello world,42)
tup match {
case(s,i)=>
println(字符串是+ s)
println(数字是+ i

列表:

  val xs = List(1,2,3, 4,5,6)
xs match {
case h :: t =>
// h是头部:1
// t是尾部:2, 3,4,5,6
// ::上面也是一个匹配INFIX TYPE
}

使用case类

  case class Person(name:String,age:Int )
val p = Person(John Doe,42)
p match {
case Person(name,42)=>
//这里只提取名字,如果年龄不是42
println(name)
}



<

 <$> p>  C)模式匹配可用于值赋值和for-comprehensions 。对于((a,b)),c $ c> val tup =(19,73)

val(a,b)= tup

- )产生a + b //一些(92)

D)匹配块是表达式,而不是表达式



这意味着它们评估而不是通过副作用完全行事。这对函数式编程至关重要!

  val result = tup match {case(a,b)=> a + b} 


Everybody says that pattern matching is a great feature in functional languages. Why?

Can't I simple use ifs and switch cases for everything?

I'd like to understand the advantages of using pattern matching instead of regular procedural programming ifs and switch cases

解决方案

I'd first like to note that you don't use pattern matching "instead" of switch statements. Scala doesn't have switch statements, what it does have is match blocks, with cases inside that superficially look very similar to a switch statement.

Match blocks with pattern matching does everything that switch does, and much more.

A) It's not restricted to just primitives and other types that Oracle have chosen to "bless" in the language spec (Strings and Enums). If you want to match on your own types, go right ahead!

B) Pattern matching can also extract. For example, with a tuple:

val tup = ("hello world", 42)
tup match {
  case (s,i) =>
    println("the string was " + s)
    println("the number was " + i
}

With a list:

val xs = List(1,2,3,4,5,6)
xs match {
  case h :: t =>
    // h is the head: 1
    // t is the tail: 2,3,4,5,6
    // The :: above is also an example of matching with an INFIX TYPE
}

With a case class

case class Person(name: String, age: Int)
val p = Person("John Doe", 42)
p match {
  case Person(name, 42) =>
    //only extracting the name here, the match would fail if the age wasn't 42
    println(name)
}

C) pattern matching can be used in value assignment and for-comprehensions, not just in match blocks:

val tup = (19,73)

val (a,b) = tup

for((a,b) <- Some(tup)) yield a+b // Some(92)

D) match blocks are expressions, not statements

This means that they evaluate to the body of whichever case was matched, instead of acting entirely through side-effects. This is crucial for functional programming!

val result = tup match { case (a,b) => a + b }

这篇关于使用scala模式匹配代替java switch case的好处是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆