Scala学习笔记

spray
Akka
Effective Scala
Scala school
Scala tutorial for Java programmers
stackoverflow
Scala By Example

http://learnxinyminutes.com/docs/scala/

String

// 字符串中内嵌表达式,这样的字符串要以s开头
val n = 10
val a = Array(10, 20, 30)
s"hello world $n"
>> res4: String = hello world 10
s"hello world ${a(1)}"
>> res4: String = hello world 20
s"hello world ${a(2) - a(0)}"
>> res4: String = hello world 20
s"hello world ${Math.pow(n, 2)}"
>> res6: String = hello world 100.0

// 字符串格式化,这样的字符串以f开头,且格式化字符串必须紧跟在${}后面,不能有空格
f"Square root of 10: ${Math.sqrt(10)}%1.4f"
>> String = Square root of 10: 3.1623
// 此外还有raw前缀的字符串,表示忽略字符串内的特殊字符
raw"\r\t\n"
>> String = \r\t\n
// 还有类似python中的三个引号表示多行的字符串
val html = """
<>
<>
"""

函数

// 递归函数需要显示声明返回类型,编译器无法进行推导

函数跟方法的不同

定义两个函数

def f1(x: Int) = x + 1
val f2 = (x: Int) => x + 1

其中,def f1(x: Int) = x + 1整体是一个函数,而单拿出来f1只是方法。方法加上参数构成函数。函数也是对象,也有方法,比如

scala> f1 // 只输入f1的话会报错,因为它是方法
<console>:9: error: missing arguments for method f1;
scala> f1 _ // f1加上一个参数占位符就成了函数,它可以当作参数传递,也可以赋值给变量
res31: Int => Int = <function1>
scala> val y = f1 _ // 比如另y=f1(x)
y: Int => Int = <function1>
scala> f2 // 而f2本身就是一个函数
res32: Int => Int = <function1>
// 他们在使用上都是相同的
scala> f1(1) 
res33: Int = 2
scala> y(1)
res34: Int = 2
scala> f2(1)
res35: Int = 2
// 函数也是对象,函数的调用是对象的方法的调用,f2(1)是语法糖
scala> f2.apply(1)
res36: Int = 2
// 而方法的调用就不是对象
scala> f1.apply(1)
<console>:9: error: missing arguments for method f1;
// 也可以很容易的把一个method转化成函数
scala> val y2 = f1 _
y2: Int => Int = <function1>

按照数学上来理解,数学上可以定义f(x)=x+1f(x)就是函数,但是f是什么?
我们可以令y=f(x),但是不能写成y=f,我们可以写成f(g(x)) = g(x) + 1,但是不能写成f(g),除非g是一个值。
函数是一个对象,它有很多方法

class test {
    def m1(x:Int) = x+3
    val f1 = (x:Int) => x+3
}
// 编译以上代码生成两个class文件:test.class and test$$anonfun$1.class.
Compiled from "test.scala"
public class test extends java.lang.Object implements scala.ScalaObject{
   
    public test();
    public scala.Function1 f1();
    public int m1(int);
    public int $tag()       throws java.rmi.RemoteException;
}

Compiled from "test.scala"
public final class test$$anonfun$1 extends java.lang.Object implements scala.Function1,scala.ScalaObject{
   
    public test$$anonfun$1(test);
    public final java.lang.Object apply(java.lang.Object);
    public final int apply(int);
    public int $tag()       throws java.rmi.RemoteException;
    public scala.Function1 andThen(scala.Function1);
    public scala.Function1 compose(scala.Function1);
    public java.lang.String toString();
}

既然函数也是对象,函数调用其实是封装的apply的语法糖,那么是不是随便定义一个类,只要实现了apply方法,就能像调用函数一样调用对象呢?答案是肯定的。

object play{
   
    def apply(x: Int) = 1
}
play(1)
> res0: Int = 2

Functional Combinators

map

对列表中的每个元素应用一个函数,返回应用后的元素所组成的列表。

reduce, reduceLeft, reduceRight, fold, foldLeft, foldRight, scan, scanLeft, scanRight
// reduce 默认调用的reduceLeft,其签名为
def reduce[A1 >: A](op: (A1, A1) => A1): A1 = reduceLeft(op)
// reduceLeft默认调用的foldLeft,其签名为
def reduceLeft[B >: A](f: (B, A) => B): B =
    if (isE
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值