Skip to content

Instantly share code, notes, and snippets.

@baronfel
Created July 6, 2020 23:07
Show Gist options
  • Save baronfel/c48ecc7246405526ae4bbb187cae866e to your computer and use it in GitHub Desktop.
Save baronfel/c48ecc7246405526ae4bbb187cae866e to your computer and use it in GitHub Desktop.
quick thing I made to demo generating arbitrary DU shapes for someone on the FSSF slack today
// You'll need to run this in a .net core sdk 3.1.300+ `dotnet fsi` instance, with the `--langversion:preview` flag set.
#r "nuget: FsCheck"
open FsCheck
(* type definitions *)
type Point = { x: int; y: int}
type Shape =
| Square of Point * Point * Point * Point
| Triangle of Point * Point * Point
| Circle of Point * int
/// this is an automatically-derived Gen<Shape> that FsCheck can use to make random
/// Shape instances. The parameters of the DU cases, and the distribution of the cases,
/// will be based on the random seeds used to evaluate the Gen
let randomShapes = Arb.generate<Shape>
/// Here is an example of how you could make random Square cases only.
/// The type of this value is Gen<Shape>, but it only contains Square instances
/// because that's all I put into it
let randomSquares =
Arb.generate<Point>
|> Gen.listOfLength 4
|> Gen.map (function [p1;p2;p3;p4] -> Square(p1, p2, p3, p4))
/// helper function to generate some instances from a Gen<'t>.
/// This is where you'd tweak the weights/distributions to get more realistic/
/// random data
let eval gen =
Gen.eval 10 (Random.mkStdGen 10L) gen
// gen some shapes and print them
let shapes =
Gen.nonEmptyListOf randomShapes
|> eval
printfn "%A" shapes
// gen some squares and print them
let squares =
Gen.nonEmptyListOf randomSquares
|> eval
printfn "%A" squares
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment