在ScalaCheck中生成任意线性函数?
我试图在ScalaCheck中生成形式为f(x)=ax+b的任意函数,其中a和b是任意整数。我该怎么做 我试过:在ScalaCheck中生成任意线性函数?,scala,scalacheck,Scala,Scalacheck,我试图在ScalaCheck中生成形式为f(x)=ax+b的任意函数,其中a和b是任意整数。我该怎么做 我试过: def arbitraryFunction[Int] = Arbitrary { for ( a <- Arbitrary.arbInt.arbitrary; b <- Arbitrary.arbInt.arbitrary ) yield (new Function1[Int, Int] { def apply(x : Int): Int = a
def arbitraryFunction[Int] = Arbitrary {
for (
a <- Arbitrary.arbInt.arbitrary;
b <- Arbitrary.arbInt.arbitrary
) yield (new Function1[Int, Int] { def apply(x : Int): Int = a * x + b })
}
def arbitraryFunction[Int]=任意{
为了(
A.
这定义了一个名为Int
的类型参数的函数。据我所知,您的函数不需要任何类型参数。编译器错误是由此类型标识符引起的,它会掩盖“正常的”Int
标识符。只要删除它,代码就会工作,除非它包含其他缺陷
这定义了一个名为Int
的类型参数的函数。据我所知,您的函数不需要任何类型参数。编译器错误是由此类型标识符引起的,它会掩盖“正常的”Int
标识符。只要删除它,代码就会工作,除非它包含其他缺陷
overloaded method value * with alternatives:
[error] (x: Double)Double <and>
[error] (x: Float)Float <and>
[error] (x: Long)Long <and>
[error] (x: scala.Int)scala.Int <and>
[error] (x: Char)scala.Int <and>
[error] (x: Short)scala.Int <and>
[error] (x: Byte)scala.Int
[error] cannot be applied to (Int(in method arbitraryFunction))
def arbitraryFunction[Int]