john's tech blog

hope is coming


  • 首页

  • 标签

  • 归档

scala_partialFunction

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

偏函数

语法

1
2
3
val pf: PartialFunction[Int, String] = {
case i if i%2 == 0 => "even"
}

也可以由orElse组成:

1
2
3
4
val tf: (Int => String) = pf orElse { case _ => "odd"}

tf(1) == "odd"
tf(2) == "even"

1
2
3
4
5
6
7
8
9
10
11
trait Publisher[T] {
def subscribe(f: PartialFunction[T, Unit])
}

val publisher: Publisher[Int] = ..
//定义subscribe函数
publisher.subscribe {
case i if isPrime(i) => println("found prime", i)
case i if i%2 == 0 => count += 2

}

偏应用函数

偏应用函数(Partial Applied Function)是缺少部分参数的函数,是一个逻辑上概念

语法和示例

Scala里,当你调用函数,传入任何需要的参数,你就是在把函数应用到参数上。如,给定下列函数:

1
2
scala> def sum(a: Int, b: Int, c: Int) = a + b + c
sum: (Int,Int,Int)Int

偏应用函数是一种表达式,你不需要提供函数需要的所有参数。代之以仅提供部分,或不提供所需参数

1
2
scala> val a = sum _
a: (Int, Int, Int) => Int = < function>

Scala: 偏函数(PartialFunction) && 偏应用函数(Partial Applied Function

scala之caseClass

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

case class

1 构造器中的参数如果不被声明为var的话,它默认的话是val类型的,但一般不推荐将构造器中的参数声明为var

2 自动创建伴生对象,同时在里面给我们实现子apply方法,使得我们在使用的时候可以不直接显示地new对象

3 伴生对象中同样会帮我们实现unapply方法,从而可以将case class应用于模式匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//抽象类Person
abstract class Person

//case class Student
case class Student(name:String,age:Int,studentNo:Int) extends Person
//case class Teacher
case class Teacher(name:String,age:Int,teacherNo:Int) extends Person
//case class Nobody
case class Nobody(name:String) extends Person

object CaseClassDemo{
def main(args: Array[String]): Unit = {
//case class 会自动生成apply方法,从而省去new操作
val p:Person=Student("john",18,1024)
//match case
p match {
case Student(name,age,studentNo)=>println(name+":"+age+":"+studentNo)
case Teacher(name,age,teacherNo)=>println(name+":"+age+":"+teacherNo)
case Nobody(name)=>println(name)
}
}
}

sbt中的Attributed就是个case class

以下代码摘录自sbt/util仓库:

位置:internal/util-collection/src/main/scala/sbt/internal/util/Attributes.scala

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/** Associates a `metadata` map with `data`. */
/*使一个metadata 映射 和data产生关系
final case class Attributed[D](data: D)(val metadata: AttributeMap) {
/** Retrieves the associated value of `key` from the metadata. */

def get[T](key: AttributeKey[T]): Option[T] = metadata.get(key)

/** Defines a mapping `key -> value` in the metadata. */
def put[T](key: AttributeKey[T], value: T): Attributed[D] = Attributed(data)(metadata.put(key, value))

/** Transforms the data by applying `f`. */
def map[T](f: D => T): Attributed[T] = Attributed(f(data))(metadata)
}
object Attributed {
/** Extracts the underlying data from the sequence `in`. */
def data[T](in: Seq[Attributed[T]]): Seq[T] = in.map(_.data)

/** Associates empty metadata maps with each entry of `in`.*/
def blankSeq[T](in: Seq[T]): Seq[Attributed[T]] = in map blank

/** Associates an empty metadata map with `data`. */
def blank[T](data: T): Attributed[T] = Attributed(data)(AttributeMap.empty)
}

关于Attributed类参数有两个括号的问题参考:

scala函数编程的柯里化
Scala 函数柯里化(Currying)

Scala class和case class的区别
Scala入门到精通
Scala单例对象、伴生类以及伴生对象、apply介绍

千里码算法第一题

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

码之初

1.第2333个能被2或者被3整除的正整数是多少?

被2或者被3整除的正整数依次是:2,3,4,6,8,9,10,12,14,15,16,18…。

可以考虑第N个 N为偶数的情况,推理可以得到为n+ n/2,也就是2333+ 2332/2 +2 = 3500。

也可以考虑四个为一组的情况,比如2,3,4,6 一组;8,9,10,12为一组;14,15,16,18为一组。

那么就为2332/4 = 583 4 = 2332 + 2(583)=1166 第2333个 再 +2 = 3500

码之初

sbt-settings

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

sbt Settings

Remember, a build definition creates a list of Setting, which is then used to transform sbt’s description of the build (which is a map of key-value pairs). A Setting is a transformation with sbt’s earlier map as input and a new map as output. The new map becomes sbt’s new state.

build定义会创建setting列表,用于转变build的描述,是key-value pairs.
一个setting是之前映射的会输出新的映射。新的映射是sbt新的状态。

Different settings transform the map in different ways. Earlier, you read about the := method.

不同的settings通过不同的方式改变映射。比如:=。

The Setting which := creates puts a fixed, constant value in the new, transformed map. For example, if you transform a map with the setting name := “hello” the new map has the string “hello” stored under the key name.

:= 创建了新的常量,如果你设置了 name := “Hello” ,那么就存储了key 为name值为hello的map.
在build.sbt里面并非key := value, 而是key := expression. 文件里的每一行其实是一句scala语句

More-About-Settings

sbt从入门到半熟

sbt +=

+=,将值添加进现有值里,适用于集合类型的key,比如libraryDependencies
如果是version := "0.1",再用version += “0.2”,那么在交互模式里show version,会变成0.10.2

sbt.Attributed

sbt.Attributed[java.io.File]

Retrieves the associated value of key from the metadata.

顾名思义,是获取java.io.File类型的Attributed[D]

1
2
3
def put[T](key: AttributeKey[T], value: T): Attributed[D]

Defines a mapping key -> value in the metadata.

final case class AttributedD

sbt_command

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

sbt 命令解释

run command

run *Runs a main class, passing along arguments provided on the command line.

运行一个主类,如果在文件内部定义变量或者直接调用函数,那么会提示如下:

class or object definition```
1
2
3
4
5
6
7
8
9
10
11

因为会先编译这个文件。

文件内部必须包含main方法的类:

```scala
object HelloWorld{
def main(args: Array[String]) {
println(ChecksumAccumulator.calculate("We"))
}
}

package command

在sbt交互模式下输入package,会把当前目录下的project打包,然后运行run命令,如果当前能找到object对象并且有main函数,那么就会执行它。

exit command

输入exit命令退出sbt交互模式。

1…353637…47

John

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