F# 如何从FsCheck.Gen.choose中提取int
我是F#的新手,不知道如何从中提取int值:F# 如何从FsCheck.Gen.choose中提取int,f#,fscheck,F#,Fscheck,我是F#的新手,不知道如何从中提取int值: let autoInc = FsCheck.Gen.choose(1,999) 编译器说类型是Gen,但无法从中获取int!。我需要将其转换为十进制,但这两种类型不兼容。从消费者的角度来看,您可以使用Gen.sample组合器,在给定生成器(例如Gen.choose)的情况下,该组合器会返回一些示例值 Gen.sample的签名为: val sample : size:int -> n:int -> gn:Gen<'a> -
let autoInc = FsCheck.Gen.choose(1,999)
编译器说类型是
Gen
,但无法从中获取int!。我需要将其转换为十进制,但这两种类型不兼容。从消费者的角度来看,您可以使用Gen.sample
组合器,在给定生成器(例如Gen.choose
)的情况下,该组合器会返回一些示例值
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) *)
结果
应该是封闭区间[1999]中的一个值,例如897。嗨,要添加到Nikos已经告诉您的内容中,您可以通过以下方式获得1到999之间的小数点:
#r "FsCheck.dll"
open FsCheck
let decimalBetween1and999 : Gen<decimal> =
Arb.generate |> Gen.suchThat (fun d -> d >= 1.0m && d <= 999.0m)
let sample () =
decimalBetween1and999
|> Gen.sample 0 1
|> List.head
你可能真正想做的是什么
使用它可以编写一些漂亮的类型,并检查如下属性(这里使用Xunit作为测试框架和FsCheck.Xunit包:
open FsCheck
open FsCheck.Xunit
type DecTo999 = DecTo999 of decimal
type Generators =
static member DecTo999 =
{ new Arbitrary<DecTo999>() with
override __.Generator =
Arb.generate
|> Gen.suchThat (fun d -> d >= 1.0m && d <= 999.0m)
|> Gen.map DecTo999
}
[<Arbitrary(typeof<Generators>)>]
module Tests =
type Marker = class end
[<Property>]
let ``example property`` (DecTo999 d) =
d > 1.0m
打开FsCheck
打开FsCheck.Xunit
DecTo999类型=十进制的DecTo999
类型生成器=
静态成员DecTo999=
{new arbitral()与
覆盖u u.Generator=
生成
|>此类发电站(发电站->发电站>=1.0m&&d发电站地图1999年12月
}
[]
模块测试=
类型标记=类结束
[]
让``example property``(DecTo999 d)=
直径>1.0m
gen您查看了中的功能了吗?是的。但我不理解这段代码。代码说‘让我们选择(l,h)=rand |>map(range(l,h)>>fst)’,但无法解析它的含义该表达式的结果不是一个数字,而是一个数字生成器。它不“包含”里面有任何特定的数字,所以没有什么可“提取”。如果您只是想生成一个随机数,请使用System.random
@mamcx:这是Gen.choose
的源代码,但我没有将您链接到源代码,原因是我将您链接到文档,您应该阅读该文档。或者导入操作符.decimal
,例如。|>Gen sample 0 1 |>SeqlyOne |>decimal
是的,有两个选项-选择您喜欢的任何选项
let decimalIntBetween1and999 : Gen<decimal> =
Gen.choose (1,999)
|> Gen.map decimal
let sampleInt () =
decimalIntBetween1and999
|> Gen.sample 0 1
|> List.head
open FsCheck
open FsCheck.Xunit
type DecTo999 = DecTo999 of decimal
type Generators =
static member DecTo999 =
{ new Arbitrary<DecTo999>() with
override __.Generator =
Arb.generate
|> Gen.suchThat (fun d -> d >= 1.0m && d <= 999.0m)
|> Gen.map DecTo999
}
[<Arbitrary(typeof<Generators>)>]
module Tests =
type Marker = class end
[<Property>]
let ``example property`` (DecTo999 d) =
d > 1.0m