_下划线的使用

  1. 存在性类型:Existential types

    val site = List(Option("Runoob"),Option(12),None)
    //Option[_]表示Option集合里可以是各种类型,应为上面是字符串,数字类型,也可以用Option[Any]来替换
    def test(l:List[Option[_]]):Unit={
    for(v<-l)
        {
            print(v.getOrElse("默认值"))
        }
     }
    test(site)
    
  2. 临时变量:Ignored variables

     val _ = 5
    
  3. 临时参数:Ignored parameters

     List(1, 2, 3). foreach { _ => println("Hi") }
    
  4. 通配模式:Wildcard patterns

     Some(5) match { case Some(_) => println("Yes") }
     match {
      case List(1,_,_) => " a list with three element and the       first element is 1"
      case List(_*)  => " a list with zero or more elements "
      case Map[_,_] => " matches a map with any key type and any value type "
      case _ =>
    }
    
    //模式匹配       
     abstract class Item
     case class Product(description: String, price: Double)  extends Item
     case class Bundle(description: String, discount: Double, items: Item*) extends Item
    
     def price(it: Item): Double = it match {
     case Product(_, p) => p
     //这里注释下map(price _) 注意什么都没有,_表示的是Product对象,取他的属性price,不知道为什么要这么写~~
     case Bundle(_, disc, its @ _*) => its.map(price _).sum * (100-disc) /100
     //这里@表示将嵌套的值绑定到变量its
     }
     //测试
     val bun2 = Bundle("Appliance sale",10.0,Product("Haier Refrigerato", 3000.0),Product("Geli air conditionor",2000.0))
      println(price(bun2))
    
  5. 通配导入:Wildcard imports

         import java.util._
    
  6. 隐藏导入:Hiding imports

       // Imports all the members of the object Fun but renames Foo to Bar
     import com.test.Fun.{ Foo => Bar , _ }
    
       // Imports all the members except Foo. To exclude a member rename it to _
     import com.test.Fun.{ Foo => _ , _ }
    
  7. 连接字母和标点符号:Joining letters to punctuation

       def bang_!(x: Int) = 5
    
  8. 偏应用函数:Partially applied functions

    def fun = {
    // Some code
    }
    val funLike = fun _
    
    List(1, 2, 3) foreach println _
    
    1 to 5 map (10 * _)
    
    foo _               // Eta expansion of method into method value
    
    foo(_)              // Partial function application
    Example showing why foo(_) and foo _ are different:
    
     trait PlaceholderExample {
    def process[A](f: A => Unit)
    val set: Set[_ => Unit]
        set.foreach(process _) // Error 
        set.foreach(process(_)) // No Error
    }
    
  9. 初始化默认值:default value

    var i: Int = _
    
  10. 作为参数名:
    //访问map
    var m3 = Map((1,100), (2,200))
    for(e<-m3) println(e._1 + ": " + e._2)
    m3 filter (e=>e.1>1)
    m3 filterKeys (
    >1)
    m3.map(e=>(e._1*10, e._2))
    m3 map (e=>e._2)

//访问元组:tuple getters
(1,2)._2

  1. 参数序列:parameters Sequence
    *作为一个整体,告诉编译器你希望将某个参数当作参数序列处理。例如val s = sum(1 to 5:)就是将1 to 5当作参数序列处理。
    //Range转换为List
    List(1 to 5:_
    )

//Range转换为Vector
Vector(1 to 5: _*)

//可变参数中
def capitalizeAll(args: String*) = {
args.map { arg =>
arg.capitalize
}
}

val arr = Array("what's", "up", "doc?")
capitalizeAll(arr: _*)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 下划线这个符号几乎贯穿了任何一本Scala编程书籍,并且在不同的场景下具有不同的含义,绕晕了不少初学者。正因如此,...
    Helen_Cat阅读 6,669评论 0 3
  • Scala的集合类可以从三个维度进行切分: 可变与不可变集合(Immutable and mutable coll...
    时待吾阅读 11,111评论 0 4
  • Scala与Java的关系 Scala与Java的关系是非常紧密的!! 因为Scala是基于Java虚拟机,也就是...
    灯火gg阅读 8,861评论 1 24
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,302评论 19 139
  • 1. 已经付了钱的大叔将报纸一囫囵卷起,前后瞧瞧自行车,并没有存放报纸的合适位置。赶巧又碰上这朦胧天气,滴答着几滴...
    臭小妈妈阅读 2,481评论 0 0

友情链接更多精彩内容