zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Scala中下划线的用途

scala 用途 下划线
2023-09-14 08:56:51 时间

临时参数:

List(1, 2, 3) foreach { _ = println("Hi") } //List(1, 2, 3) foreach { t = println("Hi") }

通配模式:

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 _ = 

val (a, _) = (1, 2)

for (_ - 1 to 10)

通配导入:

// Imports all the classes in the package matching

import scala.util.matching._

// Imports all the members of the object Fun (static import in Java).

import com.test.Fun._

隐藏导入:

// 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 = _ , _ }

连接字母和标点符号:

def bang_!(x: Int) = 5

占位符:

( (_: Int) + (_: Int) )(2,3)

val nums = List(1,2,3,4,5,6,7,8,9,10)

nums map (_ + 2)

nums sortWith(_ _)

nums filter (_ % 2 == 0)

nums reduceLeft(_+_)

nums reduce (_ + _)

nums reduceLeft(_ max _)

nums.exists(_ 5)

nums.takeWhile(_ 8)

偏函数:

def fun = {

 // Some code

val funLike = fun _

List(1, 2, 3) foreach println _

1 to 5 map (10 * _)

//List("foo", "bar", "baz").map(_.toUpperCase())

List("foo", "bar", "baz").map(n = n.toUpperCase())

初始化默认值:

var d:Double = _ 

var i:Int = _

参数序列:

//Range转换为List

List(1 to 5:_*)

//Range转换为Vector

Vector(1 to 5: _*)

//可变参数中

def capitalizeAll(args: String*) = {

 args.map { arg = 

 arg.capitalize

val arr = Array("whats", "up", "doc?")

capitalizeAll(arr: _*)

作为参数名:

//访问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)

(1,2)._2

原文地址:https://my.oschina.net/joymufeng/blog/863823   作者:joymufeng 下划线这个符号几乎贯穿了任何一本Scala编程书籍,并且在不同的场景下具有不同的含义,绕晕了不少初学者。
雨客 微博@JavaChen,这里的所有博客文章来自http://blog.javachen.com/。