Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala函数,用于计算包含算术序列的流_Scala - Fatal编程技术网

Scala函数,用于计算包含算术序列的流

Scala函数,用于计算包含算术序列的流,scala,Scala,测试 test("arithmetic") { assert(Calculation.arithmetic(5, 10) === 30) } Main def arithmetic(a: Int, b: Int) : Int = { val c = b - a val third = b + c val fourth = third + c val fifth = fourth + c fifth + c } 在Scala中,可以使用val fibs=fibFrom(

测试

test("arithmetic") {
  assert(Calculation.arithmetic(5, 10) === 30)
}
Main

def arithmetic(a: Int, b: Int) : Int = {
  val c = b - a
  val third = b + c
  val fourth = third + c
  val fifth = fourth + c
  fifth + c
}

在Scala中,可以使用
val fibs=fibFrom(1,1)创建。是否可以使用在Scala中创建
算术
序列的流?如果为正,如何实现它?

斐波那契序列是一个算术序列,您可以创建任何您想要的序列:

scala> val s: Stream[Int] = 0 #:: s.map(_+5)
s: Stream[Int] = Stream(0, ?)

scala> s(10)
res0: Int = 50

scala> (s take 10).toList
res1: List[Int] = List(0, 5, 10, 15, 20, 25, 30, 35, 40, 45)

但fibs流已经是一个算术序列了。因此,我看不出您要求的是什么。我正在寻找生成例如
5、10、15、20、25和30的流,而不是
1,1,2,3,5,8
。我认为第一个是一个
算术
,而后者是一个
斐波那契
序列。