日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Scala 中下划线的用法

發(fā)布時(shí)間:2025/3/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Scala 中下划线的用法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

?

1、存在性類(lèi)型:Existential types def foo(l: List[Option[_]]) = ...2、高階類(lèi)型參數(shù):Higher kinded type parameterscase class A[K[_],T](a: K[T])3、臨時(shí)變量:Ignored variables val _ = 54、臨時(shí)參數(shù):Ignored parameters List(1, 2, 3) foreach { _ => println("Hi") }5、通配模式: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 _ => } val (a, _) = (1, 2) for (_ <- 1 to 10)6、通配導(dǎo)入:Wildcard imports import java.util._7、隱藏導(dǎo)入:Hiding imports // Imports all the members of the object Fun but renames Foo to Barimport com.test.Fun.{ Foo => Bar , _ }// Imports all the members except Foo. To exclude a member rename it to _import com.test.Fun.{ Foo => _ , _ }8、連接字母和標(biāo)點(diǎn)符號(hào):Joining letters to punctuation def bang_!(x: Int) = 59、占位符語(yǔ)法:Placeholder syntax List(1, 2, 3) map (_ + 2) _ + _ ( (_: 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)10、偏應(yīng)用函數(shù):Partially applied functions 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())11、初始化默認(rèn)值:default value var i: Int = _12、作為參數(shù)名: //訪(fǎng)問(wèn)mapvar 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)//訪(fǎng)問(wèn)元組:tuple getters (1,2)._213、參數(shù)序列:parameters Sequence _*作為一個(gè)整體,告訴編譯器你希望將某個(gè)參數(shù)當(dāng)作參數(shù)序列處理。例如val s = sum(1 to 5:_*)就是將1 to 5當(dāng)作參數(shù)序列處理。 //Range轉(zhuǎn)換為L(zhǎng)istList(1 to 5:_*)//Range轉(zhuǎn)換為VectorVector(1 to 5: _*)//可變參數(shù)中def capitalizeAll(args: String*) = { args.map { arg => arg.capitalize } }val arr = Array("what's", "up", "doc?") capitalizeAll(arr: _*)

這里需要注意的是,以下兩種寫(xiě)法實(shí)現(xiàn)的是完全不一樣的功能:

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 } In the first case, process _ represents a method; Scala takes the polymorphic method and attempts to make it monomorphic by filling in the type parameter, but realizes that there is no type that can be filled in for A that will give the type (_ => Unit) => ? (Existential _ is not a type). In the second case, process(_) is a lambda; when writing a lambda with no explicit argument type, Scala infers the type from the argument that foreach expects, and _ => Unit is a type (whereas just plain _ isn't), so it can be substituted and inferred. This may well be the trickiest gotcha in Scala I have ever encountered.

?

轉(zhuǎn)載于:https://www.cnblogs.com/wzj4858/p/8204369.html

總結(jié)

以上是生活随笔為你收集整理的Scala 中下划线的用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。