
Scala
文章平均质量分 78
axxbc123
这个作者很懒,什么都没留下…
展开
-
【Scala一】Scala各种符号的含义
::::::::运算符:::(三个冒号)表示List的连接操作,比如: val a = List(1, 2)val b = List(3, 4)val c = a ::: b 其中a,b保持不变,a和b连接产生一个新表List(1,2,3,4),而不是在a上面做add操作。Scala中的List不同于Java的List,Java声...原创 2014-12-29 21:57:29 · 1473 阅读 · 0 评论 -
【Scala十八】视图界定与上下文界定
Context Bound,上下文界定,是Scala为隐式参数引入的一种语法糖,使得隐式转换的编码更加简洁。 隐式参数首先引入一个泛型函数max,用于取a和b的最大值 def max[T](a: T, b: T) = { if (a > b) a else b } 因为T是未知类型,只有运行时才会代入真正的类型,因此调用a > b是不正确的,因...原创 2015-06-13 11:48:56 · 387 阅读 · 0 评论 -
【Scala十七】Scala核心十一:下划线_的用法
下划线_在Scala中广泛应用,_的基本含义是作为占位符使用。_在使用时是出问题非常多的地方,本文将不断完善_的使用场景以及所表达的含义 1. 在高阶函数中使用scala> val list = List(-3,8,7,9)list: List[Int] = List(-3, 8, 7, 9)scala> list.filter(_ > 7)res...原创 2015-06-07 13:36:44 · 610 阅读 · 0 评论 -
【Scala十六】Scala核心十:柯里化函数
本篇文章重点说明什么是函数柯里化,这个语法现象的背后动机是什么,有什么样的应用场景,以及与部分应用函数(Partial Applied Function)之间的联系 1. 什么是柯里化函数A way to write functions with multiple parameter lists. For instancedef f(x: Int)(y: Int) is a c...原创 2015-06-07 12:01:31 · 849 阅读 · 0 评论 -
【Scala十五】Scala核心九:隐式转换之二
隐式转换存在的必要性, 在Java Swing中,按钮点击事件的处理,转换为Scala的的写法如下: val button = new JButtonbutton.addActionListener( new ActionListener { def actionPerformed(event: ActionEvent) { ...原创 2015-02-27 17:14:49 · 98 阅读 · 0 评论 -
【Scala十四】Scala核心八:闭包
Free variable A free variable of an expression is a variable that’s used inside the expression but not defined inside the expression. For instance, in the function literal expression (x: Int) => ...原创 2015-02-27 15:49:26 · 131 阅读 · 0 评论 -
【Scala十三】Scala核心七:部分应用函数
何为部分应用函数?Partially applied function: A function that’s used in an expression and that misses some of its arguments.For instance, if function f has type Int => Int => Int, then f and f(1) are...原创 2015-02-27 15:35:59 · 132 阅读 · 0 评论 -
【Scala十二】Scala核心六:Trait
Traits are a fundamental unit of code reuse in Scala. A trait encapsulates method and field definitions, which can then be reused by mixing them into classes. Unlike class inheritance, in which each...原创 2015-02-27 12:51:06 · 128 阅读 · 0 评论 -
【Scala十一】Scala核心五:case模式匹配
package spark.examples.scala.grammars.caseclassesobject CaseClass_Test00 { def simpleMatch(arg: Any) = arg match { case v: Int => "This is an Int" case v: (Int, String) => (...原创 2015-02-27 11:51:10 · 271 阅读 · 0 评论 -
【Scala十】Scala核心四:集合框架之List
Spark的RDD作为一个分布式不可变的数据集合,它提供的转换操作,很多是借鉴于Scala的集合框架提供的一些函数,因此,有必要对Scala的集合进行详细的了解 1. 泛型集合都是协变的,对于List而言,如果B是A的子类,那么List[B]也是List[A]的子类,即可以把List[B]的实例赋值给List[A]变量 2. 给变量赋值(注意val关键字,a,b,c是三个关键字...原创 2015-02-25 15:43:33 · 172 阅读 · 0 评论 -
【Scala九】Scala核心三:泛型
泛型类package spark.examples.scala.genericsclass GenericClass[K, V](val k: K, val v: V) { def print() { println(k + "," + v) }}object GenericClass { def main(args: Array[String...原创 2015-02-25 10:42:01 · 157 阅读 · 0 评论 -
【Scala八】Scala核心二:隐式转换
Implicits work like this: if you call a method on a Scala object, and the Scala compiler does not see a definition for that method in the class definition for that object, the compiler will try to c...原创 2015-02-24 15:26:00 · 168 阅读 · 0 评论 -
【Scala七】Scala核心一:函数
1. 如果函数体只有一行代码,则可以不用写{},比如def print(x: Int) = println(x)一行上的多条语句用分号隔开,则只有第一句属于方法体,例如 def printWithValue(x: Int) : String= println(x); "ABC" 上面的代码报错,因为,printWithValue的方法体只有一句代码println(x),...原创 2015-02-24 14:51:12 · 225 阅读 · 0 评论 -
【Scala六】分析Spark源代码总结的Scala语法四
1. apply语法 FileShuffleBlockManager中定义的类ShuffleFileGroup,定义: private class ShuffleFileGroup(val shuffleId: Int, val fileId: Int, val files: Array[File]) { ... def apply(bucketId: Int) =...原创 2015-02-19 18:24:23 · 144 阅读 · 0 评论 -
【Scala五】分析Spark源代码总结的Scala语法三
1. If语句作为表达式 val properties = if (jobIdToActiveJob.contains(jobId)) { jobIdToActiveJob(stage.jobId).properties } else { // this stage will be assigned to "default" pool ...原创 2015-01-24 10:44:23 · 148 阅读 · 0 评论 -
【Scala四】分析Spark源代码总结的Scala语法二
1. Some操作 在下面的代码中,使用了Some操作:if (self.partitioner == Some(partitioner)),那么Some(partitioner)表示什么含义?首先partitioner是方法combineByKey传入的变量,Some的文档说明: /** Class `Some[A]` represents existing values...原创 2015-01-22 02:34:12 · 281 阅读 · 0 评论 -
【Scala三】分析Spark源代码总结的Scala语法一
Scala语法1. classOf运算符Scala中的classOf[T]是一个class对象,等价于Java的T.class,比如classOf[TextInputFormat]等价于TextInputFormat.class 2. 方法默认值defaultMinPartitions就是一个默认值,类似C++的方法默认值 def textFile(pa...原创 2015-01-21 22:05:02 · 321 阅读 · 0 评论 -
【Scala二】Scala集合
TBD 本文翻译自http://twitter.github.io/scala_school/collections.html原创 2015-01-11 17:17:16 · 137 阅读 · 0 评论 -
【Scala十九】关于function入参类型逆变,返回结果协变
class CSuper {}class C extends CSuper {}class C2 extends CSuper {}class CSub extends C {}object Scala8 { /*** * * @return */ val func1 : ...原创 2016-08-20 16:49:16 · 441 阅读 · 0 评论