Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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和dropWhile_Scala - Fatal编程技术网

Scala和dropWhile

Scala和dropWhile,scala,Scala,我是HS的大四学生,也是函数式编程和Scala的新手。我在Scala REPL中尝试了一些构造,需要一些关于返回响应的指导 //Defined a tuple scala> val x =(2.0, 3.0, 1) x: (Double, Double, Int) = (2.0,3.0,1) //This made sense to me. Result is a list of values that are of type Ints scala> x.productIter

我是HS的大四学生,也是函数式编程和Scala的新手。我在Scala REPL中尝试了一些构造,需要一些关于返回响应的指导

//Defined a tuple

scala> val x =(2.0, 3.0, 1)
x: (Double, Double, Int) = (2.0,3.0,1)

//This made sense to me.  Result is a list of values that are of type Ints
scala> x.productIterator.dropWhile(_.isInstanceOf[Double]).toList
res1: List[Any] = List(1)

**//This DID NOT make sense to me.  Why are Double values included?**
scala> x.productIterator.dropWhile(_.isInstanceOf[Int]).toList
res0: List[Any] = List(2.0, 3.0, 1)


//filter operator seems to work
scala> x.productIterator.toList.filter(x => x.isInstanceOf[Double])
res7: List[Any] = List(2.0, 3.0)
只要与提供的谓词匹配,就会删除任何值,并返回迭代器的其余部分:

跳过此迭代器中满足以下条件的最长元素序列 并返回剩余元素的迭代器

您传递的所提供的谓词对于第一个元素(类型为
Double
)失败,因此它是您具体化为
列表[a]
的整个迭代器

例如,如果您选择在
isInstanceOf[Double]
时删除,您将收到一个包含单个元素的列表
1

scala> x.productIterator.dropWhile(_.isInstanceOf[Double]).toList
res13: List[Any] = List(1)