john's tech blog

hope is coming


  • 首页

  • 标签

  • 归档

scala_collection

发表于 2016-11-06 | 更新于 2019-05-07

映射

映射(Map)是一种可迭代的键值对结构(也称映射或关联)。Scala的Predef类提供了隐式转换,允许使用另一种语法:key -> value,来代替(key, value)。如:Map(“x” -> 24, “y” -> 25, “z” -> 26)等同于Map((“x”, 24), (“y”, 25), (“z”, 26)),却更易于阅读。

collections

colon-equals

It’s very likely you := in a SBT build file. See github.com/harrah/xsbt/wiki/Settings for all of SBT’s assignmenty operators.

IMHO : In My Humble Opinion 恕我直言
What is the difference between = and := in Scala?

scala_def定义函数

发表于 2016-11-03 | 更新于 2019-05-07

scala def 定义函数的区别

1
def foo = {1}

A function which should return a non-Unit value must be declared with the = notation (although of course the compiler can infer the return-type from the expression’s type).

1
def foo {1}

返回类型为Unit
In Scala if a method declaration does not have an equal sign before its body, the compiler infers that the result type will be Unit

参考
scala: ‘def foo = {1}’ vs ‘def foo {1}’

scala_apply函数

发表于 2016-11-02 | 更新于 2019-05-07

scala apply方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class ApplyOperation{

}

class ApplyTest{
def apply() = println("I am into spark so much!")
def haveTry: Unit ={
println("have a try on apply")
}
}

object ApplyTest{
def apply() ={
println("I am into Scala so much")

new ApplyTest
}
}

object ApplyOperation{
def main(args : Array[String]){
val array = Array(1,2,3,4)

val a = ApplyTest()// 调用object 的apply

a.haveTry

a() // class apply use
}
}

输出
I am into Scala so much
have a try on apply
I am into Spark so much!!!

参考
scala apply方法 笔记
动手实战Scala中的apply方法和单例对象

scala case class apply

Scala会给case类自动添加一个单例对象

参考探索Scala(4)– Case Classes

scala_with关键字

发表于 2016-10-29 | 更新于 2019-05-07

with 关键字

with关键字可以用来实现包装器的功能,新建withSample.scala文件示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
trait Friendly {
def greet() = "Hi"
}

class Dog extends Friendly {
override def greet() = "Woof"
}

class HungryDog extends Dog {
override def greet() = "I'd like to eat my own dog food"
}

trait ExclamatoryGreeter extends Friendly {
override def greet() = super.greet() + "!"
}

var pet: Friendly = new Dog
println(pet.greet())

pet = new HungryDog
println(pet.greet())

pet = new Dog with ExclamatoryGreeter
println(pet.greet())

pet = new HungryDog with ExclamatoryGreeter
println(pet.greet())

用scala命令调用(scala withSample.scala),输出如下

1
2
3
4
woof
I'd like to eat my own dog food
woof!
I'd like to eat my own dog food!

张逸的Scala中的Partial Function一文中提到PartialFunction的定义如下:

1
2
3
4
5
6
trait PartialFunction[-A, +B] extends (A => B) { self =>
import PartialFunction._
def isDefinedAt(x: A): Boolean
def applyOrElse[A1 <: A, B1 >: B](x: A1, default: A1 => B1): B1 =
if (isDefinedAt(x)) apply(x) else default(x)
}

追本溯源,是因为这里对偏函数值的调用,实则是调用了AbstractPartialFunction的apply()方法(case语句相当于是继承AbstractPartialFunction的子类):

1
2
3
	abstract class AbstractPartialFunction[@specialized(scala.Int, scala.Long, scala.Float, scala.Double, scala.AnyRef) -T1, @specialized(scala.Unit, scala.Boolean, scala.Int, scala.Float, scala.Long, scala.Double, scala.AnyRef) +R] extends Function1[T1, R] with PartialFunction[T1, R] { self =>
def apply(x: T1): R = applyOrElse(x, PartialFunction.empty)
}

看到里面用到了with PartialFunction[T1,R]。

Scala 学习笔记(一)

scala之空格调用函数

发表于 2016-10-28 | 更新于 2019-05-07

java和scala调用函数的区别

1
object.method();

在scala中,这样调用,don’t need the semi-colon:

1
object.method()

甚至都不用括号(and then you can omit the parentheses):

1
object.method

如果方法只有一个参数,

1
object.method(param)

你可以把点换成空格,而且可以把括号去掉(you can change the Scala syntax to use a space instead of a dot, while also dropping the parentheses):

1
object method param

Scala methods that take a single parameter can be invoked without dots or parentheses.

Scala methods: dots, spaces, and parentheses

1…343536…47

John

232 日志
43 标签
GitHub Twitter
欢迎关注我的公众号:沉迷Spring
© 2023 johnwonder
由 Hexo 强力驱动 v3.2.0
|
主题 – NexT.Pisces v7.1.1
23035 | 24304
0%