Skip to content

Commit

Permalink
Avoid unnecessary list use in productGen.
Browse files Browse the repository at this point in the history
Also expose sequenceToArr and sequenceToSeq to F# users.
  • Loading branch information
kurtschelfthout committed Aug 28, 2017
1 parent 0e06dc1 commit fabde1a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/FsCheck/Gen.fs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ module Gen =
map (fun (_, y, _) -> y) g,
map (fun (_, _, z) -> z) g

///Sequence the given seq of generators into a generator of a list.
/// Sequence the given enumerable of generators into a generator of a list.
//[category: Creating generators from generators]
[<CompiledName("SequenceToList"); EditorBrowsable(EditorBrowsableState.Never)>]
let sequence l =
Expand All @@ -349,9 +349,9 @@ module Gen =
go gs' (y::acc) size r2
Gen(fun n r -> go (Seq.toList l) [] n r)

///Sequence the given list of generators into a generator of a list.
/// Sequence the given enumerable of generators into a generator of an enumerable.
//[category: Creating generators from generators]
[<CompiledName("Sequence"); CompilerMessage("This method is not intended for use from F#.", 10001, IsHidden=true, IsError=false)>]
[<CompiledName("Sequence")>]
let sequenceToSeq generators =
// This implementation is similar to that for arrays and lists but is specialized
// to sequences to avoid intermediate conversion to an intermediate list.
Expand All @@ -364,9 +364,9 @@ module Gen =
yield g size r1
}

///Sequence the given array of generators into a generator of a array.
/// Sequence the given array of generators into a generator of a array.
//[category: Creating generators from generators]
[<CompiledName("Sequence"); CompilerMessage("This method is not intended for use from F#.", 10001, IsHidden=true, IsError=false)>]
[<CompiledName("Sequence")>]
let sequenceToArr ([<ParamArrayAttribute>]generators:array<Gen<_>>) =
if Object.ReferenceEquals (null, generators) then
nullArg "generators"
Expand Down
8 changes: 4 additions & 4 deletions src/FsCheck/ReflectArbitrary.fs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ module internal ReflectArbitrary =
fields |> Seq.exists (fun field -> isRecursive field containingType newSeen))

let productGen (ts : seq<Type>) create =
let gs = [ for t in ts -> getGenerator t ]
let gs = [| for t in ts -> getGenerator t |]
let n = gs.Length
if n = 0 then
if n <= 0 then
Gen.constant (create [||])
else
sized (fun s -> resize ((s / n) - 1) (sequence gs))
|> map (List.toArray >> create)
sized (fun s -> resize (max 0 ((s / n) - 1)) (sequenceToArr gs))
|> map create

if isRecordType t then
let fields = getRecordFieldTypes t
Expand Down

0 comments on commit fabde1a

Please sign in to comment.