问题描述
我是F#的新手,看不到如何从以下方法提取int值:
I'm new on F#, and can't see how extract the int value from:
let autoInc = FsCheck.Gen.choose(1,999)
编译器说类型是Gen<int>
,但是无法从中获取int!.我需要将其转换为十进制,并且两种类型都不兼容.
The compiler say the type is Gen<int>
, but can't get the int from it!. I need to convert it to decimal, and both types are not compatible.
推荐答案
从消费者的角度来看,您可以使用Gen.sample
组合器,该组合器在给定生成器(例如Gen.choose
)的情况下,为您提供了一些示例值
From a consumer's point of view, you can use the Gen.sample
combinator which, given a generator (e.g. Gen.choose
), gives you back some example values.
Gen.sample
的签名是:
val sample : size:int -> n:int -> gn:Gen<'a> -> 'a list
(* `size` is the size of generated test data
`n` is the number of samples to be returned
`gn` is the generator (e.g. `Gen.choose` in this case) *)
您可以忽略size
,因为Gen.choose
会忽略它,因为它的分布是均匀的,并且可以执行以下操作:
You can ignore size
because Gen.choose
ignores it, as its distribution is uniform, and do something like:
let result = Gen.choose(1,999) |> Gen.sample 0 1 |> Seq.exactlyOne |> decimal
(* 0 is the `size` (gets ignored by Gen.choose)
1 is the number of samples to be returned *)
result
应该是封闭时间间隔 [1,999] 中的一个值,例如 897 .
The result
should be a value in the closed interval [1, 999], e.g. 897.
这篇关于如何从FsCheck.Gen.choose中提取int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!